1 18 package org.objectweb.speedo.runtime.ref; 19 20 import junit.framework.Assert; 21 import org.objectweb.speedo.SpeedoTestHelper; 22 import org.objectweb.speedo.pobjects.ref.Department; 23 import org.objectweb.speedo.pobjects.ref.Employee; 24 25 import javax.jdo.PersistenceManager; 26 27 31 public class TestSimpleRef extends SpeedoTestHelper { 32 33 public TestSimpleRef(String s) { 34 super(s); 35 } 36 37 protected String getLoggerName() { 38 return LOG_NAME + "rt.ref.SimpleRef"; 39 } 40 public void test1() { 41 String eName = "seb"; 42 String dName = "ASR"; 43 PersistenceManager pm = pmf.getPersistenceManager(); 44 Employee e = new Employee(eName, new Department(dName)); 45 pm.makePersistent(e); 46 Object eId = pm.getObjectId(e); 47 Assert.assertNotNull("null object identifier", eId); 48 pm.close(); 49 50 e = null; 51 pm = pmf.getPersistenceManager(); 52 e = (Employee) pm.getObjectById(eId, true); 53 Assert.assertNotNull("null instance returned by getObjectById", e); 54 Assert.assertEquals("Bad employee name", eName, e.getName()); 55 Assert.assertNotNull("null instance returned by e.dept", e.getDept()); 56 Assert.assertEquals("Bad department name", dName, e.getDept().getName()); 57 pm.currentTransaction().begin(); 58 pm.deletePersistent(e.getDept()); 59 pm.deletePersistent(e); 60 pm.currentTransaction().commit(); 61 pm.close(); 62 } 63 64 public void testNullRef() { 65 String eName = "nullRefEmp"; 66 PersistenceManager pm = pmf.getPersistenceManager(); 67 Employee e = new Employee(eName, null); 68 pm.makePersistent(e); 69 Object eId = pm.getObjectId(e); 70 Assert.assertNotNull("null object identifier", eId); 71 pm.close(); 72 73 e = null; 74 pm = pmf.getPersistenceManager(); 75 e = (Employee) pm.getObjectById(eId, true); 76 Assert.assertNotNull("null instance returned by getObjectById", e); 77 Assert.assertEquals("Bad employee name", eName, e.getName()); 78 Assert.assertNull("not null instance returned by e.dept", e.getDept()); 79 pm.currentTransaction().begin(); 80 pm.deletePersistent(e); 81 pm.currentTransaction().commit(); 82 pm.close(); 83 } 84 public void testDeleteReferencedObjectWithEvict() { 85 testDeleteReferencedObject(true); 86 } 87 public void testDeleteReferencedObject() { 88 testDeleteReferencedObject(false); 89 } 90 public void testDeleteReferencedObject(boolean withEvict) { 91 PersistenceManager pm = pmf.getPersistenceManager(); 92 93 pm.currentTransaction().begin(); 94 Employee e = new Employee("employee testDeleteReferencedObject", 95 new Department("department testDeleteReferencedObject")); 96 pm.makePersistent(e); 97 long eid = e.getOid(); 98 Long did = e.getDept().getOid(); 99 e= null; 100 pm.currentTransaction().commit(); 101 102 pm.currentTransaction().begin(); 103 Department d = (Department) pm.getObjectById( 104 pm.newObjectIdInstance(Department.class, "" + did), true); 105 pm.deletePersistent(d); 106 d = null; 107 pm.currentTransaction().commit(); 108 109 if (withEvict) { 110 pm.evictAll(); 111 } 112 pm.currentTransaction().begin(); 113 e = (Employee) pm.getObjectById( 114 pm.newObjectIdInstance(Employee.class, "" + eid), true); 115 boolean isNull = (null == e.getDept()); 116 pm.currentTransaction().commit(); 117 118 pm.currentTransaction().begin(); 119 pm.deletePersistent(e); 120 pm.currentTransaction().commit(); 121 pm.close(); 122 assertTrue("Reference is not null", isNull); 123 } 124 } 125 | Popular Tags |