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: import com.db4o.tools.*;
030: import com.db4o.types.*;
031:
032: /**
033: *
034: */
035: public class Db4oHashMap {
036:
037: public static class Db4oHashMapHelper {
038: public Db4oHashMapHelper i_child;
039: public List i_childList;
040:
041: }
042:
043: static final int COUNT = 10;
044:
045: static final String[] DEFAULT = { "wow", "cool", "great" };
046: static final String MORE = "more and more ";
047:
048: Map i_map;
049: Db4oHashMapHelper i_helper;
050:
051: public void storeOne() {
052: i_map = Test.objectContainer().collections().newHashMap(10);
053: setDefaultValues(i_map);
054: i_helper = helper(10);
055: }
056:
057: private static Db4oHashMapHelper helper(int a_depth) {
058: if (a_depth > 0) {
059: Db4oHashMapHelper helper = new Db4oHashMapHelper();
060: helper.i_childList = Test.objectContainer().collections()
061: .newLinkedList();
062: helper.i_childList.add("hi");
063: helper.i_child = helper(a_depth - 1);
064: return helper;
065: }
066: return null;
067: }
068:
069: private void setDefaultValues(Map a_map) {
070: for (int i = 0; i < DEFAULT.length; i++) {
071: a_map.put(DEFAULT[i], new Atom(DEFAULT[i]));
072: }
073: }
074:
075: public void testOne() {
076:
077: ObjectContainer oc = Test.objectContainer();
078:
079: checkHelper(i_helper);
080: runElementTest(true);
081:
082: oc = Test.objectContainer();
083: oc.set(this );
084: oc.set(i_helper);
085: oc.commit();
086:
087: checkHelper(i_helper);
088: runElementTest(false);
089:
090: boolean defrag = true;
091:
092: if (defrag) {
093:
094: Test.commit();
095:
096: close();
097:
098: try {
099: new Defragment().run(currentFileName(), true);
100: } finally {
101:
102: reOpen();
103: }
104:
105: restoreMembers();
106: checkHelper(i_helper);
107: runElementTest(false);
108: }
109: }
110:
111: private void runElementTest(boolean onOriginal) {
112:
113: Map otherMap = new HashMap();
114:
115: Atom atom = null;
116:
117: tDefaultValues();
118:
119: int itCount = 0;
120: Iterator i = i_map.keySet().iterator();
121: while (i.hasNext()) {
122: String str = (String) i.next();
123: itCount++;
124: atom = (Atom) i_map.get(str);
125: Test.ensure(atom.name.equals(str));
126: otherMap.put(str, atom);
127: }
128: Test.ensure(itCount == DEFAULT.length);
129:
130: Test.ensure(i_map.size() == DEFAULT.length);
131: Test.ensure(i_map.isEmpty() == false);
132: Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
133: i_map.get("great");
134: Test.ensure(((Atom) i_map.get("great")).name.equals("great"));
135: Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
136:
137: if (onOriginal) {
138: Query q = Test.query();
139: Db4oHashMap template = new Db4oHashMap();
140: template.i_map = Test.objectContainer().collections()
141: .newHashMap(1);
142: template.i_map.put("cool", new Atom("cool"));
143: q.constrain(template);
144: ObjectSet qResult = q.execute();
145: Test.ensure(qResult.size() == 1);
146: Test.ensure(qResult.next() == this );
147: }
148:
149: Test.ensure(i_map.keySet().containsAll(otherMap.keySet()));
150:
151: Object[] arr = i_map.keySet().toArray();
152: tDefaultArray(arr);
153:
154: String[] cmp = new String[DEFAULT.length];
155: System.arraycopy(DEFAULT, 0, cmp, 0, DEFAULT.length);
156:
157: i = i_map.keySet().iterator();
158: while (i.hasNext()) {
159: String str = (String) i.next();
160: boolean found = false;
161: for (int j = 0; j < cmp.length; j++) {
162: if (str.equals(cmp[j])) {
163: cmp[j] = null;
164: found = true;
165: }
166: }
167: Test.ensure(found);
168: }
169:
170: for (int j = 0; j < cmp.length; j++) {
171: Test.ensure(cmp[j] == null);
172: }
173:
174: Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
175: Test.ensure(i_map.isEmpty() == false);
176: Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
177: i_map.put("yup", new Atom("yup"));
178:
179: Test.objectContainer().set(this );
180: Test.objectContainer().set(this );
181: Test.objectContainer().set(this );
182: Test.objectContainer().set(i_map);
183: Test.objectContainer().set(i_map);
184: Test.objectContainer().set(i_map);
185: Test.objectContainer().set(i_helper);
186: Test.objectContainer().set(i_helper);
187: Test.objectContainer().set(i_helper);
188: Test.objectContainer().commit();
189:
190: Test.ensure(i_map.size() == 4);
191:
192: atom = (Atom) i_map.get("yup");
193: Test.ensure(atom.name.equals("yup"));
194:
195: Atom removed = (Atom) i_map.remove("great");
196:
197: Test.ensure(removed.name.equals("great"));
198: Test.ensure(i_map.remove("great") == null);
199: Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
200: Test.ensure(i_map.size() == 3);
201:
202: Test.ensure(i_map.keySet().removeAll(otherMap.keySet()));
203: Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
204: Test.ensure(!i_map.keySet().removeAll(otherMap.keySet()));
205: Test.ensure(i_map.size() == 1);
206: i = i_map.keySet().iterator();
207: String str = (String) i.next();
208: Test.ensure(str.equals("yup"));
209: Test.ensure(!i.hasNext());
210:
211: i_map.clear();
212: Test.ensure(i_map.isEmpty());
213: Test.ensure(i_map.size() == 0);
214:
215: setDefaultValues(i_map);
216:
217: String[] strArr = new String[1];
218: strArr = (String[]) i_map.keySet().toArray(strArr);
219: tDefaultArray(strArr);
220:
221: i_map.clear();
222: i_map.put("zero", "zero");
223:
224: long start = System.currentTimeMillis();
225:
226: for (int j = 0; j < COUNT; j++) {
227: i_map.put(MORE + j, new Atom(MORE + j));
228: }
229: long stop = System.currentTimeMillis();
230: // System.out.println("Time to put " + COUNT + " elements: " + (stop - start) + "ms");
231: Test.ensure(i_map.size() == COUNT + 1);
232: lookupLast();
233:
234: Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
235: // System.out.println("Deactivated");
236: lookupLast();
237: // System.out.println("Activated");
238: lookupLast();
239:
240: Test.reOpen();
241: restoreMembers();
242: // System.out.println("Reopened");
243: lookupLast();
244:
245: atom = new Atom("double");
246:
247: i_map.put("double", atom);
248:
249: int previousSize = i_map.size();
250:
251: Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
252:
253: Atom doubleAtom = (Atom) i_map
254: .put("double", new Atom("double"));
255: Test.ensure(atom == doubleAtom);
256:
257: Test.ensure(i_map.size() == previousSize);
258: i_map.put("double", doubleAtom);
259:
260: Test.commit();
261:
262: i_map.put("rollBack", "rollBack");
263: i_map.put("double", new Atom("nono"));
264:
265: Test.rollBack();
266: Test.ensure(i_map.get("rollBack") == null);
267: Test.ensure(i_map.size() == previousSize);
268: atom = (Atom) i_map.get("double");
269: Test.ensure(atom == doubleAtom);
270: Test.ensure(i_map.containsKey("double"));
271: Test.ensure(!i_map.containsKey("rollBack"));
272:
273: otherMap.clear();
274: otherMap.put("other1", doubleAtom);
275: otherMap.put("other2", doubleAtom);
276:
277: i_map.putAll(otherMap);
278: Test.objectContainer().deactivate(i_map, Integer.MAX_VALUE);
279:
280: Test.ensure(i_map.get("other1") == doubleAtom);
281: Test.ensure(i_map.get("other2") == doubleAtom);
282:
283: i_map.clear();
284: Test.ensure(i_map.size() == 0);
285: setDefaultValues(i_map);
286:
287: int j = 0;
288: i = i_map.keySet().iterator();
289: while (i.hasNext()) {
290: String key = (String) i.next();
291: if (key.equals("cool")) {
292: i.remove();
293: }
294: j++;
295: }
296: Test.ensure(i_map.size() == 2);
297: Test.ensure(!i_map.containsKey("cool"));
298: Test.ensure(j == 3);
299:
300: i_map.put("double", doubleAtom);
301: ((Db4oMap) i_map).deleteRemoved(true);
302: i_map.keySet().remove("double");
303: Test.ensure(!Test.objectContainer().isStored(doubleAtom));
304: ((Db4oMap) i_map).deleteRemoved(false);
305:
306: i_map.clear();
307: Test.ensure(i_map.size() == 0);
308: setDefaultValues(i_map);
309: }
310:
311: private void tDefaultValues() {
312: for (int i = 0; i < DEFAULT.length; i++) {
313: Atom atom = (Atom) i_map.get(DEFAULT[i]);
314: Test.ensure(atom.name.equals(DEFAULT[i]));
315: }
316: }
317:
318: private void tDefaultArray(Object[] arr) {
319: Test.ensure(arr.length == DEFAULT.length);
320: String str[] = new String[DEFAULT.length];
321: System.arraycopy(DEFAULT, 0, str, 0, DEFAULT.length);
322: for (int i = 0; i < arr.length; i++) {
323: boolean found = false;
324: for (int j = 0; j < str.length; j++) {
325: if (arr[i].equals(str[j])) {
326: str[j] = null;
327: found = true;
328: }
329: }
330: Test.ensure(found);
331: }
332: for (int j = 0; j < str.length; j++) {
333: Test.ensure(str[j] == null);
334: }
335: }
336:
337: private void restoreMembers() {
338: Query q = Test.query();
339: q.constrain(this .getClass());
340: ObjectSet objectSet = q.execute();
341: Db4oHashMap dll = (Db4oHashMap) objectSet.next();
342: i_map = dll.i_map;
343: i_helper = dll.i_helper;
344: }
345:
346: private void lookupLast() {
347: long start = System.currentTimeMillis();
348: Atom atom = (Atom) i_map.get(MORE + (COUNT - 1));
349: long stop = System.currentTimeMillis();
350: // System.out.println("Time to look up element " + COUNT + ": " + (stop - start) + "ms");
351: Test.ensure(atom.name.equals(MORE + (COUNT - 1)));
352: }
353:
354: void checkHelper(Db4oHashMapHelper helper) {
355: ExtObjectContainer con = Test.objectContainer();
356: if (con.isActive(helper)) {
357: Test.ensure(helper.i_childList.get(0).equals("hi"));
358: checkHelper(helper.i_child);
359: }
360: }
361:
362: private String currentFileName() {
363: return Test.isClientServer() ? Test.FILE_SERVER
364: : Test.FILE_SOLO;
365: }
366:
367: private void close() {
368: Test.close();
369: if (Test.isClientServer()) {
370: Test.server().close();
371: }
372: }
373:
374: private void reOpen() {
375: Test.reOpenServer();
376: }
377:
378: }
|