Search in sources :

Example 1 with Magazine

use of org.jpox.samples.one_one.unidir_2.Magazine in project tests by datanucleus.

the class ExtentTest method testExtentOfSubclassTable.

/**
 * Test the use of extents with a class using "subclass-table" inheritance strategy.
 */
public void testExtentOfSubclassTable() throws Exception {
    try {
        // Create some objects.
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Newspaper paper = new Newspaper("Daily Vermin", 2, "George Bush", "Sleaze");
            Magazine mag = new Magazine("Buenos Dias", 4, "Cachorro sa");
            pm.makePersistent(paper);
            pm.makePersistent(mag);
            tx.commit();
        } catch (JDOUserException ue) {
            assertTrue("Exception thrown during create of objects using subclass-table inheritance", false);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Try to get extent of ExtentSub (superclass of both objects, using subclass-table)
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent<MediaWork> e = pm.getExtent(MediaWork.class, true);
            int number = 0;
            Iterator<MediaWork> iter = e.iterator();
            while (iter.hasNext()) {
                iter.next();
                number++;
            }
            assertEquals("Extent for classes with subclass-table inheritance strategy returned incorrect number of objects", number, 2);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception thrown for Extent of class using subclass-table", e);
            e.printStackTrace();
            fail("Exception was thrown when requesting Extent of class using subclass-table inheritance strategy");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out created data
        clean(Newspaper.class);
        clean(Magazine.class);
    }
}
Also used : Newspaper(org.jpox.samples.one_one.unidir_2.Newspaper) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) MediaWork(org.jpox.samples.one_one.unidir_2.MediaWork) Magazine(org.jpox.samples.one_one.unidir_2.Magazine) JDOUserException(javax.jdo.JDOUserException)

Example 2 with Magazine

use of org.jpox.samples.one_one.unidir_2.Magazine in project tests by datanucleus.

the class RelationshipTest method test1to1UnidirInheritanceSubclassTable.

/**
 * Test case for 1-1 bidirectional relationships using 2 FK's.
 * This is really tested as BasicQuery and DatastoreId test.
 */
/*public void test1to1BidirectionalMultiFK()
    throws Exception
    {
        try
        {
            Object idUser = null;
            Object idUserDetail = null;
            
            PersistenceManager pm=pmf.getPersistenceManager();
            Transaction tx=pm.currentTransaction();
            
            // Create sample data
            try
            {
                tx.begin();
                
                User user=new User("andy","password");
                UserDetails details=new UserDetails("Andy","Jefferson");
                user.setDetails(details);
                details.setUser(user);
                
                pm.makePersistent(user);
                
                tx.commit();
                idUser = pm.getObjectId(user);
                idUserDetail = pm.getObjectId(details);
            }
            catch (Exception e)
            {
                LOG.error(e);
                fail("Exception thrown while creating 1-1 bidirectional relationship data : " + e.getMessage());
            }
            finally
            {
                if (tx.isActive())
                {
                    tx.rollback();
                }
                
                pm.close();
            }
            
            pm=pmf.getPersistenceManager();
            tx=pm.currentTransaction();
            
            // Create sample data
            try
            {
                tx.begin();
                
                User user=(User)pm.getObjectById(idUser,true);
                UserDetails details=(UserDetails)pm.getObjectById(idUserDetail,true);
                assertEquals(details,user.getUserDetails());
                assertEquals(user,details.getUser());
                tx.commit();
            }
            catch (Exception e)
            {
                LOG.error(e);
                fail("Exception thrown while creating 1-1 bidirectional relationship data : " + e.getMessage());
            }
            finally
            {
                if (tx.isActive())
                {
                    tx.rollback();
                }
                
                pm.close();
            }
        }
        finally
        {
            clean(User.class);
            clean(UserDetails.class);
        }
    }*/
/**
 * Test case for 1-1 uni relationship to a class using "subclass-table" inheritance strategy. See JIRA "NUCRDBMS-18"
 */
public void test1to1UnidirInheritanceSubclassTable() throws Exception {
    try {
        // Create the necessary schema
        try {
            addClassesToSchema(new Class[] { Newspaper.class, Magazine.class, MediaWork.class, Reader.class });
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while adding classes for 1-1 relation using subclass-table : " + e.getMessage());
        }
        // Check the persistence of data
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object fredId = null;
        Object pamId = null;
        try {
            tx.begin();
            Magazine hello = new Magazine("Hello", MediaWork.FREQ_WEEKLY, "Trash Publishers");
            Newspaper mail = new Newspaper("Daily Mail", MediaWork.FREQ_DAILY, "Piers Morgan", "Tabloid");
            Reader fred = new Reader("Fred Smith", mail);
            Reader pam = new Reader("Pam Green", hello);
            pm.makePersistent(fred);
            pm.makePersistent(pam);
            tx.commit();
            fredId = pm.getObjectId(fred);
            pamId = pm.getObjectId(pam);
        } catch (Exception e) {
            e.printStackTrace();
            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();
            Reader fred = (Reader) pm.getObjectById(fredId, true);
            assertTrue("Fred has the wrong name!", fred.getName().equals("Fred Smith"));
            assertTrue("Fred has the wrong type of material", fred.getMaterial() instanceof Newspaper);
            assertTrue("Fred has the wrong material", fred.getMaterial().getName().equals("Daily Mail"));
            Reader pam = (Reader) pm.getObjectById(pamId, true);
            assertTrue("Pam has the wrong name", pam.getName().equals("Pam Green"));
            assertTrue("Pam has the wrong type of material", pam.getMaterial() instanceof Magazine);
            assertTrue("Pam has the wrong material", pam.getMaterial().getName().equals("Hello"));
            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(Reader.class, "((org.jpox.samples.one_one.unidir_2.Magazine)material).name == \"Hello\"");
            List results1 = (List) q1.execute();
            assertEquals("Number of readers who read \"Hello\" magazine was incorrect", results1.size(), 1);
            Query q2 = pm.newQuery(Reader.class, "((org.jpox.samples.one_one.unidir_2.Newspaper)material).name == \"Daily Mail\"");
            List results2 = (List) q2.execute();
            assertEquals("Number of readers who read \"Daily Mail\" newspaper was incorrect", results2.size(), 1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out all data
        clean(Reader.class);
        clean(Newspaper.class);
        clean(Magazine.class);
    }
}
Also used : Newspaper(org.jpox.samples.one_one.unidir_2.Newspaper) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Reader(org.jpox.samples.one_one.unidir_2.Reader) List(java.util.List) ArrayList(java.util.ArrayList) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) Magazine(org.jpox.samples.one_one.unidir_2.Magazine)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)2 Transaction (javax.jdo.Transaction)2 Magazine (org.jpox.samples.one_one.unidir_2.Magazine)2 Newspaper (org.jpox.samples.one_one.unidir_2.Newspaper)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 JDOUserException (javax.jdo.JDOUserException)1 Query (javax.jdo.Query)1 MediaWork (org.jpox.samples.one_one.unidir_2.MediaWork)1 Reader (org.jpox.samples.one_one.unidir_2.Reader)1