Search in sources :

Example 1 with ElectronicJournal

use of org.jpox.samples.one_one.bidir_3.ElectronicJournal in project tests by datanucleus.

the class JDOQLBasicTest method testInstanceof.

/**
 * Test case to use JDOQL "instanceof"
 */
public void testInstanceof() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // 1. instanceof where the candidate class uses "new-table" (union)
            tx.begin();
            Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
            Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
            // Eh, what's up, doc?
            Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
            // Meep! Meep!
            Manager roadrunner = new Manager(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            pm.makePersistent(bunny);
            pm.makePersistent(roadrunner);
            tx.commit();
            tx.begin();
            try {
                Query q = pm.newQuery(Employee.class, "this instanceof " + Manager.class.getName());
                List results = (List) q.execute();
                assertEquals("Number of objects returned from instanceof was incorrect", results.size(), 1);
                Object obj = results.get(0);
                assertTrue("Type of object returned should have been Manager but wasnt", obj instanceof Manager);
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            tx.commit();
            // 2. instanceof where the candidate class using "superclass-table" (discriminator)
            tx.begin();
            ElectronicJournal elecJournal = new ElectronicJournal(1, "Electronics Weekly");
            PrintJournal printJournal = new PrintJournal(2, "Glossy magazine");
            pm.makePersistent(elecJournal);
            pm.makePersistent(printJournal);
            tx.commit();
            tx.begin();
            try {
                Query q = pm.newQuery(AbstractJournal.class, "this instanceof " + PrintJournal.class.getName());
                List results = (List) q.execute();
                assertEquals("Number of objects returned from instanceof was incorrect", results.size(), 1);
                Object obj = results.get(0);
                assertTrue("Type of object returned should have been PrintJournal but wasnt", obj instanceof PrintJournal);
                q.closeAll();
            } catch (JDOUserException e) {
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(PrintJournal.class);
        clean(ElectronicJournal.class);
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ElectronicJournal(org.jpox.samples.one_one.bidir_3.ElectronicJournal) List(java.util.List) ArrayList(java.util.ArrayList) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) PrintJournal(org.jpox.samples.one_one.bidir_3.PrintJournal)

Example 2 with ElectronicJournal

use of org.jpox.samples.one_one.bidir_3.ElectronicJournal in project tests by datanucleus.

the class RelationshipTest method test1to1BidirInheritanceSuperclassTable.

/**
 * Test case for a bidirectional 1-1 relationship to a class using "superclass-table" inheritance strategy,
 * with both classes involved being mapped to the same superclass table.
 */
public void test1to1BidirInheritanceSuperclassTable() throws Exception {
    try {
        // Create the necessary schema
        try {
            addClassesToSchema(new Class[] { AbstractJournal.class, PrintJournal.class, ElectronicJournal.class });
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail("Exception thrown while adding classes for 1-1 relation using superclass-table : " + e.getMessage());
        }
        // Check the persistence of data
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object pjId = null;
        Object ejId = null;
        try {
            tx.begin();
            PrintJournal pj = new PrintJournal(1, "The PrintJournal");
            ElectronicJournal ej = new ElectronicJournal(2, "The ElectronicJournal");
            pm.makePersistent(pj);
            pm.makePersistent(ej);
            pj.setElectronicJournal(ej);
            ej.setPrintJournal(pj);
            tx.commit();
            pjId = pm.getObjectId(pj);
            ejId = pm.getObjectId(ej);
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check the retrieval of the data
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            PrintJournal pj = (PrintJournal) pm.getObjectById(pjId, true);
            ElectronicJournal ej = (ElectronicJournal) pm.getObjectById(ejId, true);
            assertTrue("ej.getPrintJournal() must not be null", ej.getPrintJournal() != null);
            assertTrue("pj.getElectronicJournal() must not be null", pj.getElectronicJournal() != null);
            assertTrue("PrintJournal has wrong associated ElectronicJournal", pj.getElectronicJournal() == ej);
            assertTrue("ElectronicJournal has wrong associated PrintJournal", ej.getPrintJournal() == pj);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check a query
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q1 = pm.newQuery(PrintJournal.class, "electronicJournal != null");
            List results1 = (List) q1.execute();
            assertEquals("Number of PrintJournals with ElectronicJournal was incorrect", results1.size(), 1);
            Query q2 = pm.newQuery(ElectronicJournal.class, "printJournal != null");
            List results2 = (List) q2.execute();
            assertEquals("Number of ElectronicJournals with PrintJournal was incorrect", results2.size(), 1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out all data
        clean(PrintJournal.class);
        clean(ElectronicJournal.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) ElectronicJournal(org.jpox.samples.one_one.bidir_3.ElectronicJournal) List(java.util.List) ArrayList(java.util.ArrayList) PrintJournal(org.jpox.samples.one_one.bidir_3.PrintJournal) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 PersistenceManager (javax.jdo.PersistenceManager)2 Query (javax.jdo.Query)2 Transaction (javax.jdo.Transaction)2 ElectronicJournal (org.jpox.samples.one_one.bidir_3.ElectronicJournal)2 PrintJournal (org.jpox.samples.one_one.bidir_3.PrintJournal)2 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 JDOUserException (javax.jdo.JDOUserException)1 StoreManager (org.datanucleus.store.StoreManager)1 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)1 Employee (org.jpox.samples.models.company.Employee)1 Manager (org.jpox.samples.models.company.Manager)1