KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > ref > TestSimpleRef


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

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 /**
28  *
29  * @author S.Chassande-Barrioz
30  */

31 public class TestSimpleRef extends SpeedoTestHelper {
32
33     public TestSimpleRef(String JavaDoc s) {
34         super(s);
35     }
36
37     protected String JavaDoc getLoggerName() {
38         return LOG_NAME + "rt.ref.SimpleRef";
39     }
40     public void test1() {
41         String JavaDoc eName = "seb";
42         String JavaDoc dName = "ASR";
43         PersistenceManager pm = pmf.getPersistenceManager();
44         Employee e = new Employee(eName, new Department(dName));
45         pm.makePersistent(e);
46         Object JavaDoc 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 JavaDoc eName = "nullRefEmp";
66         PersistenceManager pm = pmf.getPersistenceManager();
67         Employee e = new Employee(eName, null);
68         pm.makePersistent(e);
69         Object JavaDoc 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 JavaDoc 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