use of org.datanucleus.api.jpa.JPAEntityManagerFactory in project tests by datanucleus.
the class EntityGraphTest method testNamedGraphSpecification.
/**
* Test of specification and registering of a named EntityGraph.
*/
public void testNamedGraphSpecification() {
try {
Set<String> graphNames = ((JPAEntityManagerFactory) emf).getEntityGraphNames();
assertNotNull(graphNames);
assertEquals("Number of EntityGraphs is wrong", 1, graphNames.size());
JPAEntityGraph eg = (JPAEntityGraph) ((JPAEntityManagerFactory) emf).getNamedEntityGraph(graphNames.iterator().next());
assertEquals("baseGraph", eg.getName());
assertEquals(GraphBase.class, eg.getClassType());
assertFalse(eg.getIncludeAllAttributes());
List<AttributeNode<?>> egNodes = eg.getAttributeNodes();
assertNotNull(egNodes);
assertEquals(3, egNodes.size());
boolean idPresent = false;
boolean namePresent = false;
boolean relationPresent = false;
for (AttributeNode node : egNodes) {
if (node.getAttributeName().equals("id")) {
idPresent = true;
} else if (node.getAttributeName().equals("name")) {
namePresent = true;
} else if (node.getAttributeName().equals("relation")) {
relationPresent = true;
Map<Class, Subgraph> subgraphsByClass = node.getSubgraphs();
assertNotNull(subgraphsByClass);
assertEquals(1, subgraphsByClass.size());
Map.Entry<Class, Subgraph> subgraphEntry = subgraphsByClass.entrySet().iterator().next();
assertEquals(GraphRelated.class, subgraphEntry.getKey());
Subgraph subgraph = subgraphEntry.getValue();
List<AttributeNode<?>> subNodes = subgraph.getAttributeNodes();
assertNotNull(subNodes);
assertEquals(1, subNodes.size());
AttributeNode subNode = subNodes.iterator().next();
assertEquals("id", subNode.getAttributeName());
}
}
assertTrue("id not present", idPresent);
assertTrue("name not present", namePresent);
assertTrue("relation not present", relationPresent);
// Should have been registered as dynamic FetchGroups
PersistenceNucleusContext nucCtx = emf.unwrap(PersistenceNucleusContext.class);
Set<FetchGroup> fgs = nucCtx.getFetchGroupManager().getFetchGroupsWithName("baseGraph");
assertEquals(2, fgs.size());
} finally {
}
}
use of org.datanucleus.api.jpa.JPAEntityManagerFactory in project tests by datanucleus.
the class JPANamingFactoryTest method testDatastoreIdColumnName.
public void testDatastoreIdColumnName() {
JPAEntityManagerFactory jpaEMF = (JPAEntityManagerFactory) emf;
NucleusContext nucCtx = jpaEMF.getNucleusContext();
ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
MetaDataManager mmgr = nucCtx.getMetaDataManager();
AbstractClassMetaData cmd1 = mmgr.getMetaDataForClass(MyDatastoreId.class, clr);
NamingFactory factory = new JPANamingFactory(nucCtx);
factory.setMaximumLength(SchemaComponent.COLUMN, 128);
factory.setNamingCase(NamingCase.LOWER_CASE);
assertEquals("Column name for datastore-id is incorrect", "mydatastoreid_id", factory.getColumnName(cmd1, ColumnType.DATASTOREID_COLUMN));
}
use of org.datanucleus.api.jpa.JPAEntityManagerFactory in project tests by datanucleus.
the class JPANamingFactoryTest method testVersionColumnName.
public void testVersionColumnName() {
JPAEntityManagerFactory jpaEMF = (JPAEntityManagerFactory) emf;
NucleusContext nucCtx = jpaEMF.getNucleusContext();
ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
MetaDataManager mmgr = nucCtx.getMetaDataManager();
AbstractClassMetaData cmd1 = mmgr.getMetaDataForClass(Trade4.class, clr);
NamingFactory factory = new JPANamingFactory(nucCtx);
factory.setMaximumLength(SchemaComponent.COLUMN, 128);
factory.setNamingCase(NamingCase.LOWER_CASE);
assertEquals("Column name for version is incorrect", "version", factory.getColumnName(cmd1, ColumnType.VERSION_COLUMN));
}
use of org.datanucleus.api.jpa.JPAEntityManagerFactory in project tests by datanucleus.
the class JPANamingFactoryTest method testTableName.
public void testTableName() {
JPAEntityManagerFactory jpaEMF = (JPAEntityManagerFactory) emf;
NucleusContext nucCtx = jpaEMF.getNucleusContext();
ClassLoaderResolver clr = nucCtx.getClassLoaderResolver(null);
MetaDataManager mmgr = nucCtx.getMetaDataManager();
AbstractClassMetaData cmd1 = mmgr.getMetaDataForClass(ByteArray.class, clr);
NamingFactory factory = new JPANamingFactory(nucCtx);
factory.setMaximumLength(SchemaComponent.TABLE, 128);
factory.setNamingCase(NamingCase.LOWER_CASE);
assertEquals("Table name is incorrect", "bytearray", factory.getTableName(cmd1));
factory.setNamingCase(NamingCase.LOWER_CASE_QUOTED);
assertEquals("Table name is incorrect", "\"bytearray\"", factory.getTableName(cmd1));
factory.setNamingCase(NamingCase.UPPER_CASE);
assertEquals("Table name is incorrect", "BYTEARRAY", factory.getTableName(cmd1));
factory.setNamingCase(NamingCase.UPPER_CASE_QUOTED);
assertEquals("Table name is incorrect", "\"BYTEARRAY\"", factory.getTableName(cmd1));
AbstractClassMetaData cmd2 = mmgr.getMetaDataForClass(SimpleClass.class, clr);
factory.setNamingCase(NamingCase.LOWER_CASE);
assertEquals("Table name is incorrect", "mysimpleclass", factory.getTableName(cmd2));
}
use of org.datanucleus.api.jpa.JPAEntityManagerFactory in project tests by datanucleus.
the class EntityManagerFactoryTest method testSerialize.
public void testSerialize() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@jpox.com");
p.setGlobalNum("First");
em.persist(p);
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception on persist before serialisation", e);
fail("Exception on persist : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Serialize the current EMF
byte[] bytes = null;
try {
bytes = TestHelper.serializeObject(emf);
} catch (RuntimeException re) {
LOG.error("Error serializing EMF", re);
fail("Error in serialization : " + re.getMessage());
}
// Deserialise the EMF
EntityManagerFactory emf = null;
try {
emf = (JPAEntityManagerFactory) TestHelper.deserializeObject(bytes);
} catch (RuntimeException re) {
LOG.error("Error deserializing EMF", re);
fail("Error in deserialization : " + re.getMessage());
}
JPAEntityManagerFactory jpaEMF = (JPAEntityManagerFactory) emf;
assertNotNull(jpaEMF);
assertNotNull(jpaEMF.getNucleusContext());
assertNotNull(jpaEMF.getMetamodel());
em = emf.createEntityManager();
tx = em.getTransaction();
try {
tx.begin();
TypedQuery<Person> q = em.createQuery("SELECT p FROM " + Person.class.getName() + " p", Person.class);
List<Person> results = q.getResultList();
assertEquals(1, results.size());
Person p = results.get(0);
assertEquals("Fred", p.getFirstName());
assertEquals("Flintstone", p.getLastName());
assertEquals("fred.flintstone@jpox.com", p.getEmailAddress());
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception on retrieve after deserialisation", e);
fail("Exception on retrieve after deserialisation : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Person.class);
}
}
Aggregations