KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > legacy > Db4oLinkedList


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.test.legacy;
22
23 import java.util.*;
24
25 import com.db4o.*;
26 import com.db4o.ext.*;
27 import com.db4o.query.*;
28 import com.db4o.test.*;
29
30
31 /**
32  *
33  */

34 public class Db4oLinkedList {
35     
36     public static class Db4oLinkedListHelper{
37         public Db4oLinkedListHelper i_child;
38         public List i_childList;
39         
40     }
41     
42     static final int COUNT = 10;
43     
44     List i_list;
45     Db4oLinkedListHelper i_helper;
46     List i_subList;
47     
48     public void storeOne(){
49         i_list = Test.objectContainer().collections().newLinkedList();
50         setDefaultValues();
51         i_helper = helper(10);
52     }
53     
54     private static Db4oLinkedListHelper helper(int a_depth){
55         if(a_depth > 0){
56             Db4oLinkedListHelper helper = new Db4oLinkedListHelper();
57             helper.i_childList = Test.objectContainer().collections().newLinkedList();
58             helper.i_childList.add("hi");
59             helper.i_child = helper(a_depth - 1);
60             return helper;
61         }
62         return null;
63     }
64     
65     private void setDefaultValues(){
66         i_list.add(new Atom("wow"));
67         i_list.add(new Atom("cool"));
68         i_list.add(new Atom("great"));
69     }
70     
71     public void testOne(){
72         
73         checkHelper(i_helper);
74         runElementTest(true);
75         
76         Test.defragment();
77         
78        restoreMembers();
79        checkHelper(i_helper);
80        runElementTest(false);
81
82     }
83     
84     
85     private void runElementTest(boolean onOriginal){
86         
87         
88         List otherList = new ArrayList();
89         Iterator i = i_list.iterator();
90         Atom atom = (Atom)i.next();
91         Test.ensure(atom.name.equals("wow"));
92         otherList.add(atom);
93         atom = (Atom)i.next();
94         Test.ensure(atom.name.equals("cool"));
95         otherList.add(atom);
96         atom = (Atom)i.next();
97         Test.ensure(atom.name.equals("great"));
98         otherList.add(atom);
99         Test.ensure(i_list.size() == 3);
100         Test.ensure(i_list.isEmpty() == false);
101         Test.objectContainer().deactivate(i_list, Integer.MAX_VALUE);
102         Test.ensure(((Atom)i_list.get(i_list.size() - 1)).name.equals("great"));
103         Test.objectContainer().deactivate(i_list, Integer.MAX_VALUE);
104         
105         if(onOriginal){
106             Query q = Test.query();
107             Db4oLinkedList template = new Db4oLinkedList();
108             template.i_list = Test.objectContainer().collections().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         
117         Test.ensure(i_list.containsAll(otherList));
118         
119         otherList.clear();
120         
121         Object JavaDoc[] arr = i_list.toArray();
122         Test.ensure(arr.length ==3);
123         atom = (Atom)arr[0];
124         Test.ensure(atom.name.equals("wow"));
125         atom = (Atom)arr[1];
126         Test.ensure(atom.name.equals("cool"));
127         atom = (Atom)arr[2];
128         Test.ensure(atom.name.equals("great"));
129         
130         
131         i = i_list.iterator();
132         atom = (Atom)i.next();
133         Test.ensure(atom.name.equals("wow"));
134         atom = (Atom)i.next();
135         Test.ensure(atom.name.equals("cool"));
136         atom = (Atom)i.next();
137         Test.ensure(atom.name.equals("great"));
138         Test.ensure(i_list.size() == 3);
139         Test.ensure(! i.hasNext());
140         
141         Test.objectContainer().deactivate(i_list, Integer.MAX_VALUE);
142         Test.ensure(i_list.isEmpty() == false);
143         Test.objectContainer().deactivate(i_list, Integer.MAX_VALUE);
144         i_list.add(new Atom("yup"));
145         Test.ensure(i_list.size() == 4);
146         i = i_list.iterator();
147         atom = (Atom)i.next();
148         Test.ensure(atom.name.equals("wow"));
149         atom = (Atom)i.next();
150         Test.ensure(atom.name.equals("cool"));
151         Atom toRemove = (Atom)i.next();
152         Test.ensure(toRemove.name.equals("great"));
153         atom = (Atom)i.next();
154         Test.ensure(atom.name.equals("yup"));
155         Test.ensure(! i.hasNext());
156         
157         Test.ensure(i_list.remove(toRemove));
158         Test.ensure(! i_list.remove(toRemove));
159         
160         
161         
162         Test.objectContainer().deactivate(i_list, Integer.MAX_VALUE);
163         i = i_list.iterator();
164         atom = (Atom)i.next();
165         Test.ensure(atom.name.equals("wow"));
166         atom = (Atom)i.next();
167         Test.ensure(atom.name.equals("cool"));
168         otherList.add(atom);
169         atom = (Atom)i.next();
170         Test.ensure(atom.name.equals("yup"));
171         otherList.add(atom);
172         Test.ensure(i_list.size() == 3);
173         
174         Test.ensure(i_list.removeAll(otherList));
175         Test.ensure(! i_list.removeAll(otherList));
176         Test.ensure(i_list.size() == 1);
177         i = i_list.iterator();
178         atom = (Atom)i.next();
179         Test.ensure(atom.name.equals("wow"));
180         Test.ensure(! i.hasNext());
181         
182         i_list.addAll(otherList);
183         Test.ensure(i_list.size() == 3);
184         i = i_list.iterator();
185         atom = (Atom)i.next();
186         Test.ensure(atom.name.equals("wow"));
187         atom = (Atom)i.next();
188         Test.ensure(atom.name.equals("cool"));
189         atom = (Atom)i.next();
190         Test.ensure(atom.name.equals("yup"));
191         Test.ensure(! i.hasNext());
192         
193         
194         Atom[] atarr = new Atom[1];
195         atarr = (Atom[])i_list.toArray(atarr);
196         Test.ensure(atarr[0].name.equals("wow"));
197         Test.ensure(atarr[1].name.equals("cool"));
198         Test.ensure(atarr[2].name.equals("yup"));
199         
200         Test.ensure(i_list.removeAll(otherList));
201         
202         i_list.addAll(0, otherList);
203         i = i_list.iterator();
204         atom = (Atom)i.next();
205         Test.ensure(atom.name.equals("cool"));
206         i.remove();
207         atom = (Atom)i.next();
208         Test.ensure(atom.name.equals("yup"));
209         i.remove();
210         atom = (Atom)i.next();
211         Test.ensure(atom.name.equals("wow"));
212         Test.ensure(! i.hasNext());
213         Test.ensure(i_list.size() == 1);
214         
215         long start = System.currentTimeMillis();
216         
217         for (int j = 0; j < COUNT; j++) {
218             i_list.add("more and more " + j);
219         }
220         long stop = System.currentTimeMillis();
221         // System.out.println("Time to add " + COUNT + " elements: " + (stop - start) + "ms");
222
Test.ensure(i_list.size() == COUNT + 1);
223         lookupLast();
224         
225         Test.objectContainer().deactivate(i_list, Integer.MAX_VALUE);
226         lookupLast();
227         
228         Test.reOpen();
229         restoreMembers();
230         
231         lookupLast();
232         
233         String JavaDoc str = (String JavaDoc)i_list.set(10, new Atom("yo"));
234         Test.ensure(str.equals("more and more 9"));
235         
236         atom = (Atom)i_list.remove(10);
237         Test.ensure(atom.name.equals("yo"));
238         
239         i_list.add(5, new Atom("sure"));
240         Test.ensure(i_list.size() == COUNT + 1);
241         atom = (Atom)i_list.remove(5);
242         Test.ensure(atom.name.equals("sure"));
243         
244         i_list.add(0, new Atom("sure"));
245         Test.ensure(((Atom)i_list.get(0)).name.equals("sure"));
246         Test.ensure(i_list.size() == COUNT + 1);
247         i_list.add(i_list.size(), new Atom("sure"));
248         Test.ensure(i_list.size() == COUNT + 2);
249         Test.ensure(((Atom)i_list.get(i_list.size() - 1)).name.equals("sure"));
250         
251         atom = (Atom)i_list.set(0, "huh");
252         Test.ensure(atom.name.equals("sure"));
253         Test.ensure(i_list.size() == COUNT + 2);
254         
255         i_list.clear();
256         Test.ensure(i_list.size() == 0);
257         setDefaultValues();
258     }
259     
260     private void restoreMembers(){
261         Query q = Test.query();
262         q.constrain(this.getClass());
263         ObjectSet objectSet = q.execute();
264         Db4oLinkedList dll = (Db4oLinkedList)objectSet.next();
265         i_list = dll.i_list;
266         i_helper = dll.i_helper;
267     }
268     
269     private void lookupLast(){
270         long start = System.currentTimeMillis();
271         String JavaDoc str = (String JavaDoc)i_list.get(COUNT);
272         long stop = System.currentTimeMillis();
273         // System.out.println("Time to look up element " + COUNT + ": " + (stop - start) + "ms");
274
Test.ensure(str.equals("more and more " + (COUNT - 1)));
275     }
276     
277     void checkHelper(Db4oLinkedListHelper helper){
278         ExtObjectContainer con = Test.objectContainer();
279         if(con.isActive(helper)){
280             Test.ensure(helper.i_childList.get(0).equals("hi"));
281             checkHelper(helper.i_child);
282         }
283     }
284     
285     private String JavaDoc currentFileName() {
286         return Test.isClientServer()
287             ? Test.FILE_SERVER
288             : Test.FILE_SOLO;
289     }
290     
291     private void close() {
292         Test.close();
293         if (Test.isClientServer()) {
294             Test.server().close();
295         }
296     }
297     
298     private void reOpen() {
299         Test.reOpenServer();
300     }
301
302     
303
304 }
305
Popular Tags