KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.db4o.tools.*;
30 import com.db4o.types.*;
31
32
33 /**
34  *
35  */

36 public class Db4oHashMap {
37     
38     public static class Db4oHashMapHelper{
39         public Db4oHashMapHelper i_child;
40         public List i_childList;
41         
42     }
43     
44     static final int COUNT = 10;
45     
46     static final String JavaDoc[] DEFAULT = {"wow", "cool", "great"};
47     static final String JavaDoc MORE = "more and more ";
48     
49     Map i_map;
50     Db4oHashMapHelper i_helper;
51     
52     public void storeOne(){
53         i_map = Test.objectContainer().collections().newHashMap(10);
54         setDefaultValues(i_map);
55         i_helper = helper(10);
56     }
57     
58     private static Db4oHashMapHelper helper(int a_depth){
59         if(a_depth > 0){
60             Db4oHashMapHelper helper = new Db4oHashMapHelper();
61             helper.i_childList = Test.objectContainer().collections().newLinkedList();
62             helper.i_childList.add("hi");
63             helper.i_child = helper(a_depth - 1);
64             return helper;
65         }
66         return null;
67     }
68     
69     private void setDefaultValues(Map a_map){
70         for (int i = 0; i < DEFAULT.length; i++) {
71             a_map.put(DEFAULT[i], new Atom(DEFAULT[i]));
72         }
73     }
74     
75     public void testOne(){
76         
77         ObjectContainer oc = Test.objectContainer();
78         
79         checkHelper(i_helper);
80         runElementTest(true);
81         
82         oc = Test.objectContainer();
83         oc.set(this);
84         oc.set(i_helper);
85         oc.commit();
86         
87         checkHelper(i_helper);
88         runElementTest(false);
89
90         
91         boolean defrag = true;
92         
93         if(defrag){
94             
95             Test.commit();
96             
97             close();
98             
99             try {
100                 new Defragment().run(currentFileName(), true);
101             } finally {
102                 
103                 reOpen();
104             }
105             
106            restoreMembers();
107            checkHelper(i_helper);
108            runElementTest(false);
109         }
110     }
111     
112     
113     private void runElementTest(boolean onOriginal){
114         
115         Map otherMap = new HashMap();
116         
117         Atom atom = null;
118         
119         tDefaultValues();
120         
121         int itCount = 0;
122         Iterator i = i_map.keySet().iterator();
123         while(i.hasNext()){
124             String JavaDoc str = (String JavaDoc)i.next();
125             itCount ++;
126             atom = (Atom)i_map.get(str);
127             Test.ensure(atom.name.equals(str));
128             otherMap.put(str, atom);
129         }
130         Test.ensure(itCount == DEFAULT.length);
131         
132         
133         Test.ensure(i_map.size() == DEFAULT.length);
134         Test.ensure(i_map.isEmpty() == false);
135         Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
136         i_map.get("great");
137         Test.ensure(((Atom)i_map.get("great")).name.equals("great"));
138         Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
139         
140         if(onOriginal){
141             Query q = Test.query();
142             Db4oHashMap template = new Db4oHashMap();
143             template.i_map = Test.objectContainer().collections().newHashMap(1);
144             template.i_map.put("cool", new Atom("cool"));
145             q.constrain(template);
146             ObjectSet qResult = q.execute();
147             Test.ensure(qResult.size() == 1);
148             Test.ensure(qResult.next() == this);
149         }
150         
151         Test.ensure(i_map.keySet().containsAll(otherMap.keySet()));
152         
153         
154         Object JavaDoc[] arr = i_map.keySet().toArray();
155         tDefaultArray(arr);
156         
157         
158         String JavaDoc[] cmp = new String JavaDoc[DEFAULT.length];
159         System.arraycopy(DEFAULT,0, cmp, 0, DEFAULT.length);
160         
161         i = i_map.keySet().iterator();
162         while(i.hasNext()){
163             String JavaDoc str = (String JavaDoc)i.next();
164             boolean found = false;
165             for (int j = 0; j < cmp.length; j++) {
166                 if(str.equals(cmp[j])){
167                     cmp[j] = null;
168                     found = true;
169                 }
170             }
171             Test.ensure(found);
172         }
173         
174         for (int j = 0; j < cmp.length; j++) {
175             Test.ensure(cmp[j] == null);
176         }
177         
178         Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
179         Test.ensure(i_map.isEmpty() == false);
180         Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
181         i_map.put("yup", new Atom("yup"));
182         
183         Test.objectContainer().set(this);
184         Test.objectContainer().set(this);
185         Test.objectContainer().set(this);
186         Test.objectContainer().set(i_map);
187         Test.objectContainer().set(i_map);
188         Test.objectContainer().set(i_map);
189         Test.objectContainer().set(i_helper);
190         Test.objectContainer().set(i_helper);
191         Test.objectContainer().set(i_helper);
192         Test.objectContainer().commit();
193         
194         Test.ensure(i_map.size() == 4);
195         
196         atom = (Atom)i_map.get("yup");
197         Test.ensure(atom.name.equals("yup"));
198         
199         Atom removed = (Atom)i_map.remove("great");
200         
201         Test.ensure(removed.name.equals("great"));
202         Test.ensure(i_map.remove("great") == null);
203         Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
204         Test.ensure(i_map.size() == 3);
205         
206         Test.ensure(i_map.keySet().removeAll(otherMap.keySet()));
207         Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
208         Test.ensure(! i_map.keySet().removeAll(otherMap.keySet()));
209         Test.ensure(i_map.size() == 1);
210         i = i_map.keySet().iterator();
211         String JavaDoc str = (String JavaDoc)i.next();
212         Test.ensure(str.equals("yup"));
213         Test.ensure(! i.hasNext());
214         
215         i_map.clear();
216         Test.ensure(i_map.isEmpty());
217         Test.ensure(i_map.size() == 0);
218
219         setDefaultValues(i_map);
220         
221         String JavaDoc[] strArr = new String JavaDoc[1];
222         strArr = (String JavaDoc[])i_map.keySet().toArray(strArr);
223         tDefaultArray(strArr);
224         
225         i_map.clear();
226         i_map.put("zero", "zero");
227
228         long start = System.currentTimeMillis();
229         
230         for (int j = 0; j < COUNT; j++) {
231             i_map.put(MORE + j, new Atom(MORE + j));
232         }
233         long stop = System.currentTimeMillis();
234         // System.out.println("Time to put " + COUNT + " elements: " + (stop - start) + "ms");
235
Test.ensure(i_map.size() == COUNT + 1);
236         lookupLast();
237         
238         Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
239         // System.out.println("Deactivated");
240
lookupLast();
241         // System.out.println("Activated");
242
lookupLast();
243         
244         Test.reOpen();
245         restoreMembers();
246         // System.out.println("Reopened");
247
lookupLast();
248         
249         atom = new Atom("double");
250         
251         i_map.put("double", atom);
252         
253         int previousSize = i_map.size();
254         
255         Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
256         
257         Atom doubleAtom = (Atom)i_map.put("double", new Atom("double"));
258         Test.ensure(atom == doubleAtom);
259         
260         Test.ensure(i_map.size() == previousSize);
261         i_map.put("double", doubleAtom);
262         
263         Test.commit();
264         
265         i_map.put("rollBack", "rollBack");
266         i_map.put("double", new Atom("nono"));
267         
268         Test.rollBack();
269         Test.ensure(i_map.get("rollBack") == null);
270         Test.ensure(i_map.size() == previousSize);
271         atom = (Atom)i_map.get("double");
272         Test.ensure(atom == doubleAtom);
273         Test.ensure(i_map.containsKey("double"));
274         Test.ensure(! i_map.containsKey("rollBack"));
275         
276         otherMap.clear();
277         otherMap.put("other1", doubleAtom);
278         otherMap.put("other2", doubleAtom);
279         
280         i_map.putAll(otherMap);
281         Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
282         
283         Test.ensure(i_map.get("other1") == doubleAtom);
284         Test.ensure(i_map.get("other2") == doubleAtom);
285         
286         
287         i_map.clear();
288         Test.ensure(i_map.size() == 0);
289         setDefaultValues(i_map);
290         
291         int j = 0;
292         i = i_map.keySet().iterator();
293         while(i.hasNext()){
294             String JavaDoc key = (String JavaDoc)i.next();
295             if(key.equals("cool")){
296                 i.remove();
297             }
298             j++;
299         }
300         Test.ensure(i_map.size() == 2);
301         Test.ensure(! i_map.containsKey("cool"));
302         Test.ensure(j == 3);
303         
304         
305         i_map.put("double", doubleAtom);
306         ((Db4oMap)i_map).deleteRemoved(true);
307         i_map.keySet().remove("double");
308         Test.ensure(! Test.objectContainer().isStored(doubleAtom));
309         ((Db4oMap)i_map).deleteRemoved(false);
310         
311         i_map.clear();
312         Test.ensure(i_map.size() == 0);
313         setDefaultValues(i_map);
314     }
315     
316     private void tDefaultValues(){
317         for (int i = 0; i < DEFAULT.length; i++) {
318             Atom atom = (Atom)i_map.get(DEFAULT[i]);
319             Test.ensure(atom.name.equals(DEFAULT[i]));
320         }
321     }
322     
323     private void tDefaultArray(Object JavaDoc[] arr){
324         Test.ensure(arr.length == DEFAULT.length);
325         String JavaDoc str[] = new String JavaDoc[DEFAULT.length];
326         System.arraycopy(DEFAULT,0, str, 0, DEFAULT.length);
327         for (int i = 0; i < arr.length; i++) {
328             boolean found = false;
329             for (int j = 0; j < str.length; j++) {
330                 if(arr[i].equals(str[j])){
331                     str[j] = null;
332                     found = true;
333                 }
334             }
335             Test.ensure(found);
336         }
337         for (int j = 0; j < str.length; j++) {
338             Test.ensure(str[j] == null);
339         }
340     }
341
342     
343     private void restoreMembers(){
344         Query q = Test.query();
345         q.constrain(this.getClass());
346         ObjectSet objectSet = q.execute();
347         Db4oHashMap dll = (Db4oHashMap)objectSet.next();
348         i_map = dll.i_map;
349         i_helper = dll.i_helper;
350     }
351     
352     private void lookupLast(){
353         long start = System.currentTimeMillis();
354         Atom atom = (Atom)i_map.get(MORE + (COUNT - 1));
355         long stop = System.currentTimeMillis();
356         // System.out.println("Time to look up element " + COUNT + ": " + (stop - start) + "ms");
357
Test.ensure(atom.name. equals(MORE + (COUNT - 1)));
358     }
359     
360     void checkHelper(Db4oHashMapHelper helper){
361         ExtObjectContainer con = Test.objectContainer();
362         if(con.isActive(helper)){
363             Test.ensure(helper.i_childList.get(0).equals("hi"));
364             checkHelper(helper.i_child);
365         }
366     }
367     
368     private String JavaDoc currentFileName() {
369         return Test.isClientServer()
370             ? Test.FILE_SERVER
371             : Test.FILE_SOLO;
372     }
373     
374     private void close() {
375         Test.close();
376         if (Test.isClientServer()) {
377             Test.server().close();
378         }
379     }
380     
381     private void reOpen() {
382         Test.reOpenServer();
383     }
384     
385
386 }
387
Popular Tags