KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > map > TestAllMap


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.runtime.map;
19
20 import org.objectweb.speedo.SpeedoTestHelper;
21 import org.objectweb.speedo.pobjects.map.AllMap;
22
23 import javax.jdo.PersistenceManager;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import junit.framework.Assert;
30
31 /**
32  *
33  * @author S.Chassande-Barrioz
34  */

35 public class TestAllMap extends SpeedoTestHelper {
36
37     public TestAllMap(String JavaDoc s) {
38         super(s);
39     }
40
41     protected String JavaDoc getLoggerName() {
42         return LOG_NAME + ".rt.map.TestAllMap";
43     }
44
45     public void testNullMap() {
46         PersistenceManager pm = pmf.getPersistenceManager();
47         pm.currentTransaction().begin();
48         AllMap am = new AllMap("testNullMap");
49         pm.makePersistent(am);
50         Object JavaDoc oid = pm.getObjectId(am);
51         Assert.assertNotNull("The identifier is null", oid);
52         am.setLong2Long(null);
53         am.setlong2Ref(null);
54         am.setProp(null);
55         am.setStr2Long(null);
56         am.setStr2Ref(null);
57         pm.currentTransaction().commit();
58         pm.close();
59         pm = null;
60         am = null;
61         pm = pmf.getPersistenceManager();
62         am = (AllMap) pm.getObjectById(oid, true);
63         Assert.assertNotNull("No persistence found with the id " + oid, am);
64         Assert.assertTrue("The Map (Long,Long) is not null", am.getMap_Long2Long().isEmpty());
65         Assert.assertTrue("The Map (Long,Ref) is not null", am.getMap_Long2Ref().isEmpty());
66         Assert.assertTrue("The Map (String,Long) is not null", am.getMap_str2Long().isEmpty());
67         Assert.assertTrue("The Map (String,Ref) is not null", am.getMap_str2Ref().isEmpty());
68
69         Assert.assertTrue("The HashMap (Long,Long) is not null", am.getHmap_Long2Long().isEmpty());
70         Assert.assertTrue("The HashMap (Long,Ref) is not null", am.getHmap_Long2Ref().isEmpty());
71         Assert.assertTrue("The HashMap (String,Long) is not null", am.getHmap_str2Long().isEmpty());
72         Assert.assertTrue("The HashMap (String,Ref) is not null", am.getHmap_str2Ref().isEmpty());
73
74         Assert.assertTrue("The Hashtable (Long,Long) is not null", am.getHt_Long2Long().isEmpty());
75         Assert.assertTrue("The Hashtable (Long,Ref) is not null", am.getHt_Long2Ref().isEmpty());
76         Assert.assertTrue("The Hashtable (String,Long) is not null", am.getHt_str2Long().isEmpty());
77         Assert.assertTrue("The Hashtable (String,Ref) is not null", am.getHt_str2Ref().isEmpty());
78
79         Assert.assertNotNull("The Properties is not null", am.getProp());
80         pm.currentTransaction().begin();
81         pm.deletePersistent(am);
82         pm.currentTransaction().commit();
83         pm.close();
84     }
85
86     public void testEmptyMap() {
87         PersistenceManager pm = pmf.getPersistenceManager();
88         pm.currentTransaction().begin();
89         AllMap am = new AllMap("testEmptyMap");
90         pm.makePersistent(am);
91         Object JavaDoc oid = pm.getObjectId(am);
92         Assert.assertNotNull("The identifier is null", oid);
93         am.setLong2Long(new HashMap JavaDoc());
94         am.setlong2Ref(new HashMap JavaDoc());
95         am.setProp(new HashMap JavaDoc());
96         am.setStr2Long(new HashMap JavaDoc());
97         am.setStr2Ref(new HashMap JavaDoc());
98         pm.currentTransaction().commit();
99         pm.close();
100         pm = null;
101         am = null;
102         pm = pmf.getPersistenceManager();
103         am = (AllMap) pm.getObjectById(oid, true);
104         Assert.assertNotNull("No persistence found with the id " + oid, am);
105
106         Assert.assertNotNull("The Map (Long,Long) ", am.getMap_Long2Long());
107         assertEquals("The Map (Long,Long) : bad size", 0, am.getMap_Long2Long().size());
108
109         Assert.assertNotNull("The Map (Long,Ref) ", am.getMap_Long2Ref());
110         assertEquals("The Map (Long,Ref) : bad size", 0, am.getMap_Long2Ref().size());
111
112         Assert.assertNotNull("The Map (String,Long) ", am.getMap_str2Long());
113         assertEquals("The Map (String,Long) : bad size", 0, am.getMap_str2Long().size());
114
115         Assert.assertNotNull("The Map (String,Ref) ", am.getMap_str2Ref());
116         assertEquals("The Map (String,Ref) : bad size", 0, am.getMap_str2Ref().size());
117
118
119         Assert.assertNotNull("The HashMap (Long,Long) ", am.getHmap_Long2Long());
120         assertEquals("The HashMap (Long,Long) : bad size", 0, am.getHmap_Long2Long().size());
121
122         Assert.assertNotNull("The HashMap (Long,Ref) ", am.getHmap_Long2Ref());
123         assertEquals("The HashMap (Long,Ref) : bad size", 0, am.getHmap_Long2Ref().size());
124
125         Assert.assertNotNull("The HashMap (String,Long) ", am.getHmap_str2Long());
126         assertEquals("The HashMap (String,Long) : bad size", 0, am.getHmap_str2Long().size());
127
128         Assert.assertNotNull("The HashMap (String,Ref) ", am.getHmap_str2Ref());
129         assertEquals("The HashMap (String,Ref) : bad size", 0, am.getHmap_str2Ref().size());
130
131
132         Assert.assertNotNull("The Hashtable (Long,Long) ", am.getHt_Long2Long());
133         assertEquals("The Hashtable (Long,Long) : bad size", 0, am.getHt_Long2Long().size());
134
135         Assert.assertNotNull("The Hashtable (Long,Ref) ", am.getHt_Long2Ref());
136         assertEquals("The Hashtable (Long,Ref) : bad size", 0, am.getHt_Long2Ref().size());
137
138         Assert.assertNotNull("The Hashtable (String,Long) ", am.getHt_str2Long());
139         assertEquals("The Hashtable (String,Long) : bad size", 0, am.getHt_str2Long().size());
140
141         Assert.assertNotNull("The Hashtable (String,Ref) ", am.getHt_str2Ref());
142         assertEquals("The Hashtable (String,Ref) : bad size", 0, am.getHt_str2Ref().size());
143
144         Assert.assertNotNull("The Properties ", am.getProp());
145         assertEquals("The Properties : bad size", 0, am.getProp().size());
146         pm.currentTransaction().begin();
147         pm.deletePersistent(am);
148         pm.currentTransaction().commit();
149         pm.close();
150     }
151
152
153     public void testTwoElem() {
154         PersistenceManager pm = pmf.getPersistenceManager();
155         pm.currentTransaction().begin();
156         AllMap am = new AllMap("testTwoElem");
157         Map l2l = new HashMap JavaDoc();
158         l2l.put(new Long JavaDoc(12), new Long JavaDoc(34));
159         l2l.put(new Long JavaDoc(56), new Long JavaDoc(78));
160         am.setLong2Long(l2l);
161
162         Map l2r = new HashMap JavaDoc();
163         l2r.put(new Long JavaDoc(12), am);
164         am.setlong2Ref(l2r);
165
166         Properties JavaDoc p = new Properties JavaDoc();
167         p.put("a","b");
168         p.put("c","d");
169         am.setProp(p);
170
171         Map s2l = new HashMap JavaDoc();
172         s2l.put("a", new Long JavaDoc(12));
173         s2l.put("b", new Long JavaDoc(34));
174         am.setStr2Long(s2l);
175
176         Map s2r = new HashMap JavaDoc();
177         s2r.put("a", am);
178         am.setStr2Ref(s2r);
179
180         pm.makePersistent(am);
181         Object JavaDoc oid = pm.getObjectId(am);
182         Assert.assertNotNull("The identifier is null", oid);
183
184         pm.currentTransaction().commit();
185         pm.close();
186         pm = null;
187         am = null;
188         pm = pmf.getPersistenceManager();
189         am = (AllMap) pm.getObjectById(oid, true);
190         Assert.assertNotNull("No persistence found with the id " + oid, am);
191
192         Assert.assertNotNull("The Map (Long,Long) ", am.getMap_Long2Long());
193         assertEquals("The Map (Long,Long)", l2l, am.getMap_Long2Long());
194
195         Assert.assertNotNull("The Map (Long,Ref) ", am.getMap_Long2Ref());
196         assertEquals("The Map (Long,Ref)", l2r, am.getMap_Long2Ref());
197
198         Assert.assertNotNull("The Map (String,Long) ", am.getMap_str2Long());
199         assertEquals("The Map (String,Long)", s2l, am.getMap_str2Long());
200
201         Assert.assertNotNull("The Map (String,Ref) ", am.getMap_str2Ref());
202         assertEquals("The Map (String,Ref)", s2r, am.getMap_str2Ref());
203
204
205         Assert.assertNotNull("The HashMap (Long,Long) ", am.getHmap_Long2Long());
206         assertEquals("The HashMap (Long,Long)", l2l, am.getHmap_Long2Long());
207
208         Assert.assertNotNull("The HashMap (Long,Ref) ", am.getHmap_Long2Ref());
209         assertEquals("The HashMap (Long,Ref) ", l2r, am.getHmap_Long2Ref());
210
211         Assert.assertNotNull("The HashMap (String,Long) ", am.getHmap_str2Long());
212         assertEquals("The HashMap (String,Long) ", s2l, am.getHmap_str2Long());
213
214         Assert.assertNotNull("The HashMap (String,Ref) ", am.getHmap_str2Ref());
215         assertEquals("The HashMap (String,Ref) ", s2r, am.getHmap_str2Ref());
216
217
218         Assert.assertNotNull("The Hashtable (Long,Long) ", am.getHt_Long2Long());
219         assertEquals("The Hashtable (Long,Long) ", l2l, am.getHt_Long2Long());
220
221         Assert.assertNotNull("The Hashtable (Long,Ref) ", am.getHt_Long2Ref());
222         assertEquals("The Hashtable (Long,Ref) ", l2r, am.getHt_Long2Ref());
223
224         Assert.assertNotNull("The Hashtable (String,Long) ", am.getHt_str2Long());
225         assertEquals("The Hashtable (String,Long) ", s2l, am.getHt_str2Long());
226
227         Assert.assertNotNull("The Hashtable (String,Ref) ", am.getHt_str2Ref());
228         assertEquals("The Hashtable (String,Ref) ", s2r, am.getHt_str2Ref());
229
230         Assert.assertNotNull("The Properties ", am.getProp());
231         assertEquals("The Properties ", p, am.getProp());
232         pm.currentTransaction().begin();
233         pm.deletePersistent(am);
234         pm.currentTransaction().commit();
235         pm.close();
236     }
237
238     public void testPutSameElement() {
239         PersistenceManager pm = pmf.getPersistenceManager();
240         AllMap am = new AllMap("testTwoElem");
241         Map l2l = new HashMap JavaDoc();
242         l2l.put(new Long JavaDoc(12), new Long JavaDoc(34));
243         l2l.put(new Long JavaDoc(56), new Long JavaDoc(78));
244         am.setLong2Long(l2l);
245
246         Properties JavaDoc p = new Properties JavaDoc();
247         p.put("a","b");
248         p.put("c","d");
249         am.setProp(p);
250
251         Map s2l = new HashMap JavaDoc();
252         s2l.put("a", new Long JavaDoc(12));
253         s2l.put("b", new Long JavaDoc(34));
254         am.setStr2Long(s2l);
255
256         pm.currentTransaction().begin();
257         pm.makePersistent(am);
258         checkAllMap(am, l2l, s2l, p);
259         Object JavaDoc oid = pm.getObjectId(am);
260         Assert.assertNotNull("The identifier is null", oid);
261         checkAllMap(am, l2l, s2l, p);
262         pm.currentTransaction().commit();
263
264         pm.currentTransaction().begin();
265         checkAllMap(am, l2l, s2l, p);
266         pm.currentTransaction().commit();
267
268         am = null;
269         pm.evictAll();
270         pm.currentTransaction().begin();
271         am = (AllMap) pm.getObjectById(oid, false);
272         checkAllMap(am, l2l, s2l, p);
273         pm.currentTransaction().commit();
274
275         pm.currentTransaction().begin();
276
277         l2l.put(new Long JavaDoc(12), new Long JavaDoc(35));
278         am.getMap_Long2Long().put(new Long JavaDoc(12), new Long JavaDoc(35));
279         am.getHmap_Long2Long().put(new Long JavaDoc(12), new Long JavaDoc(35));
280         am.getHt_Long2Long().put(new Long JavaDoc(12), new Long JavaDoc(35));
281
282         l2l.put(new Long JavaDoc(13), new Long JavaDoc(38));
283         am.getMap_Long2Long().put(new Long JavaDoc(13), new Long JavaDoc(38));
284         am.getHmap_Long2Long().put(new Long JavaDoc(13), new Long JavaDoc(38));
285         am.getHt_Long2Long().put(new Long JavaDoc(13), new Long JavaDoc(38));
286
287         p.setProperty("a", "x");
288         am.getProp().put("a", "x");
289         
290         p.setProperty("e", "f");
291         am.getProp().put("e", "f");
292         
293         s2l.put("a", new Long JavaDoc(13));
294         am.getMap_str2Long().put("a", new Long JavaDoc(13));
295         am.getHmap_str2Long().put("a", new Long JavaDoc(13));
296         am.getHt_str2Long().put("a", new Long JavaDoc(13));
297
298         s2l.put("c", new Long JavaDoc(24));
299         am.getMap_str2Long().put("c", new Long JavaDoc(24));
300         am.getHmap_str2Long().put("c", new Long JavaDoc(24));
301         am.getHt_str2Long().put("c", new Long JavaDoc(24));
302
303         checkAllMap(am, l2l, s2l, p);
304         pm.currentTransaction().commit();
305         
306         
307         pm.currentTransaction().begin();
308         checkAllMap(am, l2l, s2l, p);
309         pm.currentTransaction().commit();
310
311         am = null;
312         pm.evictAll();
313         pm.currentTransaction().begin();
314         am = (AllMap) pm.getObjectById(oid, false);
315         checkAllMap(am, l2l, s2l, p);
316         pm.currentTransaction().commit();
317         
318         pm.currentTransaction().begin();
319         pm.deletePersistent(am);
320         pm.currentTransaction().commit();
321         
322         pm.close();
323         
324     }
325
326     private void checkAllMap(AllMap am, Map l2l, Map s2l, Properties JavaDoc p) {
327         assertEquals("The Map (Long,Long)", l2l, am.getMap_Long2Long());
328         assertEquals("The HashMap (Long,Long)", l2l, am.getHmap_Long2Long());
329         assertEquals("The Hashtable (Long,Long) ", l2l, am.getHt_Long2Long());
330
331         assertEquals("The Map (String,Long)", s2l, am.getMap_str2Long());
332         assertEquals("The HashMap (String,Long) ", s2l, am.getHmap_str2Long());
333         assertEquals("The Hashtable (String,Long) ", s2l, am.getHt_str2Long());
334
335         assertEquals("The Properties ", p, am.getProp());
336     }
337     
338     
339     private void assertEquals(String JavaDoc msg, Map expected, Map found) {
340         if (expected == null) {
341             Assert.assertNull(msg + " is not null", found);
342             return;
343         }
344         Assert.assertNotNull(msg + " is null", found);
345         Iterator JavaDoc it = expected.entrySet().iterator();
346         while(it.hasNext()) {
347             Map.Entry me = (Map.Entry) it.next();
348             Object JavaDoc val = found.get(me.getKey());
349             Assert.assertNotNull(msg + ": the expected entry (" + me.getKey()
350                     + ", "+ me.getValue() + ") has not been found ("
351                     + found.keySet() + ")", val);
352         }
353         it = found.entrySet().iterator();
354         while(it.hasNext()) {
355             Map.Entry me = (Map.Entry) it.next();
356             Object JavaDoc val = expected.get(me.getKey());
357             Assert.assertNotNull(msg + ": the entry (" + me.getKey()
358                     + ", "+ me.getValue() + ") has not expected ("
359                     + expected.keySet() + ")", val);
360         }
361     }
362
363 }
364
Popular Tags