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.ref;
018:
019: import junit.framework.Assert;
020: import org.objectweb.speedo.SpeedoTestHelper;
021: import org.objectweb.speedo.pobjects.ref.Department;
022: import org.objectweb.speedo.pobjects.ref.Employee;
023:
024: import javax.jdo.PersistenceManager;
025:
026: /**
027: *
028: * @author S.Chassande-Barrioz
029: */
030: public class TestSimpleRef extends SpeedoTestHelper {
031:
032: public TestSimpleRef(String s) {
033: super (s);
034: }
035:
036: protected String getLoggerName() {
037: return LOG_NAME + "rt.ref.SimpleRef";
038: }
039:
040: public void test1() {
041: String eName = "seb";
042: String dName = "ASR";
043: PersistenceManager pm = pmf.getPersistenceManager();
044: Employee e = new Employee(eName, new Department(dName));
045: pm.makePersistent(e);
046: Object eId = pm.getObjectId(e);
047: Assert.assertNotNull("null object identifier", eId);
048: pm.close();
049:
050: e = null;
051: pm = pmf.getPersistenceManager();
052: e = (Employee) pm.getObjectById(eId, true);
053: Assert.assertNotNull("null instance returned by getObjectById",
054: e);
055: Assert.assertEquals("Bad employee name", eName, e.getName());
056: Assert.assertNotNull("null instance returned by e.dept", e
057: .getDept());
058: Assert.assertEquals("Bad department name", dName, e.getDept()
059: .getName());
060: pm.currentTransaction().begin();
061: pm.deletePersistent(e.getDept());
062: pm.deletePersistent(e);
063: pm.currentTransaction().commit();
064: pm.close();
065: }
066:
067: public void testNullRef() {
068: String eName = "nullRefEmp";
069: PersistenceManager pm = pmf.getPersistenceManager();
070: Employee e = new Employee(eName, null);
071: pm.makePersistent(e);
072: Object eId = pm.getObjectId(e);
073: Assert.assertNotNull("null object identifier", eId);
074: pm.close();
075:
076: e = null;
077: pm = pmf.getPersistenceManager();
078: e = (Employee) pm.getObjectById(eId, true);
079: Assert.assertNotNull("null instance returned by getObjectById",
080: e);
081: Assert.assertEquals("Bad employee name", eName, e.getName());
082: Assert.assertNull("not null instance returned by e.dept", e
083: .getDept());
084: pm.currentTransaction().begin();
085: pm.deletePersistent(e);
086: pm.currentTransaction().commit();
087: pm.close();
088: }
089:
090: public void testDeleteReferencedObjectWithEvict() {
091: testDeleteReferencedObject(true);
092: }
093:
094: public void testDeleteReferencedObject() {
095: testDeleteReferencedObject(false);
096: }
097:
098: public void testDeleteReferencedObject(boolean withEvict) {
099: PersistenceManager pm = pmf.getPersistenceManager();
100:
101: pm.currentTransaction().begin();
102: Employee e = new Employee(
103: "employee testDeleteReferencedObject", new Department(
104: "department testDeleteReferencedObject"));
105: pm.makePersistent(e);
106: long eid = e.getOid();
107: Long did = e.getDept().getOid();
108: e = null;
109: pm.currentTransaction().commit();
110:
111: pm.currentTransaction().begin();
112: Department d = (Department) pm.getObjectById(pm
113: .newObjectIdInstance(Department.class, "" + did), true);
114: pm.deletePersistent(d);
115: d = null;
116: pm.currentTransaction().commit();
117:
118: if (withEvict) {
119: pm.evictAll();
120: }
121: pm.currentTransaction().begin();
122: e = (Employee) pm.getObjectById(pm.newObjectIdInstance(
123: Employee.class, "" + eid), true);
124: boolean isNull = (null == e.getDept());
125: pm.currentTransaction().commit();
126:
127: pm.currentTransaction().begin();
128: pm.deletePersistent(e);
129: pm.currentTransaction().commit();
130: pm.close();
131: assertTrue("Reference is not null", isNull);
132: }
133: }
|