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