KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > jre12 > collections > custom > Db4oLinkedListTestCase


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.db4ounit.jre12.collections.custom;
22
23 import java.util.*;
24
25 import com.db4o.*;
26 import com.db4o.db4ounit.common.sampledata.*;
27 import com.db4o.ext.*;
28 import com.db4o.query.*;
29
30 import db4ounit.*;
31 import db4ounit.extensions.*;
32
33
34 /**
35  *
36  */

37 public class Db4oLinkedListTestCase extends AbstractDb4oTestCase {
38     
39     public static class Db4oLinkedListHelper{
40         public Db4oLinkedListHelper i_child;
41         public List i_childList;
42         
43     }
44     
45     static final int COUNT = 10;
46
47     static class Data {
48         List i_list;
49         Db4oLinkedListHelper i_helper;
50         List i_subList;
51     }
52     
53     private List createList() {
54         return db().collections().newLinkedList();
55     }
56     
57     protected void store(){
58         Data data=new Data();
59         data.i_list = createList();
60         setDefaultValues(data);
61         data.i_helper = helper(10);
62         store(data);
63     }
64     
65     private Db4oLinkedListHelper helper(int a_depth){
66         if(a_depth > 0){
67             Db4oLinkedListHelper helper = new Db4oLinkedListHelper();
68             helper.i_childList = createList();
69             helper.i_childList.add("hi");
70             helper.i_child = helper(a_depth - 1);
71             return helper;
72         }
73         return null;
74     }
75     
76     private void setDefaultValues(Data data){
77         data.i_list.add(new AtomData("wow"));
78         data.i_list.add(new AtomData("cool"));
79         data.i_list.add(new AtomData("great"));
80     }
81     
82     public void test() throws Exception JavaDoc{
83         Data data=(Data) retrieveOnlyInstance(Data.class);
84         checkHelper(data.i_helper);
85         runElementTest(data,true);
86
87         reopen();
88         
89        restoreMembers(data);
90        checkHelper(data.i_helper);
91        runElementTest(data,false);
92
93     }
94     
95     
96     private void runElementTest(Data data,boolean onOriginal) throws Exception JavaDoc{
97         
98         
99         List otherList = new ArrayList();
100         Iterator i = data.i_list.iterator();
101         AtomData atom = (AtomData)i.next();
102         Assert.areEqual("wow",atom.name);
103         otherList.add(atom);
104         atom = (AtomData)i.next();
105         Assert.areEqual("cool",atom.name);
106         otherList.add(atom);
107         atom = (AtomData)i.next();
108         Assert.areEqual("great",atom.name);
109         otherList.add(atom);
110         Assert.areEqual(3,data.i_list.size());
111         Assert.isFalse(data.i_list.isEmpty());
112         db().deactivate(data.i_list, Integer.MAX_VALUE);
113         Assert.areEqual("great",((AtomData)data.i_list.get(data.i_list.size() - 1)).name);
114         db().deactivate(data.i_list, Integer.MAX_VALUE);
115         
116         if(onOriginal){
117             Query q = newQuery();
118             Data template = new Data();
119             template.i_list = createList();
120             template.i_list.add(new AtomData("cool"));
121             q.constrain(template);
122             ObjectSet qResult = q.execute();
123             Assert.areEqual(1,qResult.size());
124             Assert.areEqual(data,qResult.next());
125         }
126         
127         
128         Assert.isTrue(data.i_list.containsAll(otherList));
129         
130         otherList.clear();
131         
132         Object JavaDoc[] arr = data.i_list.toArray();
133         Assert.areEqual(3,arr.length);
134         atom = (AtomData)arr[0];
135         Assert.areEqual("wow",atom.name);
136         atom = (AtomData)arr[1];
137         Assert.areEqual("cool",atom.name);
138         atom = (AtomData)arr[2];
139         Assert.areEqual("great",atom.name);
140         
141         
142         i = data.i_list.iterator();
143         atom = (AtomData)i.next();
144         Assert.areEqual("wow",atom.name);
145         atom = (AtomData)i.next();
146         Assert.areEqual("cool",atom.name);
147         atom = (AtomData)i.next();
148         Assert.areEqual("great",atom.name);
149         Assert.areEqual(3,data.i_list.size());
150         Assert.isFalse(i.hasNext());
151         
152         db().deactivate(data.i_list, Integer.MAX_VALUE);
153         Assert.isFalse(data.i_list.isEmpty());
154         db().deactivate(data.i_list, Integer.MAX_VALUE);
155         data.i_list.add(new AtomData("yup"));
156         Assert.areEqual(4,data.i_list.size());
157         i = data.i_list.iterator();
158         atom = (AtomData)i.next();
159         Assert.areEqual("wow",atom.name);
160         atom = (AtomData)i.next();
161         Assert.areEqual("cool",atom.name);
162         AtomData toRemove = (AtomData)i.next();
163         Assert.areEqual("great",toRemove.name);
164         atom = (AtomData)i.next();
165         Assert.areEqual("yup",atom.name);
166         Assert.isFalse(i.hasNext());
167         
168         Assert.isTrue(data.i_list.remove(toRemove));
169         Assert.isFalse(data.i_list.remove(toRemove));
170         
171         db().deactivate(data.i_list, Integer.MAX_VALUE);
172         i = data.i_list.iterator();
173         atom = (AtomData)i.next();
174         Assert.areEqual("wow",atom.name);
175         atom = (AtomData)i.next();
176         Assert.areEqual("cool",atom.name);
177         otherList.add(atom);
178         atom = (AtomData)i.next();
179         Assert.areEqual("yup",atom.name);
180         otherList.add(atom);
181         Assert.areEqual(3,data.i_list.size());
182         
183         Assert.isTrue(data.i_list.removeAll(otherList));
184         Assert.isFalse(data.i_list.removeAll(otherList));
185         Assert.areEqual(1,data.i_list.size());
186         i = data.i_list.iterator();
187         atom = (AtomData)i.next();
188         Assert.areEqual("wow",atom.name);
189         Assert.isFalse(i.hasNext());
190         
191         data.i_list.addAll(otherList);
192         Assert.areEqual(3,data.i_list.size());
193         i = data.i_list.iterator();
194         atom = (AtomData)i.next();
195         Assert.areEqual("wow",atom.name);
196         atom = (AtomData)i.next();
197         Assert.areEqual("cool",atom.name);
198         atom = (AtomData)i.next();
199         Assert.areEqual("yup",atom.name);
200         Assert.isFalse(i.hasNext());
201         
202         
203         AtomData[] atarr = new AtomData[1];
204         atarr = (AtomData[])data.i_list.toArray(atarr);
205         Assert.areEqual("wow",atarr[0].name);
206         Assert.areEqual("cool",atarr[1].name);
207         Assert.areEqual("yup",atarr[2].name);
208         
209         Assert.isTrue(data.i_list.removeAll(otherList));
210         
211         data.i_list.addAll(0, otherList);
212         i = data.i_list.iterator();
213         atom = (AtomData)i.next();
214         Assert.areEqual("cool",atom.name);
215         i.remove();
216         atom = (AtomData)i.next();
217         Assert.areEqual("yup",atom.name);
218         i.remove();
219         atom = (AtomData)i.next();
220         Assert.areEqual("wow",atom.name);
221         Assert.isFalse(i.hasNext());
222         Assert.areEqual(1,data.i_list.size());
223         
224         for (int j = 0; j < COUNT; j++) {
225             data.i_list.add("more and more " + j);
226         }
227         Assert.areEqual(COUNT + 1,data.i_list.size());
228         lookupLast(data);
229         
230         db().deactivate(data.i_list, Integer.MAX_VALUE);
231         lookupLast(data);
232         
233         reopen();
234         restoreMembers(data);
235         
236         lookupLast(data);
237         
238         String JavaDoc str = (String JavaDoc)data.i_list.set(10, new AtomData("yo"));
239         Assert.areEqual("more and more 9",str);
240         
241         atom = (AtomData)data.i_list.remove(10);
242         Assert.areEqual("yo",atom.name);
243         
244         data.i_list.add(5, new AtomData("sure"));
245         Assert.areEqual(COUNT+1,data.i_list.size());
246         atom = (AtomData)data.i_list.remove(5);
247         Assert.areEqual("sure",atom.name);
248         
249         data.i_list.add(0, new AtomData("sure"));
250         Assert.areEqual("sure",((AtomData)data.i_list.get(0)).name);
251         Assert.areEqual(COUNT + 1,data.i_list.size());
252         data.i_list.add(data.i_list.size(), new AtomData("sure"));
253         Assert.areEqual(COUNT + 2,data.i_list.size());
254         Assert.areEqual("sure",((AtomData)data.i_list.get(data.i_list.size() - 1)).name);
255         
256         atom = (AtomData)data.i_list.set(0, "huh");
257         Assert.areEqual("sure",atom.name);
258         Assert.areEqual(COUNT + 2,data.i_list.size());
259         
260         data.i_list.clear();
261         Assert.areEqual(0,data.i_list.size());
262         setDefaultValues(data);
263     }
264     
265     private void restoreMembers(Data data){
266         Query q = newQuery();
267         q.constrain(Data.class);
268         ObjectSet objectSet = q.execute();
269         Data dll = (Data)objectSet.next();
270         data.i_list = dll.i_list;
271         data.i_helper = dll.i_helper;
272     }
273     
274     private void lookupLast(Data data){
275         String JavaDoc str = (String JavaDoc)data.i_list.get(COUNT);
276         Assert.areEqual("more and more " + (COUNT - 1),str);
277     }
278     
279     void checkHelper(Db4oLinkedListHelper helper){
280         ExtObjectContainer con = db();
281         if(con.isActive(helper)){
282             Assert.areEqual("hi",helper.i_childList.get(0));
283             checkHelper(helper.i_child);
284         }
285     }
286 }
287
Popular Tags