KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > collection > TestEmployee


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 package org.objectweb.speedo.runtime.collection;
28
29 import org.objectweb.speedo.SpeedoTestHelper;
30 import org.objectweb.speedo.pobjects.collection.Employee;
31 import org.objectweb.speedo.pobjects.collection.Group;
32 import org.objectweb.speedo.pobjects.collection.User;
33 import org.objectweb.speedo.pobjects.collection.Ref2StringSet;
34 import org.objectweb.util.monolog.api.BasicLevel;
35
36 import javax.jdo.PersistenceManager;
37 import javax.jdo.Query;
38 import javax.jdo.JDOHelper;
39
40 import junit.framework.Assert;
41
42 import java.util.Collection JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.ArrayList JavaDoc;
45
46 public class TestEmployee extends SpeedoTestHelper {
47
48     public TestEmployee(String JavaDoc s) {
49         super(s);
50     }
51
52     protected String JavaDoc getLoggerName() {
53         return LOG_NAME + ".rt.collection.TestEmployee";
54     }
55
56     public void testCreation1() {
57         logger.log(BasicLevel.DEBUG, "Start testCreation1");
58         logger.log(BasicLevel.DEBUG, "Create Employee");
59         Employee e1 = new Employee("e1");
60         Employee e2 = new Employee("e2");
61         e1.setBoss(e2);
62
63         PersistenceManager pm = pmf.getPersistenceManager();
64         pm.makePersistent(e1);
65         Object JavaDoc eId = pm.getObjectId(e1);
66         Assert.assertNotNull("null object identifier", eId);
67         pm.close();
68         e1 = null;
69         e2 = null;
70         pm = pmf.getPersistenceManager();
71         e1 = (Employee) pm.getObjectById(eId, true);
72         Assert.assertNotNull("null instance returned by getObjectById", e1);
73         Assert.assertEquals("Bad employee name", "e1", e1.getName());
74         Assert.assertNotNull("null instance returned by getObjectById", e1.getBoss());
75         Assert.assertEquals("Bad boss name", "e2", e1.getBoss().getName());
76         pm.currentTransaction().begin();
77         pm.deletePersistent(e1);
78         pm.deletePersistent(e1.getBoss());
79         pm.currentTransaction().commit();
80         pm.close();
81     }
82
83     public void testCreation2() {
84         logger.log(BasicLevel.DEBUG, "Start testCreation2");
85         logger.log(BasicLevel.DEBUG, "Create Employee");
86         Employee e1 = new Employee("e1");
87         e1.setBoss(e1);
88
89         PersistenceManager pm = pmf.getPersistenceManager();
90         pm.makePersistent(e1);
91         Object JavaDoc eId = pm.getObjectId(e1);
92         Assert.assertNotNull("null object identifier", eId);
93         pm.close();
94
95         e1 = null;
96         pm = pmf.getPersistenceManager();
97         e1 = (Employee) pm.getObjectById(eId, true);
98         Assert.assertNotNull("null instance returned by getObjectById", e1);
99         Assert.assertEquals("Bad employee name", "e1", e1.getName());
100         Assert.assertNotNull("null instance returned by getObjectById", e1.getBoss());
101         Assert.assertEquals("Bad boss", e1, e1.getBoss());
102         pm.currentTransaction().begin();
103         pm.deletePersistent(e1);
104         pm.currentTransaction().commit();
105         pm.close();
106     }
107
108     public void testCreation3() {
109         logger.log(BasicLevel.DEBUG, "Start testCreation3");
110         logger.log(BasicLevel.DEBUG, "Create Employee");
111         Employee e1 = new Employee("e1", null);
112         Employee e2 = new Employee("e2");
113
114         e1.addFriend(e2);
115         Assert.assertEquals("Bad friends set size", e1.getFriendsCol().size(), 1);
116         Assert.assertTrue("Bad friends set content", e1.getFriendsCol().contains(e2));
117
118         e1.addInt(1);
119         Assert.assertEquals("Bad ints set size", e1.getIntsCol().size(), 1);
120         Assert.assertTrue("Bad ints set content", e1.getIntsCol().contains(new Integer JavaDoc(1)));
121
122         PersistenceManager pm = pmf.getPersistenceManager();
123         pm.makePersistent(e1);
124         Object JavaDoc eId = pm.getObjectId(e1);
125         Assert.assertNotNull("null object identifier", eId);
126         pm.close();
127
128         e1 = null;
129         pm = pmf.getPersistenceManager();
130         e1 = (Employee) pm.getObjectById(eId, true);
131         Assert.assertNotNull("null instance returned by getObjectById", e1);
132         Assert.assertEquals("Bad employee name", "e1", e1.getName());
133
134         Assert.assertNotNull("null collection returned by getObjectById", e1.getFriendsCol());
135         Assert.assertEquals("Bad friends set size", e1.getFriendsCol().size(), 1);
136         Assert.assertTrue("Bad friends set content", e1.getFriendsCol().iterator().hasNext());
137         Assert.assertNotNull("Bad friends set content", e1.getFriendsCol().iterator().next());
138         Assert.assertEquals("Bad friends set content", ((Employee) e1.getFriendsCol().iterator().next()).getName(), "e2");
139
140         Assert.assertNotNull("null collection returned by getObjectById", e1.getIntsCol());
141         Assert.assertEquals("Bad ints set size", e1.getIntsCol().size(), 1);
142         Assert.assertTrue("Bad ints set content", e1.getIntsCol().iterator().hasNext());
143         Assert.assertNotNull("Bad ints set content", e1.getIntsCol().iterator().next());
144         Assert.assertTrue("Bad ints set content", e1.getIntsCol().contains(new Integer JavaDoc(1)));
145         pm.currentTransaction().begin();
146         pm.deletePersistent(e2);
147         pm.deletePersistent(e1);
148         pm.currentTransaction().commit();
149         pm.close();
150     }
151
152     public void testCreation4() {
153         logger.log(BasicLevel.DEBUG, "Start testCreation4");
154         logger.log(BasicLevel.DEBUG, "Create Employee");
155         Employee e1 = new Employee("e1", null);
156
157         PersistenceManager pm = pmf.getPersistenceManager();
158         pm.makePersistent(e1);
159         Object JavaDoc eId = pm.getObjectId(e1);
160         Assert.assertNotNull("null object identifier", eId);
161
162         Employee e2 = new Employee("e2");
163
164         e1.addFriend(e2);
165         Assert.assertEquals("Bad friends set size", e1.getFriendsCol().size(), 1);
166         Assert.assertTrue("Bad friends set content", e1.getFriendsCol().contains(e2));
167
168         e1.addInt(1);
169         Assert.assertEquals("Bad ints set size", e1.getIntsCol().size(), 1);
170         Assert.assertTrue("Bad ints set content", e1.getIntsCol().contains(new Integer JavaDoc(1)));
171
172         pm.close();
173
174         e1 = null;
175         pm = pmf.getPersistenceManager();
176         e1 = (Employee) pm.getObjectById(eId, true);
177         Assert.assertNotNull("null instance returned by getObjectById", e1);
178         Assert.assertEquals("Bad employee name", "e1", e1.getName());
179
180         Assert.assertNotNull("null collection returned by getObjectById", e1.getFriendsCol());
181         Assert.assertEquals("Bad friends set size", e1.getFriendsCol().size(), 1);
182         Assert.assertTrue("Bad friends set content", e1.getFriendsCol().iterator().hasNext());
183         Assert.assertNotNull("Bad friends set content", e1.getFriendsCol().iterator().next());
184         Assert.assertEquals("Bad friends set content", ((Employee) e1.getFriendsCol().iterator().next()).getName(), "e2");
185
186         Assert.assertNotNull("null collection returned by getObjectById", e1.getIntsCol());
187         Assert.assertEquals("Bad ints set size", e1.getIntsCol().size(), 1);
188         Assert.assertTrue("Bad ints set content", e1.getIntsCol().iterator().hasNext());
189         Assert.assertNotNull("Bad ints set content", e1.getIntsCol().iterator().next());
190         Assert.assertTrue("Bad ints set content", e1.getIntsCol().contains(new Integer JavaDoc(1)));
191         Object JavaDoc o = e2.getName();
192         pm.currentTransaction().begin();
193         pm.deletePersistent(e1);
194         pm.deletePersistent(e2);
195         pm.currentTransaction().commit();
196         pm.close();
197     }
198
199     public void testCollectionLoading() {
200         logger.log(BasicLevel.DEBUG, "Start testCollectionLoading");
201         PersistenceManager pm = pmf.getPersistenceManager();
202         Query q = pm.newQuery(Employee.class);
203         q.declareParameters("String en");
204         q.setFilter("(en == name)");
205         Collection JavaDoc c = (Collection JavaDoc) q.execute(POBuilder.EMPLOYEE_NAME);
206         Assert.assertNotNull("Collection returned by the query is null", c);
207         Iterator JavaDoc it = c.iterator();
208         Assert.assertNotNull("Iterator over the collection returned by the query is null", it);
209         if (!it.hasNext()) {
210             fail("Run the POBuilder test before this test in order to create objects");
211         }
212         Object JavaDoc o = it.next();
213         q.closeAll();
214
215         Assert.assertNotNull("Null object returned by the iterator", o);
216         Assert.assertEquals("bad object type", Employee.class, o.getClass());
217         Employee e = (Employee) o;
218         Collection JavaDoc elems = e.getFriendsCol();
219         Assert.assertNotNull("Null collection field", elems);
220         Iterator JavaDoc elemIt = elems.iterator();
221         Assert.assertNotNull("Null iterator over elements", elemIt);
222         ArrayList JavaDoc elemNames = new ArrayList JavaDoc();
223         while(elemIt.hasNext()) {
224             Object JavaDoc elem = elemIt.next();
225             Assert.assertNotNull("Null element", elem);
226             Assert.assertEquals("bad elem type", Employee.class, elem.getClass());
227             elemNames.add(((Employee) elem).getName());
228         }
229         assertSameCollection("Bad element names: ", POBuilder.getElementNames(), elemNames);
230         pm.close();
231     }
232
233     public void testCheckSetCollection() {
234         logger.log(BasicLevel.DEBUG, "Start testCheckSetCollection");
235         PersistenceManager pm = pmf.getPersistenceManager();
236         Query q = pm.newQuery(Employee.class);
237         q.declareParameters("String en");
238         q.setFilter("(en == name)");
239         Collection JavaDoc c = (Collection JavaDoc) q.execute(POBuilder.EMPLOYEE_NAME2);
240         Assert.assertNotNull("Collection returned by the query is null", c);
241         Iterator JavaDoc it = c.iterator();
242         Assert.assertNotNull("Iterator over the collection returned by the query is null", it);
243         if (!it.hasNext()) {
244             fail("Run the POBuilder test before this test in order to create objects");
245         }
246         Object JavaDoc o = it.next();
247         q.closeAll();
248
249         Assert.assertNotNull("Null object returned by the iterator", o);
250         Assert.assertEquals("bad object type", Employee.class, o.getClass());
251         Employee e = (Employee) o;
252         Collection JavaDoc elems = e.getFriendsCol();
253         Assert.assertNotNull("Null collection field", elems);
254         Iterator JavaDoc elemIt = elems.iterator();
255         Assert.assertNotNull("Null iterator over elements", elemIt);
256         ArrayList JavaDoc elemNames = new ArrayList JavaDoc();
257         while(elemIt.hasNext()) {
258             Object JavaDoc elem = elemIt.next();
259             Assert.assertNotNull("Null element", elem);
260             Assert.assertEquals("bad elem type", Employee.class, elem.getClass());
261             elemNames.add(((Employee) elem).getName());
262         }
263         assertSameCollection("Bad element names: ", POBuilder.getElementNames2(), elemNames);
264         pm.close();
265     }
266
267     public void testReachability() {
268         PersistenceManager pm = pmf.getPersistenceManager();
269         pm.currentTransaction().begin();
270
271         final Group group = new Group("Group One");
272
273         final User user = new User("Tom");
274         user.setE_mail("tom@example.com");
275         group.getUsers().add(user);
276         assertTrue("User already marked as persistent before the makePersitent action: ",
277             !JDOHelper.isPersistent(user));
278         assertTrue("Group already marked as persistent before the makePersitent action: ",
279             !JDOHelper.isPersistent(group));
280
281         pm.makePersistent(group);
282
283         assertTrue("Group is not marked as persistent after the makePersitent action: ",
284             JDOHelper.isPersistent(group));
285         assertTrue("User is marked as persistent after the makePersitent action (back hole)",
286             JDOHelper.isPersistent(user));
287         assertTrue("User is not marked as New after the makePersitent action: ",
288             JDOHelper.isNew(user));
289         assertTrue("User is not marked as Dirty after the makePersitent action: ",
290             JDOHelper.isDirty(user));
291         pm.currentTransaction().commit();
292
293         pm.currentTransaction().begin();
294         pm.deletePersistent(user);
295         pm.deletePersistent(group);
296         pm.currentTransaction().commit();
297
298         pm.close();
299
300     }
301
302     public void testRef2StringSet() {
303         PersistenceManager pm = pmf.getPersistenceManager();
304
305         pm.currentTransaction().begin();
306         Ref2StringSet rss = new Ref2StringSet("testRef2StringSet");
307         rss.getStrings().add("str1");
308         rss.getStrings().add("str2");
309         rss.getStrings().add("str3");
310         assertTrue("Bad return value for a new value",
311             !rss.getStrings().add("str3"));
312         pm.makePersistent(rss);
313         pm.currentTransaction().commit();
314
315         assertTrue("Bad return value for a new value",
316             rss.getStrings().add("str4"));
317         assertTrue("Bad return value for an existing value",
318             !rss.getStrings().add("str3"));
319
320         pm.currentTransaction().begin();
321         pm.deletePersistent(rss);
322         pm.currentTransaction().commit();
323
324         pm.close();
325
326     }
327     
328     public void testColModifNSet() {
329         Employee e1 = new Employee("e1");
330         Employee e2 = new Employee("e2");
331         Employee e3 = new Employee("e3");
332         PersistenceManager pm = pmf.getPersistenceManager();
333         pm.currentTransaction().begin();
334         pm.makePersistent(e1);
335         Object JavaDoc oid1 = pm.getObjectId(e1);
336         pm.makePersistent(e2);
337         Object JavaDoc oid2 = pm.getObjectId(e2);
338         pm.makePersistent(e3);
339         Object JavaDoc oid3 = pm.getObjectId(e3);
340         e1.addFriend(e2);
341         pm.currentTransaction().commit();
342         
343         pm.currentTransaction().begin();
344         Collection JavaDoc friends = e1.getFriendsCol();
345         friends.remove(e2);
346         friends.add(e3);
347         e1.setFriends(friends);
348         pm.currentTransaction().commit();
349
350         e1 = null;
351         e2 = null;
352         e3 = null;
353         pm.evictAll();
354         
355         pm.currentTransaction().begin();
356         e1 = (Employee) pm.getObjectById(oid1, false);
357         e2 = (Employee) pm.getObjectById(oid2, false);
358         e3 = (Employee) pm.getObjectById(oid3, false);
359         int s = e1.getFriendsCol().size();
360         boolean containse2 = e1.getFriendsCol().contains(e2);
361         boolean containse3 = e1.getFriendsCol().contains(e3);
362         pm.deletePersistent(e1);
363         pm.deletePersistent(e2);
364         pm.deletePersistent(e3);
365         pm.currentTransaction().commit();
366         Assert.assertEquals("Bad collection size", 1, s);
367         Assert.assertTrue("E2 again in friends", !containse2);
368         Assert.assertTrue("E3 not in friends", containse3);
369     }
370     public void testDeleteColElemWithoutRelationWithEvictAll() {
371         testDeleteColElemWithoutRelation(true);
372     }
373     public void testDeleteColElemWithoutRelation() {
374         testDeleteColElemWithoutRelation(false);
375     }
376     public void testDeleteColElemWithoutRelation(boolean withEvictAll) {
377         String JavaDoc gn = "group testDeleteColElemWithoutRelation" + withEvictAll;
378         String JavaDoc un = "user testDeleteColElemWithoutRelation" + withEvictAll;
379         Group group = new Group(gn);
380         User user = new User(un);
381         group.getUsers().add(user);
382         PersistenceManager pm = pmf.getPersistenceManager();
383         pm.currentTransaction().begin();
384         pm.makePersistent(group);
385         group = null;
386         user = null;
387         pm.currentTransaction().commit();
388         if (withEvictAll) {
389             pm.evictAll();
390         }
391         pm.currentTransaction().begin();
392         user = (User) pm.getObjectById(
393                 pm.newObjectIdInstance(User.class, un), false);
394         pm.deletePersistent(user);
395         pm.currentTransaction().commit();
396         
397         pm.currentTransaction().begin();
398         group = (Group) pm.getObjectById(
399                 pm.newObjectIdInstance(Group.class, gn), false);
400         ArrayList JavaDoc al = new ArrayList JavaDoc();
401         for(Iterator JavaDoc it = group.getUsers().iterator(); it.hasNext();) {
402             user = (User) it.next();
403             al.add(user.getName());
404         }
405         pm.deletePersistent(group);
406         pm.currentTransaction().commit();
407         pm.close();
408         assertTrue("Group not empty: " + al, al.isEmpty());
409     }
410 }
411
Popular Tags