001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: * Authors: S.Chassande-Barrioz.
025: *
026: */package org.objectweb.speedo.runtime.collection;
027:
028: import org.objectweb.speedo.SpeedoTestHelper;
029: import org.objectweb.speedo.pobjects.collection.Employee;
030: import org.objectweb.speedo.pobjects.collection.Group;
031: import org.objectweb.speedo.pobjects.collection.User;
032: import org.objectweb.speedo.pobjects.collection.Ref2StringSet;
033: import org.objectweb.util.monolog.api.BasicLevel;
034:
035: import javax.jdo.PersistenceManager;
036: import javax.jdo.Query;
037: import javax.jdo.JDOHelper;
038:
039: import junit.framework.Assert;
040:
041: import java.util.Collection;
042: import java.util.Iterator;
043: import java.util.ArrayList;
044:
045: public class TestEmployee extends SpeedoTestHelper {
046:
047: public TestEmployee(String s) {
048: super (s);
049: }
050:
051: protected String getLoggerName() {
052: return LOG_NAME + ".rt.collection.TestEmployee";
053: }
054:
055: public void testCreation1() {
056: logger.log(BasicLevel.DEBUG, "Start testCreation1");
057: logger.log(BasicLevel.DEBUG, "Create Employee");
058: Employee e1 = new Employee("e1");
059: Employee e2 = new Employee("e2");
060: e1.setBoss(e2);
061:
062: PersistenceManager pm = pmf.getPersistenceManager();
063: pm.makePersistent(e1);
064: Object eId = pm.getObjectId(e1);
065: Assert.assertNotNull("null object identifier", eId);
066: pm.close();
067: e1 = null;
068: e2 = null;
069: pm = pmf.getPersistenceManager();
070: e1 = (Employee) pm.getObjectById(eId, true);
071: Assert.assertNotNull("null instance returned by getObjectById",
072: e1);
073: Assert.assertEquals("Bad employee name", "e1", e1.getName());
074: Assert.assertNotNull("null instance returned by getObjectById",
075: e1.getBoss());
076: Assert.assertEquals("Bad boss name", "e2", e1.getBoss()
077: .getName());
078: pm.currentTransaction().begin();
079: pm.deletePersistent(e1);
080: pm.deletePersistent(e1.getBoss());
081: pm.currentTransaction().commit();
082: pm.close();
083: }
084:
085: public void testCreation2() {
086: logger.log(BasicLevel.DEBUG, "Start testCreation2");
087: logger.log(BasicLevel.DEBUG, "Create Employee");
088: Employee e1 = new Employee("e1");
089: e1.setBoss(e1);
090:
091: PersistenceManager pm = pmf.getPersistenceManager();
092: pm.makePersistent(e1);
093: Object eId = pm.getObjectId(e1);
094: Assert.assertNotNull("null object identifier", eId);
095: pm.close();
096:
097: e1 = null;
098: pm = pmf.getPersistenceManager();
099: e1 = (Employee) pm.getObjectById(eId, true);
100: Assert.assertNotNull("null instance returned by getObjectById",
101: e1);
102: Assert.assertEquals("Bad employee name", "e1", e1.getName());
103: Assert.assertNotNull("null instance returned by getObjectById",
104: e1.getBoss());
105: Assert.assertEquals("Bad boss", e1, e1.getBoss());
106: pm.currentTransaction().begin();
107: pm.deletePersistent(e1);
108: pm.currentTransaction().commit();
109: pm.close();
110: }
111:
112: public void testCreation3() {
113: logger.log(BasicLevel.DEBUG, "Start testCreation3");
114: logger.log(BasicLevel.DEBUG, "Create Employee");
115: Employee e1 = new Employee("e1", null);
116: Employee e2 = new Employee("e2");
117:
118: e1.addFriend(e2);
119: Assert.assertEquals("Bad friends set size", e1.getFriendsCol()
120: .size(), 1);
121: Assert.assertTrue("Bad friends set content", e1.getFriendsCol()
122: .contains(e2));
123:
124: e1.addInt(1);
125: Assert.assertEquals("Bad ints set size",
126: e1.getIntsCol().size(), 1);
127: Assert.assertTrue("Bad ints set content", e1.getIntsCol()
128: .contains(new Integer(1)));
129:
130: PersistenceManager pm = pmf.getPersistenceManager();
131: pm.makePersistent(e1);
132: Object eId = pm.getObjectId(e1);
133: Assert.assertNotNull("null object identifier", eId);
134: pm.close();
135:
136: e1 = null;
137: pm = pmf.getPersistenceManager();
138: e1 = (Employee) pm.getObjectById(eId, true);
139: Assert.assertNotNull("null instance returned by getObjectById",
140: e1);
141: Assert.assertEquals("Bad employee name", "e1", e1.getName());
142:
143: Assert.assertNotNull(
144: "null collection returned by getObjectById", e1
145: .getFriendsCol());
146: Assert.assertEquals("Bad friends set size", e1.getFriendsCol()
147: .size(), 1);
148: Assert.assertTrue("Bad friends set content", e1.getFriendsCol()
149: .iterator().hasNext());
150: Assert.assertNotNull("Bad friends set content", e1
151: .getFriendsCol().iterator().next());
152: Assert.assertEquals("Bad friends set content", ((Employee) e1
153: .getFriendsCol().iterator().next()).getName(), "e2");
154:
155: Assert.assertNotNull(
156: "null collection returned by getObjectById", e1
157: .getIntsCol());
158: Assert.assertEquals("Bad ints set size",
159: e1.getIntsCol().size(), 1);
160: Assert.assertTrue("Bad ints set content", e1.getIntsCol()
161: .iterator().hasNext());
162: Assert.assertNotNull("Bad ints set content", e1.getIntsCol()
163: .iterator().next());
164: Assert.assertTrue("Bad ints set content", e1.getIntsCol()
165: .contains(new Integer(1)));
166: pm.currentTransaction().begin();
167: pm.deletePersistent(e2);
168: pm.deletePersistent(e1);
169: pm.currentTransaction().commit();
170: pm.close();
171: }
172:
173: public void testCreation4() {
174: logger.log(BasicLevel.DEBUG, "Start testCreation4");
175: logger.log(BasicLevel.DEBUG, "Create Employee");
176: Employee e1 = new Employee("e1", null);
177:
178: PersistenceManager pm = pmf.getPersistenceManager();
179: pm.makePersistent(e1);
180: Object eId = pm.getObjectId(e1);
181: Assert.assertNotNull("null object identifier", eId);
182:
183: Employee e2 = new Employee("e2");
184:
185: e1.addFriend(e2);
186: Assert.assertEquals("Bad friends set size", e1.getFriendsCol()
187: .size(), 1);
188: Assert.assertTrue("Bad friends set content", e1.getFriendsCol()
189: .contains(e2));
190:
191: e1.addInt(1);
192: Assert.assertEquals("Bad ints set size",
193: e1.getIntsCol().size(), 1);
194: Assert.assertTrue("Bad ints set content", e1.getIntsCol()
195: .contains(new Integer(1)));
196:
197: pm.close();
198:
199: e1 = null;
200: pm = pmf.getPersistenceManager();
201: e1 = (Employee) pm.getObjectById(eId, true);
202: Assert.assertNotNull("null instance returned by getObjectById",
203: e1);
204: Assert.assertEquals("Bad employee name", "e1", e1.getName());
205:
206: Assert.assertNotNull(
207: "null collection returned by getObjectById", e1
208: .getFriendsCol());
209: Assert.assertEquals("Bad friends set size", e1.getFriendsCol()
210: .size(), 1);
211: Assert.assertTrue("Bad friends set content", e1.getFriendsCol()
212: .iterator().hasNext());
213: Assert.assertNotNull("Bad friends set content", e1
214: .getFriendsCol().iterator().next());
215: Assert.assertEquals("Bad friends set content", ((Employee) e1
216: .getFriendsCol().iterator().next()).getName(), "e2");
217:
218: Assert.assertNotNull(
219: "null collection returned by getObjectById", e1
220: .getIntsCol());
221: Assert.assertEquals("Bad ints set size",
222: e1.getIntsCol().size(), 1);
223: Assert.assertTrue("Bad ints set content", e1.getIntsCol()
224: .iterator().hasNext());
225: Assert.assertNotNull("Bad ints set content", e1.getIntsCol()
226: .iterator().next());
227: Assert.assertTrue("Bad ints set content", e1.getIntsCol()
228: .contains(new Integer(1)));
229: Object o = e2.getName();
230: pm.currentTransaction().begin();
231: pm.deletePersistent(e1);
232: pm.deletePersistent(e2);
233: pm.currentTransaction().commit();
234: pm.close();
235: }
236:
237: public void testCollectionLoading() {
238: logger.log(BasicLevel.DEBUG, "Start testCollectionLoading");
239: PersistenceManager pm = pmf.getPersistenceManager();
240: Query q = pm.newQuery(Employee.class);
241: q.declareParameters("String en");
242: q.setFilter("(en == name)");
243: Collection c = (Collection) q.execute(POBuilder.EMPLOYEE_NAME);
244: Assert.assertNotNull(
245: "Collection returned by the query is null", c);
246: Iterator it = c.iterator();
247: Assert
248: .assertNotNull(
249: "Iterator over the collection returned by the query is null",
250: it);
251: if (!it.hasNext()) {
252: fail("Run the POBuilder test before this test in order to create objects");
253: }
254: Object o = it.next();
255: q.closeAll();
256:
257: Assert.assertNotNull("Null object returned by the iterator", o);
258: Assert.assertEquals("bad object type", Employee.class, o
259: .getClass());
260: Employee e = (Employee) o;
261: Collection elems = e.getFriendsCol();
262: Assert.assertNotNull("Null collection field", elems);
263: Iterator elemIt = elems.iterator();
264: Assert.assertNotNull("Null iterator over elements", elemIt);
265: ArrayList elemNames = new ArrayList();
266: while (elemIt.hasNext()) {
267: Object elem = elemIt.next();
268: Assert.assertNotNull("Null element", elem);
269: Assert.assertEquals("bad elem type", Employee.class, elem
270: .getClass());
271: elemNames.add(((Employee) elem).getName());
272: }
273: assertSameCollection("Bad element names: ", POBuilder
274: .getElementNames(), elemNames);
275: pm.close();
276: }
277:
278: public void testCheckSetCollection() {
279: logger.log(BasicLevel.DEBUG, "Start testCheckSetCollection");
280: PersistenceManager pm = pmf.getPersistenceManager();
281: Query q = pm.newQuery(Employee.class);
282: q.declareParameters("String en");
283: q.setFilter("(en == name)");
284: Collection c = (Collection) q.execute(POBuilder.EMPLOYEE_NAME2);
285: Assert.assertNotNull(
286: "Collection returned by the query is null", c);
287: Iterator it = c.iterator();
288: Assert
289: .assertNotNull(
290: "Iterator over the collection returned by the query is null",
291: it);
292: if (!it.hasNext()) {
293: fail("Run the POBuilder test before this test in order to create objects");
294: }
295: Object o = it.next();
296: q.closeAll();
297:
298: Assert.assertNotNull("Null object returned by the iterator", o);
299: Assert.assertEquals("bad object type", Employee.class, o
300: .getClass());
301: Employee e = (Employee) o;
302: Collection elems = e.getFriendsCol();
303: Assert.assertNotNull("Null collection field", elems);
304: Iterator elemIt = elems.iterator();
305: Assert.assertNotNull("Null iterator over elements", elemIt);
306: ArrayList elemNames = new ArrayList();
307: while (elemIt.hasNext()) {
308: Object elem = elemIt.next();
309: Assert.assertNotNull("Null element", elem);
310: Assert.assertEquals("bad elem type", Employee.class, elem
311: .getClass());
312: elemNames.add(((Employee) elem).getName());
313: }
314: assertSameCollection("Bad element names: ", POBuilder
315: .getElementNames2(), elemNames);
316: pm.close();
317: }
318:
319: public void testReachability() {
320: PersistenceManager pm = pmf.getPersistenceManager();
321: pm.currentTransaction().begin();
322:
323: final Group group = new Group("Group One");
324:
325: final User user = new User("Tom");
326: user.setE_mail("tom@example.com");
327: group.getUsers().add(user);
328: assertTrue(
329: "User already marked as persistent before the makePersitent action: ",
330: !JDOHelper.isPersistent(user));
331: assertTrue(
332: "Group already marked as persistent before the makePersitent action: ",
333: !JDOHelper.isPersistent(group));
334:
335: pm.makePersistent(group);
336:
337: assertTrue(
338: "Group is not marked as persistent after the makePersitent action: ",
339: JDOHelper.isPersistent(group));
340: assertTrue(
341: "User is marked as persistent after the makePersitent action (back hole)",
342: JDOHelper.isPersistent(user));
343: assertTrue(
344: "User is not marked as New after the makePersitent action: ",
345: JDOHelper.isNew(user));
346: assertTrue(
347: "User is not marked as Dirty after the makePersitent action: ",
348: JDOHelper.isDirty(user));
349: pm.currentTransaction().commit();
350:
351: pm.currentTransaction().begin();
352: pm.deletePersistent(user);
353: pm.deletePersistent(group);
354: pm.currentTransaction().commit();
355:
356: pm.close();
357:
358: }
359:
360: public void testRef2StringSet() {
361: PersistenceManager pm = pmf.getPersistenceManager();
362:
363: pm.currentTransaction().begin();
364: Ref2StringSet rss = new Ref2StringSet("testRef2StringSet");
365: rss.getStrings().add("str1");
366: rss.getStrings().add("str2");
367: rss.getStrings().add("str3");
368: assertTrue("Bad return value for a new value", !rss
369: .getStrings().add("str3"));
370: pm.makePersistent(rss);
371: pm.currentTransaction().commit();
372:
373: assertTrue("Bad return value for a new value", rss.getStrings()
374: .add("str4"));
375: assertTrue("Bad return value for an existing value", !rss
376: .getStrings().add("str3"));
377:
378: pm.currentTransaction().begin();
379: pm.deletePersistent(rss);
380: pm.currentTransaction().commit();
381:
382: pm.close();
383:
384: }
385:
386: public void testColModifNSet() {
387: Employee e1 = new Employee("e1");
388: Employee e2 = new Employee("e2");
389: Employee e3 = new Employee("e3");
390: PersistenceManager pm = pmf.getPersistenceManager();
391: pm.currentTransaction().begin();
392: pm.makePersistent(e1);
393: Object oid1 = pm.getObjectId(e1);
394: pm.makePersistent(e2);
395: Object oid2 = pm.getObjectId(e2);
396: pm.makePersistent(e3);
397: Object oid3 = pm.getObjectId(e3);
398: e1.addFriend(e2);
399: pm.currentTransaction().commit();
400:
401: pm.currentTransaction().begin();
402: Collection friends = e1.getFriendsCol();
403: friends.remove(e2);
404: friends.add(e3);
405: e1.setFriends(friends);
406: pm.currentTransaction().commit();
407:
408: e1 = null;
409: e2 = null;
410: e3 = null;
411: pm.evictAll();
412:
413: pm.currentTransaction().begin();
414: e1 = (Employee) pm.getObjectById(oid1, false);
415: e2 = (Employee) pm.getObjectById(oid2, false);
416: e3 = (Employee) pm.getObjectById(oid3, false);
417: int s = e1.getFriendsCol().size();
418: boolean containse2 = e1.getFriendsCol().contains(e2);
419: boolean containse3 = e1.getFriendsCol().contains(e3);
420: pm.deletePersistent(e1);
421: pm.deletePersistent(e2);
422: pm.deletePersistent(e3);
423: pm.currentTransaction().commit();
424: Assert.assertEquals("Bad collection size", 1, s);
425: Assert.assertTrue("E2 again in friends", !containse2);
426: Assert.assertTrue("E3 not in friends", containse3);
427: }
428:
429: public void testDeleteColElemWithoutRelationWithEvictAll() {
430: testDeleteColElemWithoutRelation(true);
431: }
432:
433: public void testDeleteColElemWithoutRelation() {
434: testDeleteColElemWithoutRelation(false);
435: }
436:
437: public void testDeleteColElemWithoutRelation(boolean withEvictAll) {
438: String gn = "group testDeleteColElemWithoutRelation"
439: + withEvictAll;
440: String un = "user testDeleteColElemWithoutRelation"
441: + withEvictAll;
442: Group group = new Group(gn);
443: User user = new User(un);
444: group.getUsers().add(user);
445: PersistenceManager pm = pmf.getPersistenceManager();
446: pm.currentTransaction().begin();
447: pm.makePersistent(group);
448: group = null;
449: user = null;
450: pm.currentTransaction().commit();
451: if (withEvictAll) {
452: pm.evictAll();
453: }
454: pm.currentTransaction().begin();
455: user = (User) pm.getObjectById(pm.newObjectIdInstance(
456: User.class, un), false);
457: pm.deletePersistent(user);
458: pm.currentTransaction().commit();
459:
460: pm.currentTransaction().begin();
461: group = (Group) pm.getObjectById(pm.newObjectIdInstance(
462: Group.class, gn), false);
463: ArrayList al = new ArrayList();
464: for (Iterator it = group.getUsers().iterator(); it.hasNext();) {
465: user = (User) it.next();
466: al.add(user.getName());
467: }
468: pm.deletePersistent(group);
469: pm.currentTransaction().commit();
470: pm.close();
471: assertTrue("Group not empty: " + al, al.isEmpty());
472: }
473: }
|