Search in sources :

Example 41 with PersistenceUnitMetaData

use of org.datanucleus.metadata.PersistenceUnitMetaData in project tests by datanucleus.

the class AnnotationTest method testEventListeners.

/**
 * Test of EventListeners
 */
public void testEventListeners() {
    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);
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(WebSite.class.getName(), clr);
    // Example callbacks
    EventListenerMetaData elmd = cmd1.getListenerForClass(cmd1.getFullClassName());
    assertNotNull("Site didnt have its own class registered as an EventListener!", elmd);
    assertEquals("Site EventListener has incorrect method for prePersist callback", elmd.getClassName() + ".prePersist", elmd.getMethodNameForCallbackClass(PrePersist.class.getName()));
    assertEquals("Site EventListener has incorrect method for postPersist callback", elmd.getClassName() + ".postPersist", elmd.getMethodNameForCallbackClass(PostPersist.class.getName()));
    assertEquals("Site EventListener has incorrect method for postPersist callback", elmd.getClassName() + ".load", elmd.getMethodNameForCallbackClass(PostLoad.class.getName()));
    assertNull(elmd.getMethodNameForCallbackClass(PreRemove.class.getName()));
    // Example listener
    elmd = cmd1.getListenerForClass(MyListener.class.getName());
    assertNotNull("Site didnt have MyListener registered as an EventListener!", elmd);
    assertEquals("Site EventListener has incorrect method for prePersist callback", elmd.getClassName() + ".register", elmd.getMethodNameForCallbackClass(PostPersist.class.getName()));
    assertEquals("Site EventListener has incorrect method for postPersist callback", elmd.getClassName() + ".deregister", elmd.getMethodNameForCallbackClass(PreRemove.class.getName()));
    assertNull(elmd.getMethodNameForCallbackClass(PrePersist.class.getName()));
}
Also used : 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) EventListenerMetaData(org.datanucleus.metadata.EventListenerMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 42 with PersistenceUnitMetaData

use of org.datanucleus.metadata.PersistenceUnitMetaData in project tests by datanucleus.

the class XMLTest method testOneToManyBiJoin.

/**
 * Test of JPA 1-N unidir FK relation.
 * This is really a 1-N uni join since JPA doesnt support 1-N uni FK.
 */
/*public void testOneToManyUniFK()
    {
        NucleusContext pmfcontext = new NucleusContext(new PersistenceConfiguration(){});
        pmfcontext.setApi("JPA");
        ClassLoaderResolver clr = new ClassLoaderResolverImpl();
        MetaDataManager metaDataMgr = new JPAMetaDataManager(pmfcontext);
        PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
        metaDataMgr.initialise(pumd, clr);

        // owner side
        ClassMetaData cmd1 = (ClassMetaData)metaDataMgr.getMetaDataForClass(Site.class.getName(), clr);
        AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("offices");
        assertNotNull("Site.offices is null!", fmd1);
        assertEquals("Site.offices mapped-by is incorrect", fmd1.getMappedBy(), null);
        assertEquals("Site.offices relationType is incorrect",
            fmd1.getRelationType(clr), Relation.ONE_TO_MANY_UNI);
        assertEquals("Site.offices jointable name is incorrect", fmd1.getTable(), null);
        assertNotNull("Site.offices should have join but doesnt", fmd1.getJoinMetaData());

        ElementMetaData elemmd = fmd1.getElementMetaData();
        assertNotNull("Site.offices has no element column info but should", elemmd);
        ColumnMetaData[] colmds = elemmd.getColumnMetaData();
        assertNotNull("Site.offices has incorrect element columns", colmds);
        assertEquals("Site.offices has incorrect number of element columns", colmds.length, 1);
        assertEquals("Site.offices has incorrect element column name", colmds[0].getName(), "SITE_ID");
    }*/
/**
 * Test of JPA 1-N bidir JoinTable relation
 */
public void testOneToManyBiJoin() {
    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);
    // owner side
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Manager.class.getName(), clr);
    assertEquals("Manager has incorrect table name", cmd1.getTable(), "JPA_MD_MANAGER");
    AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("subordinates");
    assertNotNull("Manager.subordinates is null!", fmd1);
    assertEquals("Manager.subordinates mapped-by is incorrect", fmd1.getMappedBy(), "manager");
    assertEquals("Manager.subordinates relationType is incorrect", fmd1.getRelationType(clr), RelationType.ONE_TO_MANY_BI);
    assertEquals("Manager.subordinates jointable name is incorrect", fmd1.getTable(), "JPA_MD_MGR_EMPLOYEES");
    // non-owner side
    ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Employee.class.getName(), clr);
    assertEquals("Employee has incorrect table name", cmd2.getTable(), "JPA_MD_EMPLOYEE");
    AbstractMemberMetaData fmd2 = cmd2.getMetaDataForMember("manager");
    assertNotNull("Employee.manager is null!", fmd2);
    assertEquals("Employee.manager mapped-by is incorrect", fmd2.getMappedBy(), null);
    assertEquals("Employee.manager relationType is incorrect", fmd2.getRelationType(clr), RelationType.MANY_TO_ONE_BI);
    assertEquals("Employee.manager jointable name is incorrect", fmd2.getTable(), null);
    // join-table
    JoinMetaData joinmd = fmd1.getJoinMetaData();
    assertNotNull("Manager.subordinates has no join table!", joinmd);
    assertNotNull("Manager.subordinates has incorrect join columns", joinmd.getColumnMetaData());
    assertEquals("Manager.subordinates has incorrect number of join columns", 1, joinmd.getColumnMetaData().length);
    assertEquals("Manager.subordinates has incorrect owner join column name", "MGR_ID", joinmd.getColumnMetaData()[0].getName());
    ElementMetaData elemmd = fmd1.getElementMetaData();
    assertNotNull("Manager.subordinates has no element column info but should", elemmd);
    assertNotNull("Manager.subordinates has incorrect element columns", elemmd.getColumnMetaData());
    assertEquals("Manager.subordinates has incorrect number of element columns", 1, elemmd.getColumnMetaData().length);
    assertEquals("Manager.subordinates has incorrect element join column name", "EMP_ID", elemmd.getColumnMetaData()[0].getName());
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) ElementMetaData(org.datanucleus.metadata.ElementMetaData) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) JoinMetaData(org.datanucleus.metadata.JoinMetaData) AbstractMemberMetaData(org.datanucleus.metadata.AbstractMemberMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 43 with PersistenceUnitMetaData

use of org.datanucleus.metadata.PersistenceUnitMetaData in project tests by datanucleus.

the class XMLTest method testTableGenerator.

/**
 * Test of JPA <table-generator>
 */
public void testTableGenerator() {
    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);
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Employee.class.getName(), clr);
    PackageMetaData pmd = cmd1.getPackageMetaData();
    assertEquals("Number of TableGenerators registered for Employee class is wrong", pmd.getNoOfTableGenerators(), 1);
    TableGeneratorMetaData tgmd = pmd.getTableGenerators()[0];
    assertEquals("TableGenerator has incorrect name", tgmd.getName(), "EmployeeGenerator");
    assertEquals("TableGenerator has incorrect table", tgmd.getTableName(), "ID_TABLE");
    assertEquals("TableGenerator has incorrect pk column name", tgmd.getPKColumnName(), "TYPE");
    assertEquals("TableGenerator has incorrect value column name", tgmd.getValueColumnName(), "LATEST_VALUE");
    assertEquals("TableGenerator has incorrect pk column value", tgmd.getPKColumnValue(), "EMPLOYEE");
    assertEquals("TableGenerator has incorrect initial value", tgmd.getInitialValue(), 0);
    assertEquals("TableGenerator has incorrect allocation size", tgmd.getAllocationSize(), 50);
}
Also used : JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) PackageMetaData(org.datanucleus.metadata.PackageMetaData) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) JPAMetaDataManager(org.datanucleus.api.jpa.metadata.JPAMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) TableGeneratorMetaData(org.datanucleus.metadata.TableGeneratorMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 44 with PersistenceUnitMetaData

use of org.datanucleus.metadata.PersistenceUnitMetaData in project tests by datanucleus.

the class XMLTest method testNamedQuery.

/**
 * Test of JPA <named-query>, <named-native-query>
 */
public void testNamedQuery() {
    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);
    ClassMetaData cmd = (ClassMetaData) metaDataMgr.getMetaDataForClass(LoginAccount.class.getName(), clr);
    QueryMetaData[] qmds = cmd.getQueries();
    assertNotNull("LoginAccount has no queries!", qmds);
    assertEquals("LoginAccount has incorrect number of queries", 2, qmds.length);
    QueryMetaData jpqlQuery = null;
    QueryMetaData sqlQuery = null;
    if (qmds[0].getLanguage().equals(QueryLanguage.JPQL.toString())) {
        jpqlQuery = qmds[0];
    } else if (qmds[1].getLanguage().equals(QueryLanguage.JPQL.toString())) {
        jpqlQuery = qmds[1];
    }
    if (qmds[0].getLanguage().equals(QueryLanguage.SQL.toString())) {
        sqlQuery = qmds[0];
    } else if (qmds[1].getLanguage().equals(QueryLanguage.SQL.toString())) {
        sqlQuery = qmds[1];
    }
    if (jpqlQuery == null) {
        fail("No JPQL Query was registered for LoginAccount");
    }
    if (sqlQuery == null) {
        fail("No SQL Query was registered for LoginAccount");
    }
    assertEquals("LoginAccount JPQL has incorrect query name", "LoginForJohnSmith", jpqlQuery.getName());
    assertEquals("LoginAccount JPQL has incorrect query", "SELECT a FROM LoginAccount a WHERE a.firstName='John' AND a.lastName='Smith'", jpqlQuery.getQuery());
    assertEquals("LoginAccount SQL has incorrect query name", "LoginForJohn", sqlQuery.getName());
    assertEquals("LoginAccount SQL has incorrect query", "SELECT * FROM JPALOGIN WHERE FIRSTNAME = 'John'", sqlQuery.getQuery());
}
Also used : QueryMetaData(org.datanucleus.metadata.QueryMetaData) 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) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 45 with PersistenceUnitMetaData

use of org.datanucleus.metadata.PersistenceUnitMetaData in project tests by datanucleus.

the class XMLTest method testEventListeners.

/**
 * Test of Event Listeners
 */
public void testEventListeners() {
    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);
    ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(WebSite.class.getName(), clr);
    // Example callbacks
    EventListenerMetaData elmd = cmd1.getListenerForClass(cmd1.getFullClassName());
    assertNotNull("Site didnt have its own class registered as an EventListener!", elmd);
    assertEquals("Site EventListener has incorrect method for prePersist callback", elmd.getClassName() + ".prePersist", elmd.getMethodNameForCallbackClass(PrePersist.class.getName()));
    assertEquals("Site EventListener has incorrect method for postPersist callback", elmd.getClassName() + ".postPersist", elmd.getMethodNameForCallbackClass(PostPersist.class.getName()));
    assertEquals("Site EventListener has incorrect method for postPersist callback", elmd.getClassName() + ".load", elmd.getMethodNameForCallbackClass(PostLoad.class.getName()));
    assertNull(elmd.getMethodNameForCallbackClass(PreRemove.class.getName()));
    // Example listener
    elmd = cmd1.getListenerForClass(MyListener.class.getName());
    assertNotNull("Site didnt have MyListener registered as an EventListener!", elmd);
    assertEquals("Site EventListener has incorrect method for prePersist callback", elmd.getClassName() + ".register", elmd.getMethodNameForCallbackClass(PostPersist.class.getName()));
    assertEquals("Site EventListener has incorrect method for postPersist callback", elmd.getClassName() + ".deregister", elmd.getMethodNameForCallbackClass(PreRemove.class.getName()));
    assertNull(elmd.getMethodNameForCallbackClass(PrePersist.class.getName()));
}
Also used : 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) EventListenerMetaData(org.datanucleus.metadata.EventListenerMetaData) PersistenceUnitMetaData(org.datanucleus.metadata.PersistenceUnitMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Aggregations

PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)48 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)40 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)40 NucleusContext (org.datanucleus.NucleusContext)39 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)38 ClassMetaData (org.datanucleus.metadata.ClassMetaData)38 MetaDataManager (org.datanucleus.metadata.MetaDataManager)38 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)23 ElementMetaData (org.datanucleus.metadata.ElementMetaData)12 JoinMetaData (org.datanucleus.metadata.JoinMetaData)9 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)6 PackageMetaData (org.datanucleus.metadata.PackageMetaData)6 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)4 EventListenerMetaData (org.datanucleus.metadata.EventListenerMetaData)3 KeyMetaData (org.datanucleus.metadata.KeyMetaData)3 MapMetaData (org.datanucleus.metadata.MapMetaData)3 SequenceMetaData (org.datanucleus.metadata.SequenceMetaData)3 TableGeneratorMetaData (org.datanucleus.metadata.TableGeneratorMetaData)3 ArrayList (java.util.ArrayList)2 PersistenceManager (javax.jdo.PersistenceManager)2