KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27
28 package org.objectweb.speedo.runtime.map;
29
30 import org.objectweb.speedo.SpeedoTestHelper;
31 import org.objectweb.speedo.pobjects.map.Registry;
32 import org.objectweb.speedo.pobjects.map.A;
33 import org.objectweb.speedo.pobjects.map.B;
34
35 import javax.jdo.PersistenceManager;
36 import javax.jdo.JDOException;
37
38 import junit.framework.Assert;
39
40 import java.util.Collection JavaDoc;
41
42 public class TestMap extends SpeedoTestHelper {
43
44     public TestMap(String JavaDoc s) {
45         super(s);
46     }
47
48     protected String JavaDoc getLoggerName() {
49         return LOG_NAME + ".rt.map.TestMap";
50     }
51
52     public void testCreation1() {
53         Registry r1 = new Registry("r1");
54         Registry r2 = new Registry("r2");
55         Registry r3 = new Registry("r3");
56         r1.bind("a", r2);
57         r1.bind("b", r3);
58
59         PersistenceManager pm = pmf.getPersistenceManager();
60         pm.makePersistent(r1);
61         Object JavaDoc oid1 = pm.getObjectId(r1);
62         Assert.assertNotNull("null object identifier of r1", r1);
63         pm.close();
64
65         r1 = null;
66         r2 = null;
67         r3 = null;
68         pm = pmf.getPersistenceManager();
69         pm.evictAll();
70         r1 = (Registry) pm.getObjectById(oid1, true);
71         Assert.assertNotNull("null instance returned by getObjectById(oid1)", r1);
72         r2 = (Registry) r1.lookup("a");
73         Assert.assertNotNull("null instance returned by r1.lookup(a)", r2);
74         r3 = (Registry) r1.lookup("b");
75         Assert.assertNotNull("null instance returned by r1.lookup(b)", r3);
76         pm.currentTransaction().begin();
77         pm.deletePersistent(r1);
78         pm.deletePersistent(r2);
79         pm.deletePersistent(r3);
80         pm.currentTransaction().commit();
81         pm.close();
82     }
83
84     public void testA() {
85
86         //Make persitent the a instance
87
PersistenceManager pm = pmf.getPersistenceManager();
88         A a = new A(0);
89         pm.makePersistent(a);
90         pm.close();
91
92         //Add B instances into the map
93
final int NB_B = 10;
94         final String JavaDoc IDX_PREFIX = "index2B";
95         final String JavaDoc F1_PREFIX = "toto";
96         pm = pmf.getPersistenceManager();
97         for(int i=0; i<NB_B; i++) {
98             a.add(IDX_PREFIX + i, new B(i, F1_PREFIX + i));
99         }
100         pm.close();
101
102         //Clear the cache
103
a = null; //permit the GC to garbage instances
104
pm = pmf.getPersistenceManager();
105         pm.evictAll();
106         pm.close();
107
108         //Check the instance existing
109
pm = pmf.getPersistenceManager();
110         try {
111             a = (A) pm.getObjectById(pm.newObjectIdInstance(A.class, "" + 0), false);
112         } catch (JDOException e) {
113             fail("The A instance is not found on the data support");
114         }
115         Collection JavaDoc names = a.names();
116         assertNotNull("name collection is null", names);
117         assertEquals("Bad name collection size", NB_B, names.size());
118         B[] bs = new B[NB_B];
119         for(int i=0; i<NB_B; i++) {
120             String JavaDoc idx = IDX_PREFIX + i;
121             assertTrue("The name collection does not contain the index: " + idx,
122                     names.contains(idx));
123             bs[i] = a.getB(idx);
124             assertNotNull("Null element assocaited to the index " + idx, bs[i]);
125             assertEquals("Bad F1 value for the element assocaited to the index "
126                     + idx, F1_PREFIX + i, bs[i].getF1());
127         }
128         a.clear();
129         pm.currentTransaction().begin();
130         pm.deletePersistentAll(bs);
131         pm.deletePersistent(a);
132         pm.currentTransaction().commit();
133         pm.close();
134     }
135 }
136
Popular Tags