use of org.datanucleus.PersistenceNucleusContextImpl in project tests by datanucleus.
the class AnnotationTest method testManyToMany.
/**
* Test of JPA M-N relation
*/
public void testManyToMany() {
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(PetroleumCustomer.class.getName(), clr);
assertEquals("Customer has incorrect table name", cmd1.getTable(), "JPA_AN_PETROL_CUSTOMER");
AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("suppliers");
assertNotNull("Customer.suppliers is null!", fmd1);
assertEquals("Customer.suppliers mapped-by is incorrect", fmd1.getMappedBy(), "customers");
assertEquals("Customer.suppliers relationType is incorrect", fmd1.getRelationType(clr), RelationType.MANY_TO_MANY_BI);
assertEquals("Customer.suppliers jointable name is incorrect", fmd1.getTable(), "JPA_AN_PETROL_CUST_SUPP");
// non-owner side
ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(PetroleumSupplier.class.getName(), clr);
assertEquals("Supplier has incorrect table name", cmd2.getTable(), "JPA_AN_PETROL_SUPPLIER");
AbstractMemberMetaData fmd2 = cmd2.getMetaDataForMember("customers");
assertNotNull("Supplier.customers is null!", fmd2);
assertEquals("Supplier.customers mapped-by is incorrect", fmd2.getMappedBy(), null);
assertEquals("Supplier.customers relationType is incorrect", fmd2.getRelationType(clr), RelationType.MANY_TO_MANY_BI);
assertEquals("Supplier.customers jointable name is incorrect", fmd2.getTable(), null);
// join table info
JoinMetaData joinmd = fmd1.getJoinMetaData();
assertNotNull("Customer.suppliers has no join table!", joinmd);
assertNotNull("Customer.suppliers has incorrect join columns", joinmd.getColumnMetaData());
assertEquals("Customer.suppliers has incorrect number of join columns", joinmd.getColumnMetaData().length, 1);
assertEquals("Customer.suppliers has incorrect owner join column name", joinmd.getColumnMetaData()[0].getName(), "CUSTOMER_ID");
ElementMetaData elemmd = fmd1.getElementMetaData();
assertNotNull("Customer.suppliers has no element column info but should", elemmd);
assertNotNull("Customer.suppliers has incorrect element columns", elemmd.getColumnMetaData());
assertEquals("Customer.suppliers has incorrect number of element columns", elemmd.getColumnMetaData().length, 1);
assertEquals("Customer.suppliers has incorrect element join column name", elemmd.getColumnMetaData()[0].getName(), "SUPPLIER_ID");
}
use of org.datanucleus.PersistenceNucleusContextImpl in project tests by datanucleus.
the class AnnotationTest method testOrderBy.
/**
* Test of @OrderBy.
*/
public void testOrderBy() {
NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
ClassLoaderResolver clr = new ClassLoaderResolverImpl();
ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(UserGroup.class.getName(), clr);
OrderMetaData omd = cmd1.getMetaDataForMember("members").getOrderMetaData();
assertNotNull("UserGroup.members has no OrderMetaData!", omd);
FieldOrder[] orderTerms = omd.getFieldOrders();
assertFalse("UserGroup.members is not marked as using an ordered list", omd.isIndexedList());
assertNotNull("UserGroup.members has null field ordering info", orderTerms);
assertEquals("UserGroup.members has incorrect number of field ordering terms", orderTerms.length, 1);
assertEquals("UserGroup.members has incorrect field ordering field-name", orderTerms[0].getFieldName(), "name");
assertTrue("UserGroup.members has incorrect field ordering direction", orderTerms[0].isForward());
}
use of org.datanucleus.PersistenceNucleusContextImpl in project tests by datanucleus.
the class AnnotationTest method testEmbeddedId.
/**
* Test of basic JPA @EmbeddedId.
*/
public void testEmbeddedId() {
NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
// Retrieve the metadata from the MetaDataManager (populates and initialises everything)
ClassLoaderResolver clr = new ClassLoaderResolverImpl();
ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Department.class.getName(), clr);
assertEquals(1, cmd1.getNoOfPrimaryKeyMembers());
}
use of org.datanucleus.PersistenceNucleusContextImpl in project tests by datanucleus.
the class AnnotationTest method testOneToOneUni.
/**
* Test of JPA 1-1 unidir relation
*/
public void testOneToOneUni() {
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(LoginAccount.class.getName(), clr);
assertEquals("LoginAccount has incorrect table name", cmd1.getTable(), "JPA_AN_LOGINACCOUNT");
AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("login");
assertNotNull("LoginAccount.login is null!", fmd1);
assertEquals("LoginAccount.login mapped-by is incorrect", fmd1.getMappedBy(), null);
assertEquals("LoginAccount.login relationType is incorrect", fmd1.getRelationType(clr), RelationType.ONE_TO_ONE_UNI);
assertNotNull("LoginAccount.login has no column info", fmd1.getColumnMetaData());
assertEquals("LoginAccount.login has incorrect number of columns", fmd1.getColumnMetaData().length, 1);
assertEquals("LoginAccount.login column name is wrong", fmd1.getColumnMetaData()[0].getName(), "LOGIN_ID");
ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Login.class.getName(), clr);
assertEquals("LoginAccount has incorrect table name", cmd2.getTable(), "JPA_AN_LOGIN");
}
use of org.datanucleus.PersistenceNucleusContextImpl in project tests by datanucleus.
the class AnnotationTest method testNamedQuery.
/**
* Test of JPA @NamedQuery, @NamedNativeQuery.
*/
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 JPA_AN_LOGIN WHERE FIRSTNAME = 'John'", sqlQuery.getQuery());
}
Aggregations