001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.runtime.userid;
018:
019: import junit.framework.Assert;
020: import org.objectweb.speedo.SpeedoTestHelper;
021: import org.objectweb.speedo.pobjects.userid.AutoIncFieldId;
022: import org.objectweb.speedo.pobjects.userid.AutoIncId;
023: import org.objectweb.speedo.pobjects.userid.BasicB;
024: import org.objectweb.speedo.pobjects.userid.IntUserId;
025: import org.objectweb.speedo.pobjects.userid.Ref2AutoIncFieldId;
026: import org.objectweb.speedo.pobjects.userid.StringLongId;
027: import org.objectweb.speedo.pobjects.userid.StringUserId;
028: import org.objectweb.speedo.pobjects.userid.InnerUserId;
029: import org.objectweb.speedo.pobjects.userid.Ref2BasicB;
030: import org.objectweb.util.monolog.api.BasicLevel;
031:
032: import javax.jdo.JDOException;
033: import javax.jdo.JDOHelper;
034: import javax.jdo.PersistenceManager;
035: import java.io.File;
036: import java.io.FileInputStream;
037: import java.io.FileOutputStream;
038: import java.io.ObjectInputStream;
039: import java.io.ObjectOutputStream;
040: import javax.jdo.Query;
041: import java.util.ArrayList;
042: import java.util.Collection;
043: import java.util.Iterator;
044:
045: /**
046: *
047: * @author S.Chassande-Barrioz
048: */
049: public class TestUserId extends SpeedoTestHelper {
050:
051: private final static String OID_FILE_NAME = "TestUserId_test1.id";
052:
053: public TestUserId(String s) {
054: super (s);
055: }
056:
057: protected String getLoggerName() {
058: return LOG_NAME + "rt.userid";
059: }
060:
061: public void testGetUnexistingObject() {
062: PersistenceManager pm = pmf.getPersistenceManager();
063: try {
064: pm.getObjectById(new StringLongId("azerty", 217), false);
065: fail("Object found !");
066: } catch (JDOException e) {
067: //OK
068: } catch (Exception e) {
069: logger.log(BasicLevel.ERROR, e.getMessage(), e);
070: fail("Object found !");
071: } finally {
072: pm.close();
073: }
074: }
075:
076: public void test1MakePersistent() {
077: try {
078: PersistenceManager pm = pmf.getPersistenceManager();
079: BasicB bb = new BasicB("azerty", 217, "blabla", 456);
080: pm.makePersistent(bb);
081: StringLongId id = (StringLongId) pm.getObjectId(bb);
082: logger.log(BasicLevel.DEBUG, "UserId=" + id);
083: pm.close();
084: Assert.assertNotNull("Null Object id", id);
085: ObjectOutputStream os = new ObjectOutputStream(
086: new FileOutputStream(OID_FILE_NAME));
087: os.writeObject(id);
088: os.close();
089: } catch (Exception e) {
090: logger.log(BasicLevel.ERROR, "Exception", e);
091: fail(e.getMessage());
092: }
093: }
094:
095: public void test2LoadNDelete() {
096: try {
097: PersistenceManager pm = pmf.getPersistenceManager();
098: ObjectInputStream is = new ObjectInputStream(
099: new FileInputStream(OID_FILE_NAME));
100: StringLongId id = (StringLongId) is.readObject();
101: is.close();
102: new File(OID_FILE_NAME).delete();
103: BasicB bb = (BasicB) pm.getObjectById(id, true);
104: Assert.assertNotNull("Null Object", bb);
105: pm.currentTransaction().begin();
106: pm.deletePersistent(bb);
107: pm.currentTransaction().commit();
108: pm.close();
109: } catch (Exception e) {
110: logger.log(BasicLevel.ERROR, "Exception", e);
111: fail(e.getMessage());
112: }
113: }
114:
115: public void testUserId1() {
116: try {
117: String id1 = "wxcvb";
118: int id2 = 3;
119: String f1 = "sdgh";
120: int f2 = 68746;
121:
122: PersistenceManager pm = pmf.getPersistenceManager();
123: BasicB bb = new BasicB(id1, id2, f1, f2);
124: pm.makePersistent(bb);
125: StringLongId id = (StringLongId) pm.getObjectId(bb);
126: logger.log(BasicLevel.DEBUG, "UserId=" + id);
127: Assert.assertNotNull("identifier is null", id);
128: String strid = id.toString();
129: bb = null;
130: pm.close();
131:
132: pm = pmf.getPersistenceManager();
133: Object o = pm.newObjectIdInstance(BasicB.class, strid);
134: bb = (BasicB) pm.getObjectById(o, false);
135: Assert.assertNotNull("returned object is null", bb);
136: Assert.assertEquals("Bad field 'id1' value", id1, bb
137: .getId1());
138: Assert.assertEquals("Bad field 'di2' value", id2, bb
139: .getId2());
140: Assert
141: .assertEquals("Bad field 'f1' value", f1, bb
142: .readF1());
143: Assert
144: .assertEquals("Bad field 'f1' value", f2, bb
145: .readF2());
146:
147: Query q = pm.newQuery(BasicB.class);
148: Iterator it = ((Collection) q.execute()).iterator();
149: while (it.hasNext()) {
150: BasicB _bb = (BasicB) it.next();
151: StringLongId oid = (StringLongId) pm.getObjectId(_bb);
152: assertTrue(_bb == pm.getObjectById(oid, false));
153: }
154: q.closeAll();
155:
156: pm.currentTransaction().begin();
157: pm.deletePersistent(bb);
158: pm.currentTransaction().commit();
159: pm.close();
160: } catch (Exception e) {
161: logger.log(BasicLevel.ERROR, "Exception", e);
162: fail(e.getMessage());
163: }
164: }
165:
166: public void testUserId2() {
167: try {
168: String id1 = "querty";
169: int id2 = 12000;
170: String f1 = "blabla";
171: int f2 = 456;
172:
173: PersistenceManager pm = pmf.getPersistenceManager();
174: BasicB bb = new BasicB(id1, id2, f1, f2);
175: pm.makePersistent(bb);
176: StringLongId id = (StringLongId) pm.getObjectId(bb);
177: logger.log(BasicLevel.DEBUG, "UserId=" + id);
178: Assert.assertNotNull("identifier is null", id);
179: bb = null;
180: pm.close();
181:
182: pm = pmf.getPersistenceManager();
183: id = new StringLongId(id1, id2);
184: bb = (BasicB) pm.getObjectById(id, false);
185: Assert.assertNotNull("returned object is null", bb);
186: Assert.assertEquals("Bad field 'id1' value", id1, bb
187: .getId1());
188: Assert.assertEquals("Bad field 'di2' value", id2, bb
189: .getId2());
190: Assert
191: .assertEquals("Bad field 'f1' value", f1, bb
192: .readF1());
193: Assert
194: .assertEquals("Bad field 'f1' value", f2, bb
195: .readF2());
196: pm.currentTransaction().begin();
197: pm.deletePersistent(bb);
198: pm.currentTransaction().commit();
199: pm.close();
200: } catch (Exception e) {
201: logger.log(BasicLevel.ERROR, "Exception", e);
202: fail(e.getMessage());
203: }
204: }
205:
206: public void testInnerUserId() {
207: try {
208: String f1 = "testInnerUserId";
209: int f2 = 12;
210: InnerUserId.Oid iuio = new InnerUserId.Oid(0, 0);
211: PersistenceManager pm = pmf.getPersistenceManager();
212: InnerUserId iui = new InnerUserId(f1, f2, iuio);
213: pm.makePersistent(iui);
214: InnerUserId.Oid iuio2 = (InnerUserId.Oid) pm
215: .getObjectId(iui);
216: logger.log(BasicLevel.DEBUG, "UserId=" + iuio2);
217: Assert.assertNotNull("identifier is null", iuio2);
218: iui = null;
219: pm.close();
220:
221: pm = pmf.getPersistenceManager();
222: iuio2 = new InnerUserId.Oid(0, 0);
223: iui = (InnerUserId) pm.getObjectById(iuio2, false);
224: Assert.assertNotNull("returned object is null", iui);
225: Assert.assertEquals("Bad field 'oid1' value", iuio.oid1,
226: iui.getOid1());
227: Assert.assertEquals("Bad field 'oid2' value", iuio.oid2,
228: iui.getOid2());
229: Assert
230: .assertEquals("Bad field 'f1' value", f1, iui
231: .getF1());
232: Assert
233: .assertEquals("Bad field 'f2' value", f2, iui
234: .getF2());
235: pm.currentTransaction().begin();
236: pm.deletePersistent(iui);
237: pm.currentTransaction().commit();
238: pm.close();
239: } catch (Exception e) {
240: logger.log(BasicLevel.ERROR, "Exception", e);
241: fail(e.getMessage());
242: }
243: }
244:
245: public void testStringId() {
246: try {
247: String name = "testStringId1";
248: int f1 = 12;
249: Object id;
250: String strid;
251: PersistenceManager pm = pmf.getPersistenceManager();
252: StringUserId sui = new StringUserId(name, f1);
253: pm.makePersistent(sui);
254: id = pm.getObjectId(sui);
255: Assert.assertNotNull("identifier is null", id);
256: strid = id.toString();
257: id = null;
258: pm.close();
259: sui = null; // garbage object
260: pm = pmf.getPersistenceManager();
261: id = pm.newObjectIdInstance(StringUserId.class, strid);
262: sui = (StringUserId) pm.getObjectById(id, false);
263: Assert.assertNotNull("returned object is null", sui);
264: Assert.assertEquals("Bad field 'name' value", name, sui
265: .getName());
266: Assert
267: .assertEquals("Bad field 'f1' value", f1, sui
268: .getF1());
269: pm.currentTransaction().begin();
270: pm.deletePersistent(sui);
271: pm.currentTransaction().commit();
272: pm.close();
273: } catch (Exception e) {
274: logger.log(BasicLevel.ERROR, "Exception", e);
275: fail(e.getMessage());
276: }
277: }
278:
279: public void testStringId2() {
280: try {
281: String name = "testStringId2";
282: int f1 = 12;
283: Object id;
284: PersistenceManager pm = pmf.getPersistenceManager();
285: StringUserId sui = new StringUserId(name, f1);
286: pm.makePersistent(sui);
287: id = pm.getObjectId(sui);
288: Assert.assertNotNull("identifier is null", id);
289: id = null;
290: pm.close();
291: sui = null; // garbage object
292: pm = pmf.getPersistenceManager();
293: id = pm.newObjectIdInstance(StringUserId.class, name);
294: sui = (StringUserId) pm.getObjectById(id, false);
295: Assert.assertNotNull("returned object is null", sui);
296: Assert.assertEquals("Bad field 'name' value", name, sui
297: .getName());
298: Assert
299: .assertEquals("Bad field 'f1' value", f1, sui
300: .getF1());
301: pm.currentTransaction().begin();
302: pm.deletePersistent(sui);
303: pm.currentTransaction().commit();
304: pm.close();
305: } catch (Exception e) {
306: logger.log(BasicLevel.ERROR, "Exception", e);
307: fail(e.getMessage());
308: }
309: }
310:
311: public void testIntId() {
312: try {
313: String f1 = "testInt";
314: int name = 13;
315: Object id;
316: PersistenceManager pm = pmf.getPersistenceManager();
317: IntUserId iui = new IntUserId(name, f1);
318: pm.makePersistent(iui);
319: id = pm.getObjectId(iui);
320: Assert.assertNotNull("identifier is null", id);
321: pm.close();
322: iui = null; // garbage object
323: pm = pmf.getPersistenceManager();
324: id = pm.newObjectIdInstance(IntUserId.class, id.toString());
325: Assert.assertNotNull("built identifier is null", id);
326: iui = (IntUserId) pm.getObjectById(id, false);
327: Assert.assertNotNull("returned object is null", iui);
328: Assert.assertEquals("Bad field 'name' value", name, iui
329: .getName());
330: Assert
331: .assertEquals("Bad field 'f1' value", f1, iui
332: .getF1());
333: pm.currentTransaction().begin();
334: pm.deletePersistent(iui);
335: pm.currentTransaction().commit();
336: pm.close();
337: } catch (Exception e) {
338: logger.log(BasicLevel.ERROR, "Exception", e);
339: fail(e.getMessage());
340: }
341: }
342:
343: public void testRdbSequence1() {
344: try {
345: PersistenceManager pm = pmf.getPersistenceManager();
346: AutoIncId aii = new AutoIncId();
347: pm.makePersistent(aii);
348: Object id = pm.getObjectId(aii);
349: logger.log(BasicLevel.DEBUG, "id(AutoIncId): " + id);
350: Assert.assertNotNull("identifier is null", id);
351: pm.close();
352: pm = pmf.getPersistenceManager();
353: id = pm.newObjectIdInstance(AutoIncId.class, id.toString());
354: Assert.assertNotNull("built identifier is null", id);
355: aii = (AutoIncId) pm.getObjectById(id, false);
356: Assert.assertNotNull("returned object is null", aii);
357: pm.currentTransaction().begin();
358: pm.deletePersistent(aii);
359: pm.currentTransaction().commit();
360: pm.close();
361: } catch (Exception e) {
362: logger.log(BasicLevel.ERROR, "Exception", e);
363: fail(e.getMessage());
364: }
365: }
366:
367: public void testRdbSequence2() {
368: try {
369: PersistenceManager pm = pmf.getPersistenceManager();
370: AutoIncFieldId aifi = new AutoIncFieldId();
371: pm.makePersistent(aifi);
372: long id = aifi.getId();
373: logger.log(BasicLevel.DEBUG, "id(AutoIncId): " + id);
374: pm.close();
375:
376: aifi = null;
377: pm = pmf.getPersistenceManager();
378: pm.evictAll();
379: pm.close();
380:
381: pm = pmf.getPersistenceManager();
382: Object oid = pm.newObjectIdInstance(AutoIncFieldId.class,
383: "" + id);
384: Assert.assertNotNull("built identifier is null", oid);
385: aifi = (AutoIncFieldId) pm.getObjectById(oid, false);
386: Assert.assertNotNull("returned object is null", aifi);
387: pm.currentTransaction().begin();
388: pm.deletePersistent(aifi);
389: pm.currentTransaction().commit();
390: pm.close();
391: } catch (Exception e) {
392: logger.log(BasicLevel.ERROR, "Exception", e);
393: fail(e.getMessage());
394: }
395: }
396:
397: public void testRdbSequenceUserField() {
398: try {
399: PersistenceManager pm = pmf.getPersistenceManager();
400: AutoIncFieldId aifi = new AutoIncFieldId();
401: Assert.assertEquals("Bad id value before 'makePersistent'",
402: -1, aifi.getId());
403: pm.makePersistent(aifi);
404: long lid = aifi.getId();
405: Assert.assertTrue("Bad id value after 'makePersistent': "
406: + lid, 0 < lid);
407: logger.log(BasicLevel.DEBUG, "long id: " + lid);
408: Object id = pm.getObjectId(aifi);
409: logger.log(BasicLevel.DEBUG, "id(AutoIncId): " + id);
410: Assert.assertNotNull("identifier is null", id);
411: pm.close();
412:
413: pm = pmf.getPersistenceManager();
414: id = pm.newObjectIdInstance(AutoIncFieldId.class, id
415: .toString());
416: Assert.assertNotNull("built identifier is null (toString)",
417: id);
418: aifi = (AutoIncFieldId) pm.getObjectById(id, false);
419: Assert.assertNotNull("returned object is null (toString)",
420: aifi);
421: pm.close();
422:
423: pm = pmf.getPersistenceManager();
424: id = pm.newObjectIdInstance(AutoIncFieldId.class, "" + lid);
425: Assert.assertNotNull(
426: "built identifier is null (long value)", id);
427: aifi = (AutoIncFieldId) pm.getObjectById(id, false);
428: Assert.assertNotNull(
429: "returned object is null (long value)", aifi);
430: pm.currentTransaction().begin();
431: pm.deletePersistent(aifi);
432: pm.currentTransaction().commit();
433: pm.close();
434: } catch (Exception e) {
435: logger.log(BasicLevel.ERROR, "Exception", e);
436: fail(e.getMessage());
437: }
438: }
439:
440: public void testRdbSequenceRefToUserField() {
441: try {
442: PersistenceManager pm = pmf.getPersistenceManager();
443: pm.currentTransaction().begin();
444: ArrayList al = new ArrayList();
445: al.add(new AutoIncFieldId());
446: al.add(new AutoIncFieldId());
447: Ref2AutoIncFieldId ref = new Ref2AutoIncFieldId(
448: new AutoIncFieldId(), al);
449: pm.makePersistent(ref);
450: pm.currentTransaction().commit();
451: pm.currentTransaction().begin();
452: pm.deletePersistentAll(al);
453: pm.deletePersistent(ref.getSimpleRef());
454: pm.deletePersistent(ref);
455: pm.currentTransaction().commit();
456: pm.close();
457: } catch (Exception e) {
458: logger.log(BasicLevel.ERROR, "Exception", e);
459: fail(e.getMessage());
460: }
461: }
462:
463: public void testRef2BasicB() {
464: PersistenceManager pm = pmf.getPersistenceManager();
465: pm.currentTransaction().begin();
466: final String r2bb_id = "testRef2BasicB_r2bb_id";
467: final String bb_id = "testRef2BasicB_bb_id";
468: BasicB bb = new BasicB(bb_id, 1);
469: Ref2BasicB r2bb = new Ref2BasicB(r2bb_id);
470: pm.makePersistent(r2bb);
471: r2bb.setBasicb(bb);
472: pm.getObjectId(bb);
473: pm.currentTransaction().commit();
474: pm.evict(r2bb);
475: pm.evict(bb);
476: pm.close();
477:
478: pm = pmf.getPersistenceManager();
479: pm.currentTransaction().begin();
480: Object o = pm.getObjectById(pm.newObjectIdInstance(
481: Ref2BasicB.class, r2bb_id), false);
482: assertTrue("Bad object ", o == r2bb);
483: pm.deletePersistent(r2bb.getBasicb());
484: pm.deletePersistent(r2bb);
485: pm.currentTransaction().commit();
486: pm.close();
487: }
488:
489: public void testTwiceMakePersistent() {
490: try {
491: String name = "testTwiceMakePersistent";
492: String strid;
493: PersistenceManager pm = pmf.getPersistenceManager();
494: StringUserId sui = new StringUserId(name, 123456);
495: pm.currentTransaction().begin();
496: pm.makePersistent(sui);
497: pm.currentTransaction().commit();
498:
499: StringUserId sui2 = new StringUserId(name, 654321);
500: pm.currentTransaction().begin();
501: try {
502: pm.makePersistent(sui2);
503: fail("Same identifier not detected");
504: } catch (JDOException e) {
505: assertTrue("Object becomes persistent", !JDOHelper
506: .isPersistent(sui2));
507: assertEquals("Bad f1 value in non peristent object",
508: 654321, sui2.getF1());
509: }
510: pm.currentTransaction().commit();
511:
512: sui = null;
513: pm.evictAll();
514: pm.currentTransaction().begin();
515: try {
516: pm.makePersistent(sui2);
517: pm.currentTransaction().commit();
518: fail("Same identifier not detected with empty cache");
519: } catch (JDOException e) {
520: if (pm.currentTransaction().isActive()) {
521: pm.currentTransaction().rollback();
522: }
523: assertTrue("Object becomes persistent", !JDOHelper
524: .isPersistent(sui2));
525: assertEquals(
526: "Bad f1 value in non peristent object with empty cache",
527: 654321, sui2.getF1());
528: }
529:
530: pm.currentTransaction().begin();
531: sui = (StringUserId) pm.getObjectById(pm
532: .newObjectIdInstance(StringUserId.class, name),
533: false);
534: pm.deletePersistent(sui);
535: pm.currentTransaction().commit();
536: pm.close();
537: } catch (Exception e) {
538: logger.log(BasicLevel.ERROR, "Exception", e);
539: fail(e.getMessage());
540: }
541:
542: }
543: }
|