Search in sources :

Example 6 with Window

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);
    }
}
Also used : Window(org.datanucleus.samples.one_many.bidir_2.Window) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) House(org.datanucleus.samples.one_many.bidir_2.House)

Example 7 with Window

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);
    }
}
Also used : Window(org.datanucleus.samples.one_many.bidir_2.Window) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) House(org.datanucleus.samples.one_many.bidir_2.House)

Example 8 with Window

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);
    }
}
Also used : Window(org.datanucleus.samples.one_many.bidir_2.Window) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) House(org.datanucleus.samples.one_many.bidir_2.House)

Example 9 with Window

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);
    }
}
Also used : Window(org.datanucleus.samples.one_many.bidir_2.Window) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) House(org.datanucleus.samples.one_many.bidir_2.House)

Example 10 with Window

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);
    }
}
Also used : Window(org.datanucleus.samples.one_many.bidir_2.Window) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) House(org.datanucleus.samples.one_many.bidir_2.House)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)12 Transaction (javax.jdo.Transaction)12 House (org.datanucleus.samples.one_many.bidir_2.House)12 Window (org.datanucleus.samples.one_many.bidir_2.Window)12 List (java.util.List)7 ArrayList (java.util.ArrayList)6 MessageDialogButton (com.googlecode.lanterna.gui2.dialogs.MessageDialogButton)4 Button (com.googlecode.lanterna.gui2.Button)3 GridLayout (com.googlecode.lanterna.gui2.GridLayout)3 Panel (com.googlecode.lanterna.gui2.Panel)3 Query (javax.jdo.Query)3 EntityManager (javax.persistence.EntityManager)3 EntityTransaction (javax.persistence.EntityTransaction)3 House (org.datanucleus.samples.annotations.one_many.bidir_2.House)3 Window (org.datanucleus.samples.annotations.one_many.bidir_2.Window)3 Board (cardgame1.Board)2 Window (cardgame1.Window)2 TerminalSize (com.googlecode.lanterna.TerminalSize)2 Label (com.googlecode.lanterna.gui2.Label)2 Window (com.googlecode.lanterna.gui2.Window)2