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.db4ounit.jre12.collections.custom;
022:
023: import java.util.*;
024:
025: import com.db4o.*;
026: import com.db4o.db4ounit.common.sampledata.*;
027: import com.db4o.ext.*;
028: import com.db4o.query.*;
029:
030: import db4ounit.*;
031: import db4ounit.extensions.*;
032:
033: /**
034: *
035: */
036: public class Db4oLinkedListTestCase extends AbstractDb4oTestCase {
037:
038: public static class Db4oLinkedListHelper {
039: public Db4oLinkedListHelper i_child;
040: public List i_childList;
041:
042: }
043:
044: static final int COUNT = 10;
045:
046: static class Data {
047: List i_list;
048: Db4oLinkedListHelper i_helper;
049: List i_subList;
050: }
051:
052: private List createList() {
053: return db().collections().newLinkedList();
054: }
055:
056: protected void store() {
057: Data data = new Data();
058: data.i_list = createList();
059: setDefaultValues(data);
060: data.i_helper = helper(10);
061: store(data);
062: }
063:
064: private Db4oLinkedListHelper helper(int a_depth) {
065: if (a_depth > 0) {
066: Db4oLinkedListHelper helper = new Db4oLinkedListHelper();
067: helper.i_childList = createList();
068: helper.i_childList.add("hi");
069: helper.i_child = helper(a_depth - 1);
070: return helper;
071: }
072: return null;
073: }
074:
075: private void setDefaultValues(Data data) {
076: data.i_list.add(new AtomData("wow"));
077: data.i_list.add(new AtomData("cool"));
078: data.i_list.add(new AtomData("great"));
079: }
080:
081: public void test() throws Exception {
082: Data data = (Data) retrieveOnlyInstance(Data.class);
083: checkHelper(data.i_helper);
084: runElementTest(data, true);
085:
086: reopen();
087:
088: restoreMembers(data);
089: checkHelper(data.i_helper);
090: runElementTest(data, false);
091:
092: }
093:
094: private void runElementTest(Data data, boolean onOriginal)
095: throws Exception {
096:
097: List otherList = new ArrayList();
098: Iterator i = data.i_list.iterator();
099: AtomData atom = (AtomData) i.next();
100: Assert.areEqual("wow", atom.name);
101: otherList.add(atom);
102: atom = (AtomData) i.next();
103: Assert.areEqual("cool", atom.name);
104: otherList.add(atom);
105: atom = (AtomData) i.next();
106: Assert.areEqual("great", atom.name);
107: otherList.add(atom);
108: Assert.areEqual(3, data.i_list.size());
109: Assert.isFalse(data.i_list.isEmpty());
110: db().deactivate(data.i_list, Integer.MAX_VALUE);
111: Assert.areEqual("great", ((AtomData) data.i_list
112: .get(data.i_list.size() - 1)).name);
113: db().deactivate(data.i_list, Integer.MAX_VALUE);
114:
115: if (onOriginal) {
116: Query q = newQuery();
117: Data template = new Data();
118: template.i_list = createList();
119: template.i_list.add(new AtomData("cool"));
120: q.constrain(template);
121: ObjectSet qResult = q.execute();
122: Assert.areEqual(1, qResult.size());
123: Assert.areEqual(data, qResult.next());
124: }
125:
126: Assert.isTrue(data.i_list.containsAll(otherList));
127:
128: otherList.clear();
129:
130: Object[] arr = data.i_list.toArray();
131: Assert.areEqual(3, arr.length);
132: atom = (AtomData) arr[0];
133: Assert.areEqual("wow", atom.name);
134: atom = (AtomData) arr[1];
135: Assert.areEqual("cool", atom.name);
136: atom = (AtomData) arr[2];
137: Assert.areEqual("great", atom.name);
138:
139: i = data.i_list.iterator();
140: atom = (AtomData) i.next();
141: Assert.areEqual("wow", atom.name);
142: atom = (AtomData) i.next();
143: Assert.areEqual("cool", atom.name);
144: atom = (AtomData) i.next();
145: Assert.areEqual("great", atom.name);
146: Assert.areEqual(3, data.i_list.size());
147: Assert.isFalse(i.hasNext());
148:
149: db().deactivate(data.i_list, Integer.MAX_VALUE);
150: Assert.isFalse(data.i_list.isEmpty());
151: db().deactivate(data.i_list, Integer.MAX_VALUE);
152: data.i_list.add(new AtomData("yup"));
153: Assert.areEqual(4, data.i_list.size());
154: i = data.i_list.iterator();
155: atom = (AtomData) i.next();
156: Assert.areEqual("wow", atom.name);
157: atom = (AtomData) i.next();
158: Assert.areEqual("cool", atom.name);
159: AtomData toRemove = (AtomData) i.next();
160: Assert.areEqual("great", toRemove.name);
161: atom = (AtomData) i.next();
162: Assert.areEqual("yup", atom.name);
163: Assert.isFalse(i.hasNext());
164:
165: Assert.isTrue(data.i_list.remove(toRemove));
166: Assert.isFalse(data.i_list.remove(toRemove));
167:
168: db().deactivate(data.i_list, Integer.MAX_VALUE);
169: i = data.i_list.iterator();
170: atom = (AtomData) i.next();
171: Assert.areEqual("wow", atom.name);
172: atom = (AtomData) i.next();
173: Assert.areEqual("cool", atom.name);
174: otherList.add(atom);
175: atom = (AtomData) i.next();
176: Assert.areEqual("yup", atom.name);
177: otherList.add(atom);
178: Assert.areEqual(3, data.i_list.size());
179:
180: Assert.isTrue(data.i_list.removeAll(otherList));
181: Assert.isFalse(data.i_list.removeAll(otherList));
182: Assert.areEqual(1, data.i_list.size());
183: i = data.i_list.iterator();
184: atom = (AtomData) i.next();
185: Assert.areEqual("wow", atom.name);
186: Assert.isFalse(i.hasNext());
187:
188: data.i_list.addAll(otherList);
189: Assert.areEqual(3, data.i_list.size());
190: i = data.i_list.iterator();
191: atom = (AtomData) i.next();
192: Assert.areEqual("wow", atom.name);
193: atom = (AtomData) i.next();
194: Assert.areEqual("cool", atom.name);
195: atom = (AtomData) i.next();
196: Assert.areEqual("yup", atom.name);
197: Assert.isFalse(i.hasNext());
198:
199: AtomData[] atarr = new AtomData[1];
200: atarr = (AtomData[]) data.i_list.toArray(atarr);
201: Assert.areEqual("wow", atarr[0].name);
202: Assert.areEqual("cool", atarr[1].name);
203: Assert.areEqual("yup", atarr[2].name);
204:
205: Assert.isTrue(data.i_list.removeAll(otherList));
206:
207: data.i_list.addAll(0, otherList);
208: i = data.i_list.iterator();
209: atom = (AtomData) i.next();
210: Assert.areEqual("cool", atom.name);
211: i.remove();
212: atom = (AtomData) i.next();
213: Assert.areEqual("yup", atom.name);
214: i.remove();
215: atom = (AtomData) i.next();
216: Assert.areEqual("wow", atom.name);
217: Assert.isFalse(i.hasNext());
218: Assert.areEqual(1, data.i_list.size());
219:
220: for (int j = 0; j < COUNT; j++) {
221: data.i_list.add("more and more " + j);
222: }
223: Assert.areEqual(COUNT + 1, data.i_list.size());
224: lookupLast(data);
225:
226: db().deactivate(data.i_list, Integer.MAX_VALUE);
227: lookupLast(data);
228:
229: reopen();
230: restoreMembers(data);
231:
232: lookupLast(data);
233:
234: String str = (String) data.i_list.set(10, new AtomData("yo"));
235: Assert.areEqual("more and more 9", str);
236:
237: atom = (AtomData) data.i_list.remove(10);
238: Assert.areEqual("yo", atom.name);
239:
240: data.i_list.add(5, new AtomData("sure"));
241: Assert.areEqual(COUNT + 1, data.i_list.size());
242: atom = (AtomData) data.i_list.remove(5);
243: Assert.areEqual("sure", atom.name);
244:
245: data.i_list.add(0, new AtomData("sure"));
246: Assert.areEqual("sure", ((AtomData) data.i_list.get(0)).name);
247: Assert.areEqual(COUNT + 1, data.i_list.size());
248: data.i_list.add(data.i_list.size(), new AtomData("sure"));
249: Assert.areEqual(COUNT + 2, data.i_list.size());
250: Assert.areEqual("sure", ((AtomData) data.i_list.get(data.i_list
251: .size() - 1)).name);
252:
253: atom = (AtomData) data.i_list.set(0, "huh");
254: Assert.areEqual("sure", atom.name);
255: Assert.areEqual(COUNT + 2, data.i_list.size());
256:
257: data.i_list.clear();
258: Assert.areEqual(0, data.i_list.size());
259: setDefaultValues(data);
260: }
261:
262: private void restoreMembers(Data data) {
263: Query q = newQuery();
264: q.constrain(Data.class);
265: ObjectSet objectSet = q.execute();
266: Data dll = (Data) objectSet.next();
267: data.i_list = dll.i_list;
268: data.i_helper = dll.i_helper;
269: }
270:
271: private void lookupLast(Data data) {
272: String str = (String) data.i_list.get(COUNT);
273: Assert.areEqual("more and more " + (COUNT - 1), str);
274: }
275:
276: void checkHelper(Db4oLinkedListHelper helper) {
277: ExtObjectContainer con = db();
278: if (con.isActive(helper)) {
279: Assert.areEqual("hi", helper.i_childList.get(0));
280: checkHelper(helper.i_child);
281: }
282: }
283: }
|