Address address3 = new Address("Brasil", "Rio de Janeiro", "Rua Professor Azevedo Marques");
Criteria emptyCriteria = new Criteria();
Query q;
Iterator it;
Transaction tx = _kit.getTransaction(_conn);
tx.begin();
// prepare tables for the test - empty them
q = QueryFactory.newQuery(AddressDesc.class, emptyCriteria);
for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
_conn.deletePersistent(it.next());
}
q = QueryFactory.newQuery(Person.class, emptyCriteria);
for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
_conn.deletePersistent(it.next());
}
q = QueryFactory.newQuery(Address.class, emptyCriteria);
for (it = _conn.getIteratorByQuery(q); it.hasNext(); ) {
_conn.deletePersistent(it.next());
}
tx.commit();
person.setMainAddress(address1);
person.addOtherAddress("work", address2);
person.addOtherAddress("dream", address3);
tx = _kit.getTransaction(_conn);
tx.begin();
// Cascade create
_conn.makePersistent(person);
tx.commit();
Identity oid = _conn.getIdentity(person);
_conn.invalidateAll();
tx = _kit.getTransaction(_conn);
tx.begin();
person = (Person) _conn.getObjectByIdentity(oid);
assertTrue("person exists", (person != null));
assertTrue("main Address exists", (person.getMainAddress() != null));
assertEquals("main Address is correct", address1.getStreet(), person.getMainAddress().getStreet());
assertEquals("two other Addresses", 2, person.getOtherAddresses().size());
AddressDesc desc1 = (AddressDesc) person.getOtherAddresses().get(0);
assertEquals("1st other Address has correct description", "work", desc1.getDesc());
assertEquals("1st other Address is correct", address2.getStreet(), desc1.getAddress().getStreet());
AddressDesc desc2 = (AddressDesc) person.getOtherAddresses().get(1);
assertEquals("2nd other Address has correct description", "dream", desc2.getDesc());
assertEquals("2nd other Address is correct", address3.getStreet(), desc2.getAddress().getStreet());
// Delete dependent
person.setMainAddress(null);
person.getOtherAddresses().remove(1);
tx.commit();
_conn.invalidateAll();
tx = _kit.getTransaction(_conn);
tx.begin();
person = (Person) _conn.getObjectByIdentity(oid);
assertTrue("main Address doesn't exist", (person.getMainAddress() == null));
assertEquals("one other Address", 1, person.getOtherAddresses().size());
desc2 = (AddressDesc) person.getOtherAddresses().get(0);
assertEquals("the other Address has correct description", "work", desc1.getDesc());
assertEquals("the other Address is correct", address2.getStreet(), desc1.getAddress().getStreet());
// Create dependent
person.setMainAddress(address1);
person.addOtherAddress("dream", address3);
tx.commit();
_conn.invalidateAll();
tx = _kit.getTransaction(_conn);
tx.begin();
person = (Person) _conn.getObjectByIdentity(oid);
assertTrue("main Address exists", (person.getMainAddress() != null));
assertEquals("main Address is correct", address1.getStreet(), person.getMainAddress().getStreet());
assertEquals("two other Addresses", 2, person.getOtherAddresses().size());
desc1 = (AddressDesc) person.getOtherAddresses().get(0);
assertEquals("1st other Address has correct description", "work", desc1.getDesc());
assertEquals("1st other Address is correct", address2.getStreet(), desc1.getAddress().getStreet());
desc2 = (AddressDesc) person.getOtherAddresses().get(1);
assertEquals("2nd other Address has correct description", "dream", desc2.getDesc());
assertEquals("2nd other Address is correct", address3.getStreet(), desc2.getAddress().getStreet());
// Cascade delete
_conn.deletePersistent(person);
tx.commit();
_conn.invalidateAll();
tx = _kit.getTransaction(_conn);
tx.begin();
person = (Person) _conn.getObjectByIdentity(oid);
assertTrue("person doesn't exist", (person == null));
q = QueryFactory.newQuery(AddressDesc.class, emptyCriteria);
it = _conn.getIteratorByQuery(q);
assertTrue("address descriptions don't exist", !it.hasNext());
q = QueryFactory.newQuery(Address.class, emptyCriteria);
it = _conn.getIteratorByQuery(q);
assertTrue("addresses don't exist", !it.hasNext());
tx.commit();
}