001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.runtime.map;
018:
019: import org.objectweb.speedo.SpeedoTestHelper;
020: import org.objectweb.speedo.pobjects.map.AllMap;
021:
022: import javax.jdo.PersistenceManager;
023: import java.util.HashMap;
024: import java.util.Map;
025: import java.util.Properties;
026: import java.util.Iterator;
027:
028: import junit.framework.Assert;
029:
030: /**
031: *
032: * @author S.Chassande-Barrioz
033: */
034: public class TestAllMap extends SpeedoTestHelper {
035:
036: public TestAllMap(String s) {
037: super (s);
038: }
039:
040: protected String getLoggerName() {
041: return LOG_NAME + ".rt.map.TestAllMap";
042: }
043:
044: public void testNullMap() {
045: PersistenceManager pm = pmf.getPersistenceManager();
046: pm.currentTransaction().begin();
047: AllMap am = new AllMap("testNullMap");
048: pm.makePersistent(am);
049: Object oid = pm.getObjectId(am);
050: Assert.assertNotNull("The identifier is null", oid);
051: am.setLong2Long(null);
052: am.setlong2Ref(null);
053: am.setProp(null);
054: am.setStr2Long(null);
055: am.setStr2Ref(null);
056: pm.currentTransaction().commit();
057: pm.close();
058: pm = null;
059: am = null;
060: pm = pmf.getPersistenceManager();
061: am = (AllMap) pm.getObjectById(oid, true);
062: Assert.assertNotNull("No persistence found with the id " + oid,
063: am);
064: Assert.assertTrue("The Map (Long,Long) is not null", am
065: .getMap_Long2Long().isEmpty());
066: Assert.assertTrue("The Map (Long,Ref) is not null", am
067: .getMap_Long2Ref().isEmpty());
068: Assert.assertTrue("The Map (String,Long) is not null", am
069: .getMap_str2Long().isEmpty());
070: Assert.assertTrue("The Map (String,Ref) is not null", am
071: .getMap_str2Ref().isEmpty());
072:
073: Assert.assertTrue("The HashMap (Long,Long) is not null", am
074: .getHmap_Long2Long().isEmpty());
075: Assert.assertTrue("The HashMap (Long,Ref) is not null", am
076: .getHmap_Long2Ref().isEmpty());
077: Assert.assertTrue("The HashMap (String,Long) is not null", am
078: .getHmap_str2Long().isEmpty());
079: Assert.assertTrue("The HashMap (String,Ref) is not null", am
080: .getHmap_str2Ref().isEmpty());
081:
082: Assert.assertTrue("The Hashtable (Long,Long) is not null", am
083: .getHt_Long2Long().isEmpty());
084: Assert.assertTrue("The Hashtable (Long,Ref) is not null", am
085: .getHt_Long2Ref().isEmpty());
086: Assert.assertTrue("The Hashtable (String,Long) is not null", am
087: .getHt_str2Long().isEmpty());
088: Assert.assertTrue("The Hashtable (String,Ref) is not null", am
089: .getHt_str2Ref().isEmpty());
090:
091: Assert
092: .assertNotNull("The Properties is not null", am
093: .getProp());
094: pm.currentTransaction().begin();
095: pm.deletePersistent(am);
096: pm.currentTransaction().commit();
097: pm.close();
098: }
099:
100: public void testEmptyMap() {
101: PersistenceManager pm = pmf.getPersistenceManager();
102: pm.currentTransaction().begin();
103: AllMap am = new AllMap("testEmptyMap");
104: pm.makePersistent(am);
105: Object oid = pm.getObjectId(am);
106: Assert.assertNotNull("The identifier is null", oid);
107: am.setLong2Long(new HashMap());
108: am.setlong2Ref(new HashMap());
109: am.setProp(new HashMap());
110: am.setStr2Long(new HashMap());
111: am.setStr2Ref(new HashMap());
112: pm.currentTransaction().commit();
113: pm.close();
114: pm = null;
115: am = null;
116: pm = pmf.getPersistenceManager();
117: am = (AllMap) pm.getObjectById(oid, true);
118: Assert.assertNotNull("No persistence found with the id " + oid,
119: am);
120:
121: Assert.assertNotNull("The Map (Long,Long) ", am
122: .getMap_Long2Long());
123: assertEquals("The Map (Long,Long) : bad size", 0, am
124: .getMap_Long2Long().size());
125:
126: Assert.assertNotNull("The Map (Long,Ref) ", am
127: .getMap_Long2Ref());
128: assertEquals("The Map (Long,Ref) : bad size", 0, am
129: .getMap_Long2Ref().size());
130:
131: Assert.assertNotNull("The Map (String,Long) ", am
132: .getMap_str2Long());
133: assertEquals("The Map (String,Long) : bad size", 0, am
134: .getMap_str2Long().size());
135:
136: Assert.assertNotNull("The Map (String,Ref) ", am
137: .getMap_str2Ref());
138: assertEquals("The Map (String,Ref) : bad size", 0, am
139: .getMap_str2Ref().size());
140:
141: Assert.assertNotNull("The HashMap (Long,Long) ", am
142: .getHmap_Long2Long());
143: assertEquals("The HashMap (Long,Long) : bad size", 0, am
144: .getHmap_Long2Long().size());
145:
146: Assert.assertNotNull("The HashMap (Long,Ref) ", am
147: .getHmap_Long2Ref());
148: assertEquals("The HashMap (Long,Ref) : bad size", 0, am
149: .getHmap_Long2Ref().size());
150:
151: Assert.assertNotNull("The HashMap (String,Long) ", am
152: .getHmap_str2Long());
153: assertEquals("The HashMap (String,Long) : bad size", 0, am
154: .getHmap_str2Long().size());
155:
156: Assert.assertNotNull("The HashMap (String,Ref) ", am
157: .getHmap_str2Ref());
158: assertEquals("The HashMap (String,Ref) : bad size", 0, am
159: .getHmap_str2Ref().size());
160:
161: Assert.assertNotNull("The Hashtable (Long,Long) ", am
162: .getHt_Long2Long());
163: assertEquals("The Hashtable (Long,Long) : bad size", 0, am
164: .getHt_Long2Long().size());
165:
166: Assert.assertNotNull("The Hashtable (Long,Ref) ", am
167: .getHt_Long2Ref());
168: assertEquals("The Hashtable (Long,Ref) : bad size", 0, am
169: .getHt_Long2Ref().size());
170:
171: Assert.assertNotNull("The Hashtable (String,Long) ", am
172: .getHt_str2Long());
173: assertEquals("The Hashtable (String,Long) : bad size", 0, am
174: .getHt_str2Long().size());
175:
176: Assert.assertNotNull("The Hashtable (String,Ref) ", am
177: .getHt_str2Ref());
178: assertEquals("The Hashtable (String,Ref) : bad size", 0, am
179: .getHt_str2Ref().size());
180:
181: Assert.assertNotNull("The Properties ", am.getProp());
182: assertEquals("The Properties : bad size", 0, am.getProp()
183: .size());
184: pm.currentTransaction().begin();
185: pm.deletePersistent(am);
186: pm.currentTransaction().commit();
187: pm.close();
188: }
189:
190: public void testTwoElem() {
191: PersistenceManager pm = pmf.getPersistenceManager();
192: pm.currentTransaction().begin();
193: AllMap am = new AllMap("testTwoElem");
194: Map l2l = new HashMap();
195: l2l.put(new Long(12), new Long(34));
196: l2l.put(new Long(56), new Long(78));
197: am.setLong2Long(l2l);
198:
199: Map l2r = new HashMap();
200: l2r.put(new Long(12), am);
201: am.setlong2Ref(l2r);
202:
203: Properties p = new Properties();
204: p.put("a", "b");
205: p.put("c", "d");
206: am.setProp(p);
207:
208: Map s2l = new HashMap();
209: s2l.put("a", new Long(12));
210: s2l.put("b", new Long(34));
211: am.setStr2Long(s2l);
212:
213: Map s2r = new HashMap();
214: s2r.put("a", am);
215: am.setStr2Ref(s2r);
216:
217: pm.makePersistent(am);
218: Object oid = pm.getObjectId(am);
219: Assert.assertNotNull("The identifier is null", oid);
220:
221: pm.currentTransaction().commit();
222: pm.close();
223: pm = null;
224: am = null;
225: pm = pmf.getPersistenceManager();
226: am = (AllMap) pm.getObjectById(oid, true);
227: Assert.assertNotNull("No persistence found with the id " + oid,
228: am);
229:
230: Assert.assertNotNull("The Map (Long,Long) ", am
231: .getMap_Long2Long());
232: assertEquals("The Map (Long,Long)", l2l, am.getMap_Long2Long());
233:
234: Assert.assertNotNull("The Map (Long,Ref) ", am
235: .getMap_Long2Ref());
236: assertEquals("The Map (Long,Ref)", l2r, am.getMap_Long2Ref());
237:
238: Assert.assertNotNull("The Map (String,Long) ", am
239: .getMap_str2Long());
240: assertEquals("The Map (String,Long)", s2l, am.getMap_str2Long());
241:
242: Assert.assertNotNull("The Map (String,Ref) ", am
243: .getMap_str2Ref());
244: assertEquals("The Map (String,Ref)", s2r, am.getMap_str2Ref());
245:
246: Assert.assertNotNull("The HashMap (Long,Long) ", am
247: .getHmap_Long2Long());
248: assertEquals("The HashMap (Long,Long)", l2l, am
249: .getHmap_Long2Long());
250:
251: Assert.assertNotNull("The HashMap (Long,Ref) ", am
252: .getHmap_Long2Ref());
253: assertEquals("The HashMap (Long,Ref) ", l2r, am
254: .getHmap_Long2Ref());
255:
256: Assert.assertNotNull("The HashMap (String,Long) ", am
257: .getHmap_str2Long());
258: assertEquals("The HashMap (String,Long) ", s2l, am
259: .getHmap_str2Long());
260:
261: Assert.assertNotNull("The HashMap (String,Ref) ", am
262: .getHmap_str2Ref());
263: assertEquals("The HashMap (String,Ref) ", s2r, am
264: .getHmap_str2Ref());
265:
266: Assert.assertNotNull("The Hashtable (Long,Long) ", am
267: .getHt_Long2Long());
268: assertEquals("The Hashtable (Long,Long) ", l2l, am
269: .getHt_Long2Long());
270:
271: Assert.assertNotNull("The Hashtable (Long,Ref) ", am
272: .getHt_Long2Ref());
273: assertEquals("The Hashtable (Long,Ref) ", l2r, am
274: .getHt_Long2Ref());
275:
276: Assert.assertNotNull("The Hashtable (String,Long) ", am
277: .getHt_str2Long());
278: assertEquals("The Hashtable (String,Long) ", s2l, am
279: .getHt_str2Long());
280:
281: Assert.assertNotNull("The Hashtable (String,Ref) ", am
282: .getHt_str2Ref());
283: assertEquals("The Hashtable (String,Ref) ", s2r, am
284: .getHt_str2Ref());
285:
286: Assert.assertNotNull("The Properties ", am.getProp());
287: assertEquals("The Properties ", p, am.getProp());
288: pm.currentTransaction().begin();
289: pm.deletePersistent(am);
290: pm.currentTransaction().commit();
291: pm.close();
292: }
293:
294: public void testPutSameElement() {
295: PersistenceManager pm = pmf.getPersistenceManager();
296: AllMap am = new AllMap("testTwoElem");
297: Map l2l = new HashMap();
298: l2l.put(new Long(12), new Long(34));
299: l2l.put(new Long(56), new Long(78));
300: am.setLong2Long(l2l);
301:
302: Properties p = new Properties();
303: p.put("a", "b");
304: p.put("c", "d");
305: am.setProp(p);
306:
307: Map s2l = new HashMap();
308: s2l.put("a", new Long(12));
309: s2l.put("b", new Long(34));
310: am.setStr2Long(s2l);
311:
312: pm.currentTransaction().begin();
313: pm.makePersistent(am);
314: checkAllMap(am, l2l, s2l, p);
315: Object oid = pm.getObjectId(am);
316: Assert.assertNotNull("The identifier is null", oid);
317: checkAllMap(am, l2l, s2l, p);
318: pm.currentTransaction().commit();
319:
320: pm.currentTransaction().begin();
321: checkAllMap(am, l2l, s2l, p);
322: pm.currentTransaction().commit();
323:
324: am = null;
325: pm.evictAll();
326: pm.currentTransaction().begin();
327: am = (AllMap) pm.getObjectById(oid, false);
328: checkAllMap(am, l2l, s2l, p);
329: pm.currentTransaction().commit();
330:
331: pm.currentTransaction().begin();
332:
333: l2l.put(new Long(12), new Long(35));
334: am.getMap_Long2Long().put(new Long(12), new Long(35));
335: am.getHmap_Long2Long().put(new Long(12), new Long(35));
336: am.getHt_Long2Long().put(new Long(12), new Long(35));
337:
338: l2l.put(new Long(13), new Long(38));
339: am.getMap_Long2Long().put(new Long(13), new Long(38));
340: am.getHmap_Long2Long().put(new Long(13), new Long(38));
341: am.getHt_Long2Long().put(new Long(13), new Long(38));
342:
343: p.setProperty("a", "x");
344: am.getProp().put("a", "x");
345:
346: p.setProperty("e", "f");
347: am.getProp().put("e", "f");
348:
349: s2l.put("a", new Long(13));
350: am.getMap_str2Long().put("a", new Long(13));
351: am.getHmap_str2Long().put("a", new Long(13));
352: am.getHt_str2Long().put("a", new Long(13));
353:
354: s2l.put("c", new Long(24));
355: am.getMap_str2Long().put("c", new Long(24));
356: am.getHmap_str2Long().put("c", new Long(24));
357: am.getHt_str2Long().put("c", new Long(24));
358:
359: checkAllMap(am, l2l, s2l, p);
360: pm.currentTransaction().commit();
361:
362: pm.currentTransaction().begin();
363: checkAllMap(am, l2l, s2l, p);
364: pm.currentTransaction().commit();
365:
366: am = null;
367: pm.evictAll();
368: pm.currentTransaction().begin();
369: am = (AllMap) pm.getObjectById(oid, false);
370: checkAllMap(am, l2l, s2l, p);
371: pm.currentTransaction().commit();
372:
373: pm.currentTransaction().begin();
374: pm.deletePersistent(am);
375: pm.currentTransaction().commit();
376:
377: pm.close();
378:
379: }
380:
381: private void checkAllMap(AllMap am, Map l2l, Map s2l, Properties p) {
382: assertEquals("The Map (Long,Long)", l2l, am.getMap_Long2Long());
383: assertEquals("The HashMap (Long,Long)", l2l, am
384: .getHmap_Long2Long());
385: assertEquals("The Hashtable (Long,Long) ", l2l, am
386: .getHt_Long2Long());
387:
388: assertEquals("The Map (String,Long)", s2l, am.getMap_str2Long());
389: assertEquals("The HashMap (String,Long) ", s2l, am
390: .getHmap_str2Long());
391: assertEquals("The Hashtable (String,Long) ", s2l, am
392: .getHt_str2Long());
393:
394: assertEquals("The Properties ", p, am.getProp());
395: }
396:
397: private void assertEquals(String msg, Map expected, Map found) {
398: if (expected == null) {
399: Assert.assertNull(msg + " is not null", found);
400: return;
401: }
402: Assert.assertNotNull(msg + " is null", found);
403: Iterator it = expected.entrySet().iterator();
404: while (it.hasNext()) {
405: Map.Entry me = (Map.Entry) it.next();
406: Object val = found.get(me.getKey());
407: Assert.assertNotNull(msg + ": the expected entry ("
408: + me.getKey() + ", " + me.getValue()
409: + ") has not been found (" + found.keySet() + ")",
410: val);
411: }
412: it = found.entrySet().iterator();
413: while (it.hasNext()) {
414: Map.Entry me = (Map.Entry) it.next();
415: Object val = expected.get(me.getKey());
416: Assert.assertNotNull(msg + ": the entry (" + me.getKey()
417: + ", " + me.getValue() + ") has not expected ("
418: + expected.keySet() + ")", val);
419: }
420: }
421:
422: }
|