Search in sources :

Example 1 with ConcreteSimpleSub2

use of org.jpox.samples.abstractclasses.ConcreteSimpleSub2 in project tests by datanucleus.

the class XMLTest method testMappedSuperclass.

/**
 * Test of MappedSuperclass
 */
public void testMappedSuperclass() {
    NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
    ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
    MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
    PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
    metaDataMgr.loadPersistenceUnit(pumd, null);
    // AbstractSimpleBase
    ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(AbstractSimpleBase.class.getName(), clr);
    assertNotNull("No MetaData found for AbstractSimpleBase yet is MappedSuperclass", cmd);
    assertNotNull("No Inheritance info found for AbstractSimpleBase", cmd.getInheritanceMetaData());
    assertEquals("Inheritance for AbstractSimpleBase is incorrect", "subclass-table", cmd.getInheritanceMetaData().getStrategy().toString());
    AbstractMemberMetaData fmd = cmd.getMetaDataForMember("id");
    assertNotNull("No field info found for AbstractSimpleBase.id", fmd);
    assertNotNull("No column info found for AbstractSimpleBase.id", fmd.getColumnMetaData());
    assertEquals("Column name for AbstractSimpleBase.id is wrong", "ID", fmd.getColumnMetaData()[0].getName());
    fmd = cmd.getMetaDataForMember("baseField");
    assertNotNull("No field info found for AbstractSimpleBase.baseField", fmd);
    assertNotNull("No column info found for AbstractSimpleBase.baseField", fmd.getColumnMetaData());
    assertEquals("Column name for Product.baseField is wrong", "BASE_FIELD", fmd.getColumnMetaData()[0].getName());
    // ConcreteSimpleSub1
    cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(ConcreteSimpleSub1.class.getName(), clr);
    assertNotNull("No MetaData found for ConcreteSimpleSub1 yet is Entity", cmd);
    assertNotNull("No Inheritance info found for ConcreteSimpleSub1", cmd.getInheritanceMetaData());
    assertEquals("Inheritance for ConcreteSimpleSub1 is incorrect", "new-table", cmd.getInheritanceMetaData().getStrategy().toString());
    fmd = cmd.getOverriddenMember("baseField");
    assertNotNull("No overridden field info found for ConcreteSimpleSub1.baseField", fmd);
    assertNotNull("No column info found for ConcreteSimpleSub1.baseField", fmd.getColumnMetaData());
    assertEquals("Column name for ConcreteSimpleSub1.baseField is wrong", "BASE_FIELD_OR", fmd.getColumnMetaData()[0].getName());
    fmd = cmd.getMetaDataForMember("sub1Field");
    assertNotNull("No field info found for ConcreteSimpleSub1.sub1Field", fmd);
    assertNotNull("No column info found for ConcreteSimpleSub1.sub1Field", fmd.getColumnMetaData());
    assertEquals("Column name for ConcreteSimpleSub1.sub1Field is wrong", "SUB1_FIELD", fmd.getColumnMetaData()[0].getName());
    // ConcreteSimpleSub2
    cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(ConcreteSimpleSub2.class.getName(), clr);
    assertNotNull("No MetaData found for ConcreteSimpleSub2 yet is Entity", cmd);
    assertNotNull("No Inheritance info found for ConcreteSimpleSub2", cmd.getInheritanceMetaData());
    assertEquals("Inheritance for ConcreteSimpleSub2 is incorrect", "new-table", cmd.getInheritanceMetaData().getStrategy().toString());
    fmd = cmd.getOverriddenMember("baseField");
    assertNull("Overridden field info found for ConcreteSimpleSub2.baseField!", fmd);
    fmd = cmd.getMetaDataForMember("sub2Field");
    assertNotNull("No overridden field info found for ConcreteSimpleSub2.sub2Field", fmd);
    assertNotNull("No column info found for ConcreteSimpleSub2.sub2Field", fmd.getColumnMetaData());
    assertEquals("Column name for ConcreteSimpleSub2.sub2Field is wrong", "SUB2_FIELD", fmd.getColumnMetaData()[0].getName());
}
Also used : ConcreteSimpleSub2(org.jpox.samples.abstractclasses.ConcreteSimpleSub2) ConcreteSimpleSub1(org.jpox.samples.abstractclasses.ConcreteSimpleSub1) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 2 with ConcreteSimpleSub2

use of org.jpox.samples.abstractclasses.ConcreteSimpleSub2 in project tests by datanucleus.

the class AbstractClassesTest method perform1to1CreationSimple.

/*
    */
/**
 * Check the persistence of abstract objects and their holders.
 */
/*
    public void testCreationComposite()
    throws Exception
    {
        try
        {
            perform1to1CreationComposite();
        }
        finally
        {
            clean(AbstractCompositeClassHolder.class);
            clean(ConcreteCompositeSub1.class);
            clean(ConcreteCompositeSub2.class);
        }
    }

    */
/**
 * Test of the retrieval of the abstract contained objects and of the
 * holder with its related abstract object.
 */
/*
    public void testRetrievalComposite()
    throws Exception
    {
        try
        {
            perform1to1CreationComposite();
            perform1to1Retrieval(AbstractCompositeClassHolder.class, AbstractCompositeBase.class, 
                ConcreteCompositeSub1.class, ConcreteCompositeSub2.class);
        }
        finally
        {
            clean(AbstractCompositeClassHolder.class);
            clean(ConcreteCompositeSub1.class);
            clean(ConcreteCompositeSub2.class);
        }
    }

    */
/**
 * Test of the query of the abstract contained objects and of the
 * holder with its related abstract object.
 */
/*
    public void testQueryComposite()
    throws Exception
    {
        try
        {
            perform1to1CreationComposite();
            perform1to1Query(AbstractCompositeClassHolder.class, AbstractCompositeBase.class,
                ConcreteCompositeSub2.class, ConcreteCompositeSub2.class);
        }
        finally
        {
            clean(AbstractCompositeClassHolder.class);
            clean(ConcreteCompositeSub1.class);
            clean(ConcreteCompositeSub2.class);
        }
    }

    */
/**
 * Test of the deletion of the abstract contained objects and its holder.
 */
/*
    public void testDeletionComposite()
    throws Exception
    {
        try
        {
            perform1to1CreationComposite();
            perform1to1Deletion(AbstractCompositeClassHolder.class, AbstractCompositeBase.class, 
                ConcreteCompositeSub1.class, ConcreteCompositeSub2.class);
        }
        finally
        {
            clean(AbstractSimpleClassHolder.class);
            clean(ConcreteSimpleSub1.class);
            clean(ConcreteSimpleSub2.class);
        }
    }

    */
/**
 * Test for having abstract elements in a join-table Set, and creating container/elements.
 */
/*
    public void testJoinTableSetCreationComposite()
    throws Exception
    {
        try
        {
            perform1toNJoinTableSetCreationComposite();
        }
        finally
        {
            clean(AbstractCompositeClassHolder.class);
            clean(AbstractCompositeBase.class);
            clean(ConcreteCompositeSub1.class);
            clean(ConcreteCompositeSub2.class);
        }
    }

    */
/**
 * Test of the retrieval of the abstract contained objects within a Set.
 */
/*
    public void testJoinTableSetRetrievalComposite()
    throws Exception
    {
        try
        {
            perform1toNJoinTableSetCreationComposite();
            perform1toNJoinTableSetRetrieval(AbstractCompositeClassHolder.class);
        }
        finally
        {
            clean(AbstractCompositeClassHolder.class);
            clean(AbstractCompositeBase.class);
            clean(ConcreteCompositeSub1.class);
            clean(ConcreteCompositeSub2.class);
        }
    }

    */
/**
 * Test for having abstract elements in a join-table List, and creating container/elements.
 */
/*
    public void testJoinTableListCreationComposite()
    throws Exception
    {
        try
        {
            perform1toNJoinTableListCreationComposite();
        }
        finally
        {
            clean(AbstractCompositeClassHolder.class);
            clean(AbstractCompositeBase.class);
            clean(ConcreteCompositeSub1.class);
            clean(ConcreteCompositeSub2.class);
        }
    }

    */
/**
 * Test of the retrieval of the abstract contained objects within a List.
 */
/*
    public void testJoinTableListRetrievalComposite()
    throws Exception
    {
        try
        {
            perform1toNJoinTableListCreationComposite();
            perform1toNJoinTableListRetrieval(AbstractCompositeClassHolder.class,
                ConcreteCompositeSub1.class, ConcreteCompositeSub2.class);
        }
        finally
        {
            clean(AbstractCompositeClassHolder.class);
            clean(AbstractCompositeBase.class);
            clean(ConcreteCompositeSub1.class);
            clean(ConcreteCompositeSub2.class);
        }
    }
*/
// ------------------------------------------- Utility Methods ---------------------------------------------
/**
 * Convenience method to create 2 holders with ConcreteSimpleSub1, ConcreteSimpleSub2 related objects.
 */
private void perform1to1CreationSimple() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Persist 2 1-1 relations using both concrete subclass types
        tx.begin();
        AbstractSimpleClassHolder holder1 = new AbstractSimpleClassHolder(1);
        ConcreteSimpleSub1 sub1 = new ConcreteSimpleSub1(1);
        sub1.setBaseField("Base1");
        sub1.setSub1Field("Sub1");
        holder1.setAbstract1(sub1);
        pm.makePersistent(holder1);
        AbstractSimpleClassHolder holder2 = new AbstractSimpleClassHolder(2);
        ConcreteSimpleSub2 sub2 = new ConcreteSimpleSub2(2);
        sub2.setBaseField("Base2");
        sub2.setSub2Field("Sub2");
        holder2.setAbstract1(sub2);
        pm.makePersistent(holder2);
        tx.commit();
        assertTrue("Id of Abstract object ConcreteSimpleSub1 was null!", pm.getObjectId(sub1) != null);
        assertTrue("Id of Abstract object ConcreteSimpleSub2 was null!", pm.getObjectId(sub1) != null);
        assertTrue("Id of holder1 was null!", pm.getObjectId(holder1) != null);
        assertTrue("Id of holder2 was null!", pm.getObjectId(holder2) != null);
    } catch (Exception e) {
        LOG.error("Exception thrown persisting objects with abstract superclass", e);
        e.printStackTrace();
        fail("Exception thrown during persistence : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : ConcreteSimpleSub2(org.jpox.samples.abstractclasses.ConcreteSimpleSub2) ConcreteSimpleSub1(org.jpox.samples.abstractclasses.ConcreteSimpleSub1) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) AbstractSimpleClassHolder(org.jpox.samples.abstractclasses.AbstractSimpleClassHolder) JDOUserException(javax.jdo.JDOUserException)

Example 3 with ConcreteSimpleSub2

use of org.jpox.samples.abstractclasses.ConcreteSimpleSub2 in project tests by datanucleus.

the class AbstractClassesTest method perform1toNJoinTableSetCreationSimple.

/**
 * Convenience method to persist a holder with 2 elements in the set field.
 */
private void perform1toNJoinTableSetCreationSimple() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Persist holder with 2 elements in the Set field
        tx.begin();
        AbstractSimpleClassHolder holder = new AbstractSimpleClassHolder(1);
        ConcreteSimpleSub1 sub1 = new ConcreteSimpleSub1(1);
        sub1.setBaseField("Base1");
        sub1.setSub1Field("Sub1");
        holder.getAbstractSet1().add(sub1);
        ConcreteSimpleSub2 sub2 = new ConcreteSimpleSub2(2);
        sub2.setBaseField("Base2");
        sub2.setSub2Field("Sub2");
        holder.getAbstractSet1().add(sub2);
        pm.makePersistent(holder);
        tx.commit();
        assertTrue("Id of Abstract object ConcreteSimpleSub1 was null!", pm.getObjectId(sub1) != null);
        assertTrue("Id of Abstract object ConcreteSimpleSub2 was null!", pm.getObjectId(sub1) != null);
        assertTrue("Id of holder was null!", pm.getObjectId(holder) != null);
    } catch (Exception e) {
        LOG.error("Exception thrown persisting objects with abstract superclass", e);
        e.printStackTrace();
        fail("Exception thrown during persistence : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : ConcreteSimpleSub2(org.jpox.samples.abstractclasses.ConcreteSimpleSub2) ConcreteSimpleSub1(org.jpox.samples.abstractclasses.ConcreteSimpleSub1) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) AbstractSimpleClassHolder(org.jpox.samples.abstractclasses.AbstractSimpleClassHolder) JDOUserException(javax.jdo.JDOUserException)

Example 4 with ConcreteSimpleSub2

use of org.jpox.samples.abstractclasses.ConcreteSimpleSub2 in project tests by datanucleus.

the class AbstractClassesTest method perform1toNJoinTableListCreationSimple.

/**
 * Convenience method to persist a holder with 2 elements in the list field.
 */
private void perform1toNJoinTableListCreationSimple() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Persist holder with 2 elements in the List field
        tx.begin();
        AbstractSimpleClassHolder holder = new AbstractSimpleClassHolder(1);
        ConcreteSimpleSub1 sub1 = new ConcreteSimpleSub1(1);
        sub1.setBaseField("Base1");
        sub1.setSub1Field("Sub1");
        holder.getAbstractList1().add(sub1);
        ConcreteSimpleSub2 sub2 = new ConcreteSimpleSub2(2);
        sub2.setBaseField("Base2");
        sub2.setSub2Field("Sub2");
        holder.getAbstractList1().add(sub2);
        pm.makePersistent(holder);
        tx.commit();
        assertTrue("Id of Abstract object ConcreteSimpleSub1 was null!", pm.getObjectId(sub1) != null);
        assertTrue("Id of Abstract object ConcreteSimpleSub2 was null!", pm.getObjectId(sub1) != null);
        assertTrue("Id of holder was null!", pm.getObjectId(holder) != null);
    } catch (Exception e) {
        LOG.error("Exception thrown persisting objects with abstract superclass", e);
        e.printStackTrace();
        fail("Exception thrown during persistence : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : ConcreteSimpleSub2(org.jpox.samples.abstractclasses.ConcreteSimpleSub2) ConcreteSimpleSub1(org.jpox.samples.abstractclasses.ConcreteSimpleSub1) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) AbstractSimpleClassHolder(org.jpox.samples.abstractclasses.AbstractSimpleClassHolder) JDOUserException(javax.jdo.JDOUserException)

Aggregations

ConcreteSimpleSub1 (org.jpox.samples.abstractclasses.ConcreteSimpleSub1)4 ConcreteSimpleSub2 (org.jpox.samples.abstractclasses.ConcreteSimpleSub2)4 JDOUserException (javax.jdo.JDOUserException)3 PersistenceManager (javax.jdo.PersistenceManager)3 Transaction (javax.jdo.Transaction)3 AbstractSimpleClassHolder (org.jpox.samples.abstractclasses.AbstractSimpleClassHolder)3 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 NucleusContext (org.datanucleus.NucleusContext)1 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)1 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)1 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)1 ClassMetaData (org.datanucleus.metadata.ClassMetaData)1 MetaDataManager (org.datanucleus.metadata.MetaDataManager)1 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)1