001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.test.legacy;
022:
023: import java.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.ext.*;
027: import com.db4o.query.*;
028: import com.db4o.test.*;
029:
030: /**
031: *
032: */
033: public class Db4oLinkedList {
034:
035: public static class Db4oLinkedListHelper {
036: public Db4oLinkedListHelper i_child;
037: public List i_childList;
038:
039: }
040:
041: static final int COUNT = 10;
042:
043: List i_list;
044: Db4oLinkedListHelper i_helper;
045: List i_subList;
046:
047: public void storeOne() {
048: i_list = Test.objectContainer().collections().newLinkedList();
049: setDefaultValues();
050: i_helper = helper(10);
051: }
052:
053: private static Db4oLinkedListHelper helper(int a_depth) {
054: if (a_depth > 0) {
055: Db4oLinkedListHelper helper = new Db4oLinkedListHelper();
056: helper.i_childList = Test.objectContainer().collections()
057: .newLinkedList();
058: helper.i_childList.add("hi");
059: helper.i_child = helper(a_depth - 1);
060: return helper;
061: }
062: return null;
063: }
064:
065: private void setDefaultValues() {
066: i_list.add(new Atom("wow"));
067: i_list.add(new Atom("cool"));
068: i_list.add(new Atom("great"));
069: }
070:
071: public void testOne() {
072:
073: checkHelper(i_helper);
074: runElementTest(true);
075:
076: Test.defragment();
077:
078: restoreMembers();
079: checkHelper(i_helper);
080: runElementTest(false);
081:
082: }
083:
084: private void runElementTest(boolean onOriginal) {
085:
086: List otherList = new ArrayList();
087: Iterator i = i_list.iterator();
088: Atom atom = (Atom) i.next();
089: Test.ensure(atom.name.equals("wow"));
090: otherList.add(atom);
091: atom = (Atom) i.next();
092: Test.ensure(atom.name.equals("cool"));
093: otherList.add(atom);
094: atom = (Atom) i.next();
095: Test.ensure(atom.name.equals("great"));
096: otherList.add(atom);
097: Test.ensure(i_list.size() == 3);
098: Test.ensure(i_list.isEmpty() == false);
099: Test.objectContainer().deactivate(i_list, Integer.MAX_VALUE);
100: Test.ensure(((Atom) i_list.get(i_list.size() - 1)).name
101: .equals("great"));
102: Test.objectContainer().deactivate(i_list, Integer.MAX_VALUE);
103:
104: if (onOriginal) {
105: Query q = Test.query();
106: Db4oLinkedList template = new Db4oLinkedList();
107: template.i_list = Test.objectContainer().collections()
108: .newLinkedList();
109: template.i_list.add(new Atom("cool"));
110: q.constrain(template);
111: ObjectSet qResult = q.execute();
112: Test.ensure(qResult.size() == 1);
113: Test.ensure(qResult.next() == this );
114: }
115:
116: Test.ensure(i_list.containsAll(otherList));
117:
118: otherList.clear();
119:
120: Object[] arr = i_list.toArray();
121: Test.ensure(arr.length == 3);
122: atom = (Atom) arr[0];
123: Test.ensure(atom.name.equals("wow"));
124: atom = (Atom) arr[1];
125: Test.ensure(atom.name.equals("cool"));
126: atom = (Atom) arr[2];
127: Test.ensure(atom.name.equals("great"));
128:
129: i = i_list.iterator();
130: atom = (Atom) i.next();
131: Test.ensure(atom.name.equals("wow"));
132: atom = (Atom) i.next();
133: Test.ensure(atom.name.equals("cool"));
134: atom = (Atom) i.next();
135: Test.ensure(atom.name.equals("great"));
136: Test.ensure(i_list.size() == 3);
137: Test.ensure(!i.hasNext());
138:
139: Test.objectContainer().deactivate(i_list, Integer.MAX_VALUE);
140: Test.ensure(i_list.isEmpty() == false);
141: Test.objectContainer().deactivate(i_list, Integer.MAX_VALUE);
142: i_list.add(new Atom("yup"));
143: Test.ensure(i_list.size() == 4);
144: i = i_list.iterator();
145: atom = (Atom) i.next();
146: Test.ensure(atom.name.equals("wow"));
147: atom = (Atom) i.next();
148: Test.ensure(atom.name.equals("cool"));
149: Atom toRemove = (Atom) i.next();
150: Test.ensure(toRemove.name.equals("great"));
151: atom = (Atom) i.next();
152: Test.ensure(atom.name.equals("yup"));
153: Test.ensure(!i.hasNext());
154:
155: Test.ensure(i_list.remove(toRemove));
156: Test.ensure(!i_list.remove(toRemove));
157:
158: Test.objectContainer().deactivate(i_list, Integer.MAX_VALUE);
159: i = i_list.iterator();
160: atom = (Atom) i.next();
161: Test.ensure(atom.name.equals("wow"));
162: atom = (Atom) i.next();
163: Test.ensure(atom.name.equals("cool"));
164: otherList.add(atom);
165: atom = (Atom) i.next();
166: Test.ensure(atom.name.equals("yup"));
167: otherList.add(atom);
168: Test.ensure(i_list.size() == 3);
169:
170: Test.ensure(i_list.removeAll(otherList));
171: Test.ensure(!i_list.removeAll(otherList));
172: Test.ensure(i_list.size() == 1);
173: i = i_list.iterator();
174: atom = (Atom) i.next();
175: Test.ensure(atom.name.equals("wow"));
176: Test.ensure(!i.hasNext());
177:
178: i_list.addAll(otherList);
179: Test.ensure(i_list.size() == 3);
180: i = i_list.iterator();
181: atom = (Atom) i.next();
182: Test.ensure(atom.name.equals("wow"));
183: atom = (Atom) i.next();
184: Test.ensure(atom.name.equals("cool"));
185: atom = (Atom) i.next();
186: Test.ensure(atom.name.equals("yup"));
187: Test.ensure(!i.hasNext());
188:
189: Atom[] atarr = new Atom[1];
190: atarr = (Atom[]) i_list.toArray(atarr);
191: Test.ensure(atarr[0].name.equals("wow"));
192: Test.ensure(atarr[1].name.equals("cool"));
193: Test.ensure(atarr[2].name.equals("yup"));
194:
195: Test.ensure(i_list.removeAll(otherList));
196:
197: i_list.addAll(0, otherList);
198: i = i_list.iterator();
199: atom = (Atom) i.next();
200: Test.ensure(atom.name.equals("cool"));
201: i.remove();
202: atom = (Atom) i.next();
203: Test.ensure(atom.name.equals("yup"));
204: i.remove();
205: atom = (Atom) i.next();
206: Test.ensure(atom.name.equals("wow"));
207: Test.ensure(!i.hasNext());
208: Test.ensure(i_list.size() == 1);
209:
210: long start = System.currentTimeMillis();
211:
212: for (int j = 0; j < COUNT; j++) {
213: i_list.add("more and more " + j);
214: }
215: long stop = System.currentTimeMillis();
216: // System.out.println("Time to add " + COUNT + " elements: " + (stop - start) + "ms");
217: Test.ensure(i_list.size() == COUNT + 1);
218: lookupLast();
219:
220: Test.objectContainer().deactivate(i_list, Integer.MAX_VALUE);
221: lookupLast();
222:
223: Test.reOpen();
224: restoreMembers();
225:
226: lookupLast();
227:
228: String str = (String) i_list.set(10, new Atom("yo"));
229: Test.ensure(str.equals("more and more 9"));
230:
231: atom = (Atom) i_list.remove(10);
232: Test.ensure(atom.name.equals("yo"));
233:
234: i_list.add(5, new Atom("sure"));
235: Test.ensure(i_list.size() == COUNT + 1);
236: atom = (Atom) i_list.remove(5);
237: Test.ensure(atom.name.equals("sure"));
238:
239: i_list.add(0, new Atom("sure"));
240: Test.ensure(((Atom) i_list.get(0)).name.equals("sure"));
241: Test.ensure(i_list.size() == COUNT + 1);
242: i_list.add(i_list.size(), new Atom("sure"));
243: Test.ensure(i_list.size() == COUNT + 2);
244: Test.ensure(((Atom) i_list.get(i_list.size() - 1)).name
245: .equals("sure"));
246:
247: atom = (Atom) i_list.set(0, "huh");
248: Test.ensure(atom.name.equals("sure"));
249: Test.ensure(i_list.size() == COUNT + 2);
250:
251: i_list.clear();
252: Test.ensure(i_list.size() == 0);
253: setDefaultValues();
254: }
255:
256: private void restoreMembers() {
257: Query q = Test.query();
258: q.constrain(this .getClass());
259: ObjectSet objectSet = q.execute();
260: Db4oLinkedList dll = (Db4oLinkedList) objectSet.next();
261: i_list = dll.i_list;
262: i_helper = dll.i_helper;
263: }
264:
265: private void lookupLast() {
266: long start = System.currentTimeMillis();
267: String str = (String) i_list.get(COUNT);
268: long stop = System.currentTimeMillis();
269: // System.out.println("Time to look up element " + COUNT + ": " + (stop - start) + "ms");
270: Test.ensure(str.equals("more and more " + (COUNT - 1)));
271: }
272:
273: void checkHelper(Db4oLinkedListHelper helper) {
274: ExtObjectContainer con = Test.objectContainer();
275: if (con.isActive(helper)) {
276: Test.ensure(helper.i_childList.get(0).equals("hi"));
277: checkHelper(helper.i_child);
278: }
279: }
280:
281: private String currentFileName() {
282: return Test.isClientServer() ? Test.FILE_SERVER
283: : Test.FILE_SOLO;
284: }
285:
286: private void close() {
287: Test.close();
288: if (Test.isClientServer()) {
289: Test.server().close();
290: }
291: }
292:
293: private void reOpen() {
294: Test.reOpenServer();
295: }
296:
297: }
|