KickJava   Java API By Example, From Geeks To Geeks.

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


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.C;
22 import org.objectweb.speedo.pobjects.map.D;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Map JavaDoc;
26 import javax.jdo.PersistenceManager;
27
28 /**
29  *
30  * @author S.Chassande-Barrioz
31  */

32 public class TestKeyFieldMap extends SpeedoTestHelper {
33
34     public TestKeyFieldMap(String JavaDoc s) {
35         super(s);
36     }
37
38     protected String JavaDoc getLoggerName() {
39         return LOG_NAME + ".rt.map.TestKeyFieldMap";
40     }
41
42     public void test1() {
43         int MAP_SIZE = 10;
44         String JavaDoc F1_PREFIX = "f1_";
45         PersistenceManager pm = pmf.getPersistenceManager();
46
47         //Create instances
48
pm.currentTransaction().begin();
49         C c = new C(1);
50         ArrayList JavaDoc ds = new ArrayList JavaDoc(MAP_SIZE * 2);
51         for(int i=0; i<MAP_SIZE; i++) {
52             String JavaDoc f1 = F1_PREFIX + i;
53             ds.add(new D(i, f1));
54         }
55         pm.makePersistent(c);
56         pm.makePersistentAll(ds);
57         pm.currentTransaction().commit();
58
59         //Add into the map
60
pm.currentTransaction().begin();
61         Map JavaDoc m = c.getDkey2d();
62         for(int i=0; i<MAP_SIZE; i++) {
63             D d = (D) ds.get(i);
64             m.put(d.getF1(), d);
65         }
66         c = null;
67         ds = null;
68         pm.currentTransaction().commit();
69         finishTest(pm, MAP_SIZE, F1_PREFIX);
70     }
71
72     public void test2() {
73         int MAP_SIZE = 10;
74         String JavaDoc F1_PREFIX = "f1_";
75         PersistenceManager pm = pmf.getPersistenceManager();
76         pm.currentTransaction().begin();
77         C c = new C(1);
78         Map JavaDoc m = c.getDkey2d();
79         for(int i=0; i<MAP_SIZE; i++) {
80             String JavaDoc f1 = "f1_" + i;
81             D d = new D(i, f1);
82             m.put(f1, d);
83         }
84         pm.makePersistent(c);
85         pm.currentTransaction().commit();
86         finishTest(pm, MAP_SIZE, F1_PREFIX);
87     }
88
89     public void test3() {
90         int MAP_SIZE = 10;
91         String JavaDoc F1_PREFIX = "f1_";
92         PersistenceManager pm = pmf.getPersistenceManager();
93         pm.currentTransaction().begin();
94         C c = new C(1);
95         for(int i=0; i<MAP_SIZE; i++) {
96             String JavaDoc f1 = "f1_" + i;
97             D d = new D(i, f1);
98             d.setMyc(c);
99             pm.makePersistent(d);
100         }
101         pm.currentTransaction().commit();
102         finishTest(pm, MAP_SIZE, F1_PREFIX);
103     }
104
105     private void finishTest(PersistenceManager pm,
106                             final int MAP_SIZE,
107                             final String JavaDoc F1_PREFIX) {
108         pm.currentTransaction().begin();
109         long id = MAP_SIZE/2;
110         String JavaDoc f1 = F1_PREFIX + id;
111         C c = (C) pm.getObjectById(pm.newObjectIdInstance(C.class, "1"), false);
112         Object JavaDoc d = c.getDkey2d().get(f1);
113         assertNotNull("No Object associated to the key '" + f1 + "'", d);
114         assertTrue("Object associated to the key '" + f1 +"'", d instanceof D);
115         assertEquals("Bad D identifier ", id, ((D) d).getMyid());
116         assertNotNull("Bad D identifier ", ((D) d).getMyc());
117         assertEquals("Bad C identifier ", 1, ((D) d).getMyc().getMyid());
118         c = ((D) d).getMyc();
119         ((D) d).setMyc(null);
120         assertNull("The map contains again the D instance", c.getDkey2d().get(f1));
121         pm.currentTransaction().commit();
122
123         //Delete
124
pm.currentTransaction().begin();
125         pm.deletePersistent(d);
126         pm.deletePersistentAll(c.getDkey2d().values());
127         pm.deletePersistent(c);
128         pm.currentTransaction().commit();
129         pm.close();
130     }
131 }
132
Popular Tags