Search in sources :

Example 16 with Window

use of com.googlecode.lanterna.gui2.Window in project tests by datanucleus.

the class ManagedRelationshipTest method testOneToManyJoinBidirUnsetOwner.

/**
 * Test for management of relations with a 1-N jointable bidir where an element's owner is being removed
 * (set to null), and check that both element and owner are updated accordingly.
 */
public void testOneToManyJoinBidirUnsetOwner() {
    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 Window1 to have no house
            window1.setHouse(null);
            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));
            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)

Example 17 with Window

use of com.googlecode.lanterna.gui2.Window in project tests by datanucleus.

the class ManagedRelationshipTest method testOneToManyJoinBidirRemoveElement.

/**
 * Test for management of relations with a 1-N jointable bidir where an element is being removed
 * from its owner, and check that both element and owner are updated accordingly.
 */
public void testOneToManyJoinBidirRemoveElement() {
    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 Window1 no to belong to House1 anymore
            house1.removeWindow(window1);
            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));
            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)

Example 18 with Window

use of com.googlecode.lanterna.gui2.Window in project tests by datanucleus.

the class ManagedRelationshipTest method testOneToManyJoinBidirSetCollectionMoveElement.

/**
 * Test for management of relations with a 1-N jointable bidir where an element is being moved
 * from one collection owner to another, by setting a collection containing that element
 * on a new owner.
 * See NUCCORE-291
 * @see {@link #testOneToManyFKBidirSetCollectionMoveElement()}
 */
public void testOneToManyJoinBidirSetCollectionMoveElement() {
    PersistenceManager pm = null;
    Transaction tx = null;
    try {
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        // Create object graph:
        // house1 <-> {window1, window2}
        // house2 <-> {window3, window4}
        // house5 <-> {window5}
        tx.begin();
        House house1 = new House(1, "house1");
        Window window1 = new Window("window1");
        Window window2 = new Window("window2");
        house1.setWindows(createSet(window1, window2));
        pm.makePersistent(house1);
        House house2 = new House(2, "house2");
        Window window3 = new Window("window3");
        Window window4 = new Window("window4");
        house2.setWindows(createSet(window3, window4));
        pm.makePersistent(house2);
        House house3 = new House(3, "house3");
        Window window5 = new Window("window5");
        house3.setWindows(createSet(window5));
        pm.makePersistent(house3);
        pm.flush();
        // validate objectgraph
        assertEquals(createSet(window1, window2), house1.getWindows());
        assertEquals(house1, window1.getHouse());
        assertEquals(house1, window2.getHouse());
        assertEquals(createSet(window3, window4), house2.getWindows());
        assertEquals(house2, window3.getHouse());
        assertEquals(house2, window4.getHouse());
        assertEquals(createSet(window5), house3.getWindows());
        assertEquals(house3, window5.getHouse());
        tx.commit();
        // perform update and validate
        tx.begin();
        house1.setWindows(createSet(window2, window3, window5));
        pm.flush();
        // should result in:
        // house1 <-> {window2, window3, window5}
        // house2 <-> {window4}
        // house3 <-> {}
        // i.e. window3 and window5 moved from their previous owners to house1
        assertEquals(createSet(window2, window3, window5), house1.getWindows());
        assertEquals(house1, window2.getHouse());
        assertEquals(house1, window3.getHouse());
        assertEquals(house1, window5.getHouse());
        assertEquals(createSet(window4), house2.getWindows());
        assertEquals(house2, window4.getHouse());
        assertEquals(Collections.EMPTY_SET, house3.getWindows());
        tx.commit();
    } finally {
        try {
            if (tx != null && tx.isActive()) {
                tx.rollback();
            }
            if (pm != null) {
                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 19 with Window

use of com.googlecode.lanterna.gui2.Window in project tests by datanucleus.

the class JPQLQueryTest method testInnerJoinManyToOneBiJoinTableElement.

/**
 * Test for Inner Join N-1 bidirectional JoinTable relation from the element side.
 */
public void testInnerJoinManyToOneBiJoinTableElement() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            House house = new House(101, "Coronation Street");
            Window window = new Window(200, 400, house);
            house.getWindows().add(window);
            em.persist(house);
            em.flush();
            List result = em.createQuery("SELECT W FROM " + Window.class.getName() + " W INNER JOIN W.house H").getResultList();
            assertEquals(1, result.size());
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(House.class);
        clean(Window.class);
    }
}
Also used : Window(org.datanucleus.samples.annotations.one_many.bidir_2.Window) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) House(org.datanucleus.samples.annotations.one_many.bidir_2.House) List(java.util.List) ArrayList(java.util.ArrayList)

Example 20 with Window

use of com.googlecode.lanterna.gui2.Window in project tests by datanucleus.

the class RelationshipsTest method testOneToManyJoinTableSet.

/**
 * Test of 1-N JoinTable bidir relation with set.
 */
public void testOneToManyJoinTableSet() {
    try {
        EntityManager em = getEM();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            House house1 = new House(15, "High Street");
            Window win1 = new Window(100, 200, house1);
            house1.getWindows().add(win1);
            Window win2 = new Window(400, 200, house1);
            house1.getWindows().add(win2);
            em.persist(house1);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while creating House+Windows");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
        // Check the contents of the datastore
        em = getEM();
        tx = em.getTransaction();
        try {
            tx.begin();
            List result = em.createQuery("SELECT Object(T) FROM " + House.class.getName() + " T").getResultList();
            assertEquals(1, result.size());
            House house = (House) result.get(0);
            Collection<Window> windows = house.getWindows();
            assertEquals("Number of Windows in House is incorrect", 2, windows.size());
            Iterator<Window> iter = windows.iterator();
            boolean hasWin1 = false;
            boolean hasWin2 = false;
            while (iter.hasNext()) {
                Window win = iter.next();
                if (win.getWidth() == 100 && win.getHeight() == 200) {
                    hasWin1 = true;
                } else if (win.getWidth() == 400 && win.getHeight() == 200) {
                    hasWin2 = true;
                }
            }
            assertTrue("First Window is not present in retrieved House", hasWin1);
            assertTrue("Second Window is not present in retrieved House", hasWin2);
            tx.rollback();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving House+Windows");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    } finally {
        clean(House.class);
        clean(Window.class);
    }
}
Also used : Window(org.datanucleus.samples.annotations.one_many.bidir_2.Window) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) House(org.datanucleus.samples.annotations.one_many.bidir_2.House) ArrayList(java.util.ArrayList) List(java.util.List)

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