001: package org.apache.ojb.otm;
002:
003: import java.util.Iterator;
004: import junit.framework.TestCase;
005: import org.apache.ojb.broker.Identity;
006: import org.apache.ojb.broker.PersistenceBrokerFactory;
007: import org.apache.ojb.broker.query.Criteria;
008: import org.apache.ojb.broker.query.Query;
009: import org.apache.ojb.broker.query.QueryFactory;
010: import org.apache.ojb.otm.core.Transaction;
011: import org.apache.ojb.otm.lock.LockingException;
012:
013: public class DependentTests extends TestCase {
014: private static Class CLASS = DependentTests.class;
015: private TestKit _kit;
016: private OTMConnection _conn;
017:
018: public void setUp() throws LockingException {
019: _kit = TestKit.getTestInstance();
020: _conn = _kit.acquireConnection(PersistenceBrokerFactory
021: .getDefaultKey());
022: }
023:
024: public void tearDown() throws LockingException {
025: _conn.close();
026: _conn = null;
027: }
028:
029: public static void main(String[] args) {
030: String[] arr = { CLASS.getName() };
031: junit.textui.TestRunner.main(arr);
032: }
033:
034: public void testDependent() throws Exception {
035: Person person = new Person("Ostap", "Bender");
036: Address address1 = new Address("Ukraine", "Odessa",
037: "Deribasovskaya");
038: Address address2 = new Address("Ukraine", "Odessa",
039: "Malaya Arnautskaya");
040: Address address3 = new Address("Brasil", "Rio de Janeiro",
041: "Rua Professor Azevedo Marques");
042: Criteria emptyCriteria = new Criteria();
043: Query q;
044: Iterator it;
045:
046: Transaction tx = _kit.getTransaction(_conn);
047: tx.begin();
048: // prepare tables for the test - empty them
049: q = QueryFactory.newQuery(AddressDesc.class, emptyCriteria);
050: for (it = _conn.getIteratorByQuery(q); it.hasNext();) {
051: _conn.deletePersistent(it.next());
052: }
053: q = QueryFactory.newQuery(Person.class, emptyCriteria);
054: for (it = _conn.getIteratorByQuery(q); it.hasNext();) {
055: _conn.deletePersistent(it.next());
056: }
057: q = QueryFactory.newQuery(Address.class, emptyCriteria);
058: for (it = _conn.getIteratorByQuery(q); it.hasNext();) {
059: _conn.deletePersistent(it.next());
060: }
061: tx.commit();
062:
063: person.setMainAddress(address1);
064: person.addOtherAddress("work", address2);
065: person.addOtherAddress("dream", address3);
066:
067: tx = _kit.getTransaction(_conn);
068: tx.begin();
069: // Cascade create
070: _conn.makePersistent(person);
071: tx.commit();
072:
073: Identity oid = _conn.getIdentity(person);
074:
075: _conn.invalidateAll();
076: tx = _kit.getTransaction(_conn);
077: tx.begin();
078: person = (Person) _conn.getObjectByIdentity(oid);
079: assertTrue("person exists", (person != null));
080: assertTrue("main Address exists",
081: (person.getMainAddress() != null));
082: assertEquals("main Address is correct", address1.getStreet(),
083: person.getMainAddress().getStreet());
084: assertEquals("two other Addresses", 2, person
085: .getOtherAddresses().size());
086: AddressDesc desc1 = (AddressDesc) person.getOtherAddresses()
087: .get(0);
088: assertEquals("1st other Address has correct description",
089: "work", desc1.getDesc());
090: assertEquals("1st other Address is correct", address2
091: .getStreet(), desc1.getAddress().getStreet());
092: AddressDesc desc2 = (AddressDesc) person.getOtherAddresses()
093: .get(1);
094: assertEquals("2nd other Address has correct description",
095: "dream", desc2.getDesc());
096: assertEquals("2nd other Address is correct", address3
097: .getStreet(), desc2.getAddress().getStreet());
098:
099: // Delete dependent
100: person.setMainAddress(null);
101: person.getOtherAddresses().remove(1);
102: tx.commit();
103:
104: _conn.invalidateAll();
105: tx = _kit.getTransaction(_conn);
106: tx.begin();
107: person = (Person) _conn.getObjectByIdentity(oid);
108: assertTrue("main Address doesn't exist", (person
109: .getMainAddress() == null));
110: assertEquals("one other Address", 1, person.getOtherAddresses()
111: .size());
112: desc2 = (AddressDesc) person.getOtherAddresses().get(0);
113: assertEquals("the other Address has correct description",
114: "work", desc1.getDesc());
115: assertEquals("the other Address is correct", address2
116: .getStreet(), desc1.getAddress().getStreet());
117:
118: // Create dependent
119: person.setMainAddress(address1);
120: person.addOtherAddress("dream", address3);
121: tx.commit();
122:
123: _conn.invalidateAll();
124: tx = _kit.getTransaction(_conn);
125: tx.begin();
126: person = (Person) _conn.getObjectByIdentity(oid);
127: assertTrue("main Address exists",
128: (person.getMainAddress() != null));
129: assertEquals("main Address is correct", address1.getStreet(),
130: person.getMainAddress().getStreet());
131: assertEquals("two other Addresses", 2, person
132: .getOtherAddresses().size());
133: desc1 = (AddressDesc) person.getOtherAddresses().get(0);
134: assertEquals("1st other Address has correct description",
135: "work", desc1.getDesc());
136: assertEquals("1st other Address is correct", address2
137: .getStreet(), desc1.getAddress().getStreet());
138: desc2 = (AddressDesc) person.getOtherAddresses().get(1);
139: assertEquals("2nd other Address has correct description",
140: "dream", desc2.getDesc());
141: assertEquals("2nd other Address is correct", address3
142: .getStreet(), desc2.getAddress().getStreet());
143:
144: // Cascade delete
145: _conn.deletePersistent(person);
146: tx.commit();
147:
148: _conn.invalidateAll();
149: tx = _kit.getTransaction(_conn);
150: tx.begin();
151: person = (Person) _conn.getObjectByIdentity(oid);
152: assertTrue("person doesn't exist", (person == null));
153: q = QueryFactory.newQuery(AddressDesc.class, emptyCriteria);
154: it = _conn.getIteratorByQuery(q);
155: assertTrue("address descriptions don't exist", !it.hasNext());
156: q = QueryFactory.newQuery(Address.class, emptyCriteria);
157: it = _conn.getIteratorByQuery(q);
158: assertTrue("addresses don't exist", !it.hasNext());
159: tx.commit();
160: }
161:
162: public void testDependent2() throws Exception {
163: AbstractPerson person = new LegalPerson();
164: Debitor debitor = new Debitor();
165: Address2 address = new Address2();
166: Criteria emptyCriteria = new Criteria();
167: Query q;
168: Iterator it;
169:
170: Transaction tx = _kit.getTransaction(_conn);
171: tx.begin();
172: // prepare tables for the test - empty them
173: q = QueryFactory.newQuery(Debitor.class, emptyCriteria);
174: for (it = _conn.getIteratorByQuery(q); it.hasNext();) {
175: _conn.deletePersistent(it.next());
176: }
177: q = QueryFactory.newQuery(AbstractPerson.class, emptyCriteria);
178: for (it = _conn.getIteratorByQuery(q); it.hasNext();) {
179: _conn.deletePersistent(it.next());
180: }
181: q = QueryFactory.newQuery(Address2.class, emptyCriteria);
182: for (it = _conn.getIteratorByQuery(q); it.hasNext();) {
183: _conn.deletePersistent(it.next());
184: }
185: tx.commit();
186:
187: person.getAddresses().add(address);
188: debitor.setAbstractPerson(person);
189:
190: tx = _kit.getTransaction(_conn);
191: tx.begin();
192: // Cascade create
193: _conn.makePersistent(debitor);
194: tx.commit();
195:
196: Identity debitorOid = _conn.getIdentity(debitor);
197: Identity personOid = _conn.getIdentity(person);
198: int addrId = address.getId();
199:
200: _conn.invalidateAll();
201: tx = _kit.getTransaction(_conn);
202: tx.begin();
203: debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
204: assertNotNull("debitor does not exist", debitor);
205: person = debitor.getAbstractPerson();
206: assertNotNull("person does not exist", person);
207: assertTrue("person has not the expected type",
208: (person instanceof LegalPerson));
209: assertEquals("address does not exist", 1, person.getAddresses()
210: .size());
211: address = (Address2) person.getAddresses().iterator().next();
212: assertEquals("addressid is not correct", addrId, address
213: .getId());
214:
215: // Delete dependent
216: person.getAddresses().clear();
217: tx.commit();
218:
219: _conn.invalidateAll();
220: tx = _kit.getTransaction(_conn);
221: tx.begin();
222: debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
223: person = (LegalPerson) debitor.getAbstractPerson();
224: assertEquals("address was not deleted", person.getAddresses()
225: .size(), 0);
226:
227: // Create dependent
228: person.getAddresses().add(address);
229: tx.commit();
230:
231: _conn.invalidateAll();
232: tx = _kit.getTransaction(_conn);
233: tx.begin();
234: debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
235: person = (LegalPerson) debitor.getAbstractPerson();
236: assertEquals("address does not exist", person.getAddresses()
237: .size(), 1);
238: tx.commit();
239:
240: // Change dependent reference, should delete old dependant
241: person = new NaturalPerson();
242: person.setName("before");
243: debitor.setAbstractPerson(person);
244:
245: _conn.invalidateAll();
246: tx = _kit.getTransaction(_conn);
247: tx.begin();
248: _conn.makePersistent(debitor);
249: tx.commit();
250:
251: _conn.invalidateAll();
252: tx = _kit.getTransaction(_conn);
253: tx.begin();
254: assertTrue("old person has not been deleted", (_conn
255: .getObjectByIdentity(personOid) == null));
256: q = QueryFactory.newQuery(Address2.class, emptyCriteria);
257: it = _conn.getIteratorByQuery(q);
258: assertTrue("old address has not been deleted", !it.hasNext());
259: person = debitor.getAbstractPerson();
260: assertTrue("new person has unexpected type",
261: (person instanceof NaturalPerson));
262: assertTrue("person does not have correct name", "before"
263: .equals(person.getName()));
264: tx.commit();
265:
266: person.setName("after");
267: assertTrue("name of person was not saved", "after"
268: .equals(debitor.getAbstractPerson().getName()));
269:
270: _conn.invalidateAll();
271: tx = _kit.getTransaction(_conn);
272: tx.begin();
273: _conn.makePersistent(debitor);
274: tx.commit();
275: assertTrue("name of person was not saved: "
276: + debitor.getAbstractPerson().getName(), "after"
277: .equals(debitor.getAbstractPerson().getName()));
278:
279: _conn.invalidateAll();
280: tx = _kit.getTransaction(_conn);
281: tx.begin();
282: debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
283: person = debitor.getAbstractPerson();
284: assertTrue("name of person was not saved: "
285: + debitor.getAbstractPerson().getName(), "after"
286: .equals(debitor.getAbstractPerson().getName()));
287: // Cascade delete
288: _conn.deletePersistent(debitor);
289: tx.commit();
290:
291: _conn.invalidateAll();
292: tx = _kit.getTransaction(_conn);
293: tx.begin();
294: debitor = (Debitor) _conn.getObjectByIdentity(debitorOid);
295: assertNull("debitor still exists", debitor);
296: q = QueryFactory.newQuery(AbstractPerson.class, emptyCriteria);
297: it = _conn.getIteratorByQuery(q);
298: assertTrue("persons still exist", !it.hasNext());
299: tx.commit();
300: }
301: }
|