KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > otm > DependentTests


1 package org.apache.ojb.otm;
2
3 import java.util.Iterator JavaDoc;
4 import junit.framework.TestCase;
5 import org.apache.ojb.broker.Identity;
6 import org.apache.ojb.broker.PersistenceBrokerFactory;
7 import org.apache.ojb.broker.query.Criteria;
8 import org.apache.ojb.broker.query.Query;
9 import org.apache.ojb.broker.query.QueryFactory;
10 import org.apache.ojb.otm.core.Transaction;
11 import org.apache.ojb.otm.lock.LockingException;
12
13 public class DependentTests extends TestCase
14 {
15     private static Class JavaDoc CLASS = DependentTests.class;
16     private TestKit _kit;
17     private OTMConnection _conn;
18
19     public void setUp() throws LockingException
20     {
21         _kit = TestKit.getTestInstance();
22         _conn = _kit.acquireConnection(PersistenceBrokerFactory.getDefaultKey());
23     }
24
25     public void tearDown() throws LockingException
26     {
27         _conn.close();
28         _conn = null;
29     }
30
31     public static void main(String JavaDoc[] args)
32     {
33         String JavaDoc[] arr = {CLASS.getName()};
34         junit.textui.TestRunner.main(arr);
35     }
36
37     public void testDependent() throws Exception JavaDoc
38     {
39         Person person = new Person("Ostap", "Bender");
40         Address address1 = new Address("Ukraine", "Odessa", "Deribasovskaya");
41         Address address2 = new Address("Ukraine", "Odessa", "Malaya Arnautskaya");
42         Address address3 = new Address("Brasil", "Rio de Janeiro", "Rua Professor Azevedo Marques");
43         Criteria emptyCriteria = new Criteria();
44         Query q;
45         Iterator JavaDoc it;
46
47         Transaction tx = _kit.getTransaction(_conn);
48         tx.begin();
49         // prepare tables for the test - empty them
50
q = QueryFactory.newQuery(AddressDesc.class, emptyCriteria);
51         for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
52             _conn.deletePersistent(it.next());
53         }
54         q = QueryFactory.newQuery(Person.class, emptyCriteria);
55         for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
56             _conn.deletePersistent(it.next());
57         }
58         q = QueryFactory.newQuery(Address.class, emptyCriteria);
59         for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
60             _conn.deletePersistent(it.next());
61         }
62         tx.commit();
63
64         person.setMainAddress(address1);
65         person.addOtherAddress("work", address2);
66         person.addOtherAddress("dream", address3);
67
68         tx = _kit.getTransaction(_conn);
69         tx.begin();
70         // Cascade create
71
_conn.makePersistent(person);
72         tx.commit();
73
74         Identity oid = _conn.getIdentity(person);
75
76         _conn.invalidateAll();
77         tx = _kit.getTransaction(_conn);
78         tx.begin();
79         person = (Person) _conn.getObjectByIdentity(oid);
80         assertTrue("person exists", (person != null));
81         assertTrue("main Address exists", (person.getMainAddress() != null));
82         assertEquals("main Address is correct", address1.getStreet(), person.getMainAddress().getStreet());
83         assertEquals("two other Addresses", 2, person.getOtherAddresses().size());
84         AddressDesc desc1 = (AddressDesc) person.getOtherAddresses().get(0);
85         assertEquals("1st other Address has correct description", "work", desc1.getDesc());
86         assertEquals("1st other Address is correct", address2.getStreet(), desc1.getAddress().getStreet());
87         AddressDesc desc2 = (AddressDesc) person.getOtherAddresses().get(1);
88         assertEquals("2nd other Address has correct description", "dream", desc2.getDesc());
89         assertEquals("2nd other Address is correct", address3.getStreet(), desc2.getAddress().getStreet());
90
91         // Delete dependent
92
person.setMainAddress(null);
93         person.getOtherAddresses().remove(1);
94         tx.commit();
95
96         _conn.invalidateAll();
97         tx = _kit.getTransaction(_conn);
98         tx.begin();
99         person = (Person) _conn.getObjectByIdentity(oid);
100         assertTrue("main Address doesn't exist", (person.getMainAddress() == null));
101         assertEquals("one other Address", 1, person.getOtherAddresses().size());
102         desc2 = (AddressDesc) person.getOtherAddresses().get(0);
103         assertEquals("the other Address has correct description", "work", desc1.getDesc());
104         assertEquals("the other Address is correct", address2.getStreet(), desc1.getAddress().getStreet());
105
106         // Create dependent
107
person.setMainAddress(address1);
108         person.addOtherAddress("dream", address3);
109         tx.commit();
110
111         _conn.invalidateAll();
112         tx = _kit.getTransaction(_conn);
113         tx.begin();
114         person = (Person) _conn.getObjectByIdentity(oid);
115         assertTrue("main Address exists", (person.getMainAddress() != null));
116         assertEquals("main Address is correct", address1.getStreet(), person.getMainAddress().getStreet());
117         assertEquals("two other Addresses", 2, person.getOtherAddresses().size());
118         desc1 = (AddressDesc) person.getOtherAddresses().get(0);
119         assertEquals("1st other Address has correct description", "work", desc1.getDesc());
120         assertEquals("1st other Address is correct", address2.getStreet(), desc1.getAddress().getStreet());
121         desc2 = (AddressDesc) person.getOtherAddresses().get(1);
122         assertEquals("2nd other Address has correct description", "dream", desc2.getDesc());
123         assertEquals("2nd other Address is correct", address3.getStreet(), desc2.getAddress().getStreet());
124
125         // Cascade delete
126
_conn.deletePersistent(person);
127         tx.commit();
128
129         _conn.invalidateAll();
130         tx = _kit.getTransaction(_conn);
131         tx.begin();
132         person = (Person) _conn.getObjectByIdentity(oid);
133         assertTrue("person doesn't exist", (person == null));
134         q = QueryFactory.newQuery(AddressDesc.class, emptyCriteria);
135         it = _conn.getIteratorByQuery(q);
136         assertTrue("address descriptions don't exist", !it.hasNext());
137         q = QueryFactory.newQuery(Address.class, emptyCriteria);
138         it = _conn.getIteratorByQuery(q);
139         assertTrue("addresses don't exist", !it.hasNext());
140         tx.commit();
141     }
142
143     public void testDependent2() throws Exception JavaDoc
144     {
145         AbstractPerson person = new LegalPerson();
146         Debitor debitor = new Debitor();
147         Address2 address = new Address2();
148         Criteria emptyCriteria = new Criteria();
149         Query q;
150         Iterator JavaDoc it;
151
152         Transaction tx = _kit.getTransaction(_conn);
153         tx.begin();
154         // prepare tables for the test - empty them
155
q = QueryFactory.newQuery(Debitor.class, emptyCriteria);
156         for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
157             _conn.deletePersistent(it.next());
158         }
159         q = QueryFactory.newQuery(AbstractPerson.class, emptyCriteria);
160         for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
161             _conn.deletePersistent(it.next());
162         }
163         q = QueryFactory.newQuery(Address2.class, emptyCriteria);
164         for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
165             _conn.deletePersistent(it.next());
166         }
167         tx.commit();
168
169         person.getAddresses().add(address);
170         debitor.setAbstractPerson(person);
171
172         tx = _kit.getTransaction(_conn);
173         tx.begin();
174         // Cascade create
175
_conn.makePersistent(debitor);
176         tx.commit();
177
178         Identity debitorOid = _conn.getIdentity(debitor);
179         Identity personOid = _conn.getIdentity(person);
180         int addrId = address.getId();
181
182         _conn.invalidateAll();
183         tx = _kit.getTransaction(_conn);
184         tx.begin();
185         debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
186         assertNotNull("debitor does not exist", debitor);
187         person = debitor.getAbstractPerson();
188         assertNotNull("person does not exist", person);
189         assertTrue("person has not the expected type", (person instanceof LegalPerson));
190         assertEquals("address does not exist", 1, person.getAddresses().size());
191         address = (Address2) person.getAddresses().iterator().next();
192         assertEquals("addressid is not correct", addrId, address.getId());
193
194         // Delete dependent
195
person.getAddresses().clear();
196         tx.commit();
197
198         _conn.invalidateAll();
199         tx = _kit.getTransaction(_conn);
200         tx.begin();
201         debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
202         person = (LegalPerson) debitor.getAbstractPerson();
203         assertEquals("address was not deleted", person.getAddresses().size(), 0);
204
205         // Create dependent
206
person.getAddresses().add(address);
207         tx.commit();
208
209         _conn.invalidateAll();
210         tx = _kit.getTransaction(_conn);
211         tx.begin();
212         debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
213         person = (LegalPerson) debitor.getAbstractPerson();
214         assertEquals("address does not exist", person.getAddresses().size(), 1);
215         tx.commit();
216
217         // Change dependent reference, should delete old dependant
218
person = new NaturalPerson();
219         person.setName("before");
220         debitor.setAbstractPerson(person);
221
222         _conn.invalidateAll();
223         tx = _kit.getTransaction(_conn);
224         tx.begin();
225         _conn.makePersistent(debitor);
226         tx.commit();
227
228         _conn.invalidateAll();
229         tx = _kit.getTransaction(_conn);
230         tx.begin();
231         assertTrue("old person has not been deleted", (_conn.getObjectByIdentity(personOid) == null));
232         q = QueryFactory.newQuery(Address2.class, emptyCriteria);
233         it = _conn.getIteratorByQuery(q);
234         assertTrue("old address has not been deleted", !it.hasNext());
235         person = debitor.getAbstractPerson();
236         assertTrue("new person has unexpected type", (person instanceof NaturalPerson));
237         assertTrue("person does not have correct name", "before".equals(person.getName()));
238         tx.commit();
239
240         person.setName("after");
241         assertTrue("name of person was not saved", "after".equals(debitor.getAbstractPerson().getName()));
242
243         _conn.invalidateAll();
244         tx = _kit.getTransaction(_conn);
245         tx.begin();
246         _conn.makePersistent(debitor);
247         tx.commit();
248         assertTrue("name of person was not saved: " + debitor.getAbstractPerson().getName(),
249                    "after".equals(debitor.getAbstractPerson().getName()));
250
251         _conn.invalidateAll();
252         tx = _kit.getTransaction(_conn);
253         tx.begin();
254         debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
255         person = debitor.getAbstractPerson();
256         assertTrue("name of person was not saved: " + debitor.getAbstractPerson().getName(),
257                    "after".equals(debitor.getAbstractPerson().getName()));
258         // Cascade delete
259
_conn.deletePersistent(debitor);
260         tx.commit();
261
262         _conn.invalidateAll();
263         tx = _kit.getTransaction(_conn);
264         tx.begin();
265         debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
266         assertNull("debitor still exists", debitor);
267         q = QueryFactory.newQuery(AbstractPerson.class, emptyCriteria);
268         it = _conn.getIteratorByQuery(q);
269         assertTrue("persons still exist", !it.hasNext());
270         tx.commit();
271     }
272 }
273
Popular Tags