use of com.googlecode.lanterna.gui2.Window in project tests by datanucleus.
the class ManagedRelationshipTest method testOneToManyJoinBidirPersistCollection.
/**
* Test for management of relations with a 1-N jointable bidir where the objects are persisted
* and only the collection side is set.
*/
public void testOneToManyJoinBidirPersistCollection() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
Transaction tx = pm.currentTransaction();
try {
tx.begin();
House house = new House(15, "King Street");
Window window1 = new Window(150, 200, null);
Window window2 = new Window(350, 400, null);
house.addWindow(window1);
house.addWindow(window2);
assertNull("Window1 has non-null House but should be null before persist", window1.getHouse());
assertNull("Window2 has non-null House but should be null before persist", window2.getHouse());
assertNotNull("House has null Windows yet should be non-null before persist", house.getWindows());
pm.makePersistent(house);
pm.flush();
// Check that the relation sides are both set
assertNotNull("Window1 has null House but should be not-null after persist/flush", window1.getHouse());
assertNotNull("Window2 has null House but should be not-null after persist/flush", window2.getHouse());
assertNotNull("House has null Windows yet should be non-null after persist/flush", house.getWindows());
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown persisting 1-N JoinTable bidir with only collection side set", e);
fail("Error in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(House.class);
clean(Window.class);
}
}
use of com.googlecode.lanterna.gui2.Window in project tests by datanucleus.
the class ManagedRelationshipTest method testOneToManyJoinBidirPersistInconsistent.
/**
* Test for management of relations with a 1-N jointable bidir where the objects are persisted
* and the owner side of elements is inconsistent with the container.
*/
public void testOneToManyJoinBidirPersistInconsistent() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// house1 -> window1, window2 BUT window1 -> house2, window2 -> house2
House house1 = new House(15, "King Street");
House house2 = new House(23, "Queen Street");
Window window1 = new Window(150, 200, null);
Window window2 = new Window(350, 400, null);
house1.addWindow(window1);
house1.addWindow(window2);
window1.setHouse(house2);
window2.setHouse(house2);
pm.makePersistent(house1);
pm.flush();
fail("Should have thrown exception when persisting inconsistent 1-N JoinTable bidir relation");
tx.commit();
} catch (Exception e) {
// Success
return;
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(House.class);
clean(Window.class);
}
}
use of com.googlecode.lanterna.gui2.Window in project tests by datanucleus.
the class ManagedRelationshipTest method testOneToManyJoinBidirUpdateElement.
/**
* Test for management of relations with a 1-N jointable bidir where an element is updated to be in a different
* owner collection, and check that they are updated accordingly.
*/
public void testOneToManyJoinBidirUpdateElement() {
try {
// Persist the objects so we have them
PersistenceManager pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
Transaction tx = pm.currentTransaction();
Object house1Id = null;
Object house2Id = null;
Object window1Id = null;
Object window2Id = null;
try {
tx.begin();
House house1 = new House(15, "King Street");
House house2 = new House(35, "Queen Street");
Window window1 = new Window(150, 200, null);
Window window2 = new Window(350, 400, null);
house1.addWindow(window1);
house1.addWindow(window2);
window1.setHouse(house1);
window2.setHouse(house1);
pm.makePersistent(house1);
pm.makePersistent(house2);
tx.commit();
house1Id = pm.getObjectId(house1);
house2Id = pm.getObjectId(house2);
window1Id = pm.getObjectId(window1);
window2Id = pm.getObjectId(window2);
} catch (Exception e) {
LOG.error("Exception thrown persisting 1-N JoinTable bidir", e);
fail("Error in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the objects and update an element
pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
tx = pm.currentTransaction();
try {
tx.begin();
House house1 = (House) pm.getObjectById(house1Id);
House house2 = (House) pm.getObjectById(house2Id);
Window window1 = (Window) pm.getObjectById(window1Id);
Window window2 = (Window) pm.getObjectById(window2Id);
assertEquals("Window1 'house' is incorrect at retrieve", house1, window1.getHouse());
assertEquals("Window2 'house' is incorrect at retrieve", house1, window2.getHouse());
assertEquals("House1 has incorrect number of windows at retrieve", 2, house1.getWindows().size());
assertEquals("House2 has incorrect number of windows at retrieve", 0, house2.getWindows().size());
// Update Window1 to be in House2
window1.setHouse(house2);
pm.flush();
assertEquals("House1 has incorrect number of windows after window update", 1, house1.getWindows().size());
assertEquals("House2 has incorrect number of windows after window update", 1, house2.getWindows().size());
assertFalse("House1 contains window1 after update but shouldnt", house1.getWindows().contains(window1));
assertTrue("House2 doesnt contain window1 after update but should", house2.getWindows().contains(window1));
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown updating element from 1-N JoinTable bidir", e);
fail("Error in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the objects and check
pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
tx = pm.currentTransaction();
try {
tx.begin();
House house1 = (House) pm.getObjectById(house1Id);
House house2 = (House) pm.getObjectById(house2Id);
Window window1 = (Window) pm.getObjectById(window1Id);
Window window2 = (Window) pm.getObjectById(window2Id);
assertEquals("Window1 'house' is incorrect at retrieve", house2, window1.getHouse());
assertEquals("Window2 'house' is incorrect at retrieve", house1, window2.getHouse());
assertEquals("House1 has incorrect number of windows at retrieve", 1, house1.getWindows().size());
assertEquals("House2 has incorrect number of windows at retrieve", 1, house2.getWindows().size());
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown updating element from 1-N JoinTable bidir", e);
fail("Error in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(House.class);
clean(Window.class);
}
}
use of com.googlecode.lanterna.gui2.Window in project tests by datanucleus.
the class ManagedRelationshipTest method testOneToManyJoinBidirDeleteElement.
/**
* Test for management of relations with a 1-N jointable bidir where an element is deleted, so we make
* sure that it is deleted from the collection too.
*/
public void testOneToManyJoinBidirDeleteElement() {
try {
// Persist the objects so we have them
PersistenceManager pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
Transaction tx = pm.currentTransaction();
Object houseId = null;
Object window1Id = null;
Object window2Id = null;
try {
tx.begin();
House house = new House(15, "King Street");
Window window1 = new Window(150, 200, null);
Window window2 = new Window(350, 400, null);
house.addWindow(window1);
house.addWindow(window2);
window1.setHouse(house);
window2.setHouse(house);
pm.makePersistent(house);
tx.commit();
houseId = pm.getObjectId(house);
window1Id = pm.getObjectId(window1);
window2Id = pm.getObjectId(window2);
} catch (Exception e) {
LOG.error("Exception thrown persisting 1-N JoinTable bidir", e);
fail("Error in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the objects and delete an element
pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
tx = pm.currentTransaction();
try {
tx.begin();
House house = (House) pm.getObjectById(houseId);
Window window1 = (Window) pm.getObjectById(window1Id);
Window window2 = (Window) pm.getObjectById(window2Id);
assertEquals("Window1 'house' is incorrect at retrieve", house, window1.getHouse());
assertEquals("Window2 'house' is incorrect at retrieve", house, window2.getHouse());
assertEquals("House has incorrect number of windows at retrieve", 2, house.getWindows().size());
// Delete Window1
pm.deletePersistent(window1);
pm.flush();
assertNotNull("House windows is null after window delete + flush!", house.getWindows());
assertEquals("House has incorrect number of windows after window delete", 1, house.getWindows().size());
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown deleting element from 1-N JoinTable bidir", e);
fail("Error in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(House.class);
clean(Window.class);
}
}
use of com.googlecode.lanterna.gui2.Window in project tests by datanucleus.
the class ManagedRelationshipTest method testOneToManyJoinBidirSetCollection.
/**
* Test for management of relations with a 1-N jointable bidir where an element is being removed
* from its owner by resetting its element collection,
* and check that both element and owner are updated accordingly.
*/
public void testOneToManyJoinBidirSetCollection() {
try {
// Persist the objects so we have them
PersistenceManager pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
Transaction tx = pm.currentTransaction();
Object house1Id = null;
Object window1Id = null;
Object window2Id = null;
try {
tx.begin();
House house1 = new House(15, "King Street");
Window window1 = new Window(150, 200, null);
Window window2 = new Window(350, 400, null);
house1.addWindow(window1);
house1.addWindow(window2);
pm.makePersistent(house1);
tx.commit();
house1Id = pm.getObjectId(house1);
window1Id = pm.getObjectId(window1);
window2Id = pm.getObjectId(window2);
} catch (Exception e) {
LOG.error("Exception thrown persisting 1-N JoinTable bidir", e);
fail("Error in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the objects and update an element
pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
tx = pm.currentTransaction();
try {
tx.begin();
House house1 = (House) pm.getObjectById(house1Id);
Window window1 = (Window) pm.getObjectById(window1Id);
Window window2 = (Window) pm.getObjectById(window2Id);
assertEquals("Window1 'house' is incorrect at retrieve", house1, window1.getHouse());
assertEquals("Window2 'house' is incorrect at retrieve", house1, window2.getHouse());
assertEquals("House1 has incorrect number of windows at retrieve", 2, house1.getWindows().size());
// Update House1 to have just window2
house1.setWindows(Collections.singleton(window2));
pm.flush();
assertEquals("House1 has incorrect number of windows after window update", 1, house1.getWindows().size());
assertFalse("House1 contains window1 after update but shouldnt", house1.getWindows().contains(window1));
assertTrue("House1 should still contain window2 after update", house1.getWindows().contains(window2));
assertEquals("Window1 'house' is incorrect after update", null, window1.getHouse());
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown updating element from 1-N JoinTable bidir", e);
fail("Error in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the objects and check
pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "true");
tx = pm.currentTransaction();
try {
tx.begin();
House house1 = (House) pm.getObjectById(house1Id);
Window window1 = (Window) pm.getObjectById(window1Id);
Window window2 = (Window) pm.getObjectById(window2Id);
assertEquals("Window1 'house' is incorrect at retrieve", null, window1.getHouse());
assertEquals("Window2 'house' is incorrect at retrieve", house1, window2.getHouse());
assertEquals("House1 has incorrect number of windows at retrieve", 1, house1.getWindows().size());
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown updating element from 1-N JoinTable bidir", e);
fail("Error in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(House.class);
clean(Window.class);
}
}
Aggregations