use of org.datanucleus.samples.annotations.models.company.Employee in project tests by datanucleus.
the class JPQLQueryTest method testCASE.
public void testCASE() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p = new Person(105, "Pebbles", "Flintstone", "pebbles.flintstone@datanucleus.org");
p.setAge(5);
em.persist(p);
Employee e = new Employee(106, "Barney", "Rubble", "barney.rubble@jpox.com", 10000.0f, "12345");
e.setAge(35);
em.persist(e);
em.flush();
List result = em.createQuery("SELECT p.personNum, CASE WHEN p.age < :param1 THEN 'Youth' WHEN p.age >= :param1 AND p.age < :param2 THEN 'Adult' ELSE 'Old' END" + " FROM " + Person.class.getName() + " p").setParameter("param1", 20).setParameter("param2", 50).getResultList();
Iterator resultsIter = result.iterator();
boolean pebbles = false;
boolean barney = false;
while (resultsIter.hasNext()) {
Object[] values = (Object[]) resultsIter.next();
if (((Number) values[0]).intValue() == 105 && values[1].equals("Youth")) {
pebbles = true;
}
if (((Number) values[0]).intValue() == 106 && values[1].equals("Adult")) {
barney = true;
}
}
assertTrue("Pebbles wasn't correct in the Case results", pebbles);
assertTrue("Barney wasn't correct in the Case results", barney);
tx.rollback();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(Employee.class);
clean(Person.class);
}
}
use of org.datanucleus.samples.annotations.models.company.Employee in project tests by datanucleus.
the class MetamodelTest method testInheritance.
/**
* Test for the case with inheritance.
*/
public void testInheritance() {
Metamodel model = emf.getMetamodel();
try {
EntityType<?> mgrType = model.entity(Manager.class);
assertNotNull(mgrType);
assertEquals("Number of Manager attributes is wrong", 16, mgrType.getAttributes().size());
assertEquals("Number of Manager singularAttributes is wrong", 13, mgrType.getSingularAttributes().size());
assertEquals("Number of Manager pluralAttributes is wrong", 3, mgrType.getPluralAttributes().size());
try {
// Field in Manager
Attribute attr = mgrType.getAttribute("subordinates");
assertNotNull(attr);
assertEquals(attr.getName(), "subordinates");
assertEquals(attr.getJavaType(), Set.class);
assertEquals(attr.getJavaMember().getName(), "subordinates");
assertTrue(attr.isCollection());
assertTrue(attr.isAssociation());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"subordinates\" field of " + Manager.class.getName());
}
try {
// Field in Employee
Attribute attr = mgrType.getAttribute("serialNo");
assertNotNull(attr);
assertEquals(attr.getName(), "serialNo");
assertEquals(attr.getJavaType(), String.class);
assertEquals(attr.getJavaMember().getName(), "serialNo");
assertFalse(attr.isCollection());
assertFalse(attr.isAssociation());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"serialNo\" field of " + Employee.class.getName());
}
try {
// Primitive Field in Employee
Attribute attr = mgrType.getAttribute("salary");
assertNotNull(attr);
assertEquals(attr.getName(), "salary");
assertEquals(attr.getJavaType(), float.class);
assertEquals(attr.getJavaMember().getName(), "salary");
assertFalse(attr.isCollection());
assertFalse(attr.isAssociation());
assertTrue(attr instanceof SingularAttribute);
assertFalse(((SingularAttribute) attr).isOptional());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"salary\" field of " + Employee.class.getName());
}
try {
// Field in Person
Attribute attr = mgrType.getAttribute("firstName");
assertNotNull(attr);
assertEquals(attr.getName(), "firstName");
assertEquals(attr.getJavaType(), String.class);
assertEquals(attr.getJavaMember().getName(), "firstName");
assertFalse(attr.isCollection());
assertFalse(attr.isAssociation());
} catch (IllegalArgumentException iae) {
fail("Didnt find Attribute for \"firstName\" field of " + Person.class.getName());
}
// Supertype should not be null
IdentifiableType empType = mgrType.getSupertype();
assertNotNull(empType);
IdentifiableType persType = empType.getSupertype();
assertNotNull(persType);
IdentifiableType superType = persType.getSupertype();
assertNull(superType);
} catch (IllegalArgumentException iae) {
fail("Didnt find EntityType for " + Manager.class.getName());
}
try {
EntityType<?> mugType = model.entity(ModeratedUserGroup.class);
assertNotNull(mugType);
assertNotNull(mugType.getId(long.class));
assertEquals("id", mugType.getId(long.class).getName());
} catch (IllegalArgumentException iae) {
fail("Error in metamodel tests" + iae.getMessage());
}
}
use of org.datanucleus.samples.annotations.models.company.Employee in project tests by datanucleus.
the class MultithreadedTest method processFind.
protected String processFind(Object mgrId, boolean transaction) {
EntityManager em = emf.createEntityManager();
emf.getCache().evictAll();
EntityTransaction tx = em.getTransaction();
try {
if (transaction) {
tx.begin();
}
Manager mgr = em.find(Manager.class, mgrId);
if (mgr == null) {
return "Manager not found!";
}
if (!("The".equals(mgr.getFirstName()))) {
return "Manager first name is wrong";
}
if (!("Boss".equals(mgr.getLastName()))) {
return "Manager last name is wrong";
}
Set<Employee> emps = mgr.getSubordinates();
if (emps == null) {
return "Manager has null subordinates!";
} else if (emps.size() != 100) {
return "Manager has incorrect number of subordinates (" + emps.size() + ")";
}
if (transaction) {
tx.commit();
}
} catch (Throwable thr) {
LOG.error("Exception in find", thr);
return "Exception in find : " + thr.getMessage();
} finally {
if (transaction && tx.isActive()) {
tx.rollback();
}
em.close();
}
return null;
}
use of org.datanucleus.samples.annotations.models.company.Employee in project tests by datanucleus.
the class MultithreadedTest method persistManagerWithEmployees.
/**
* Convenience method to create a Manager with 100 Employees. Used by various tests in this file
* @return The id of the manager object
*/
private Object persistManagerWithEmployees() {
// Persist some data
LOG.debug(">> Persisting data");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
Object mgrId = null;
try {
tx.begin();
Manager mgr = new Manager(1, "The", "Boss", "the.boss@datanucleus.com", 200000, "100000");
em.persist(mgr);
for (int i = 0; i < 100; i++) {
Employee emp = new Employee(i + 2, "FirstName" + i, "LastName" + i, "first.last." + i + "@datanucleus.com", 100000 + i, "12345" + i);
emp.setManager(mgr);
mgr.addSubordinate(emp);
em.persist(emp);
}
tx.commit();
mgrId = mgr.getPK();
} catch (Throwable thr) {
LOG.error("Exception persisting objects", thr);
fail("Exception persisting data : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
LOG.debug(">> Persisted data");
// Verify the persistence
em = emf.createEntityManager();
tx = em.getTransaction();
try {
tx.begin();
Query q = em.createQuery("SELECT e FROM " + Employee.class.getName() + " e");
List<Employee> emps = q.getResultList();
for (Employee e : emps) {
LOG.debug(">> emp=" + e + " e.mgr=" + e.getManager());
}
LOG.debug(">> Queried Employees");
tx.commit();
} catch (Throwable thr) {
LOG.error("Exception checking objects", thr);
fail("Exception checking data : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
return mgrId;
}
use of org.datanucleus.samples.annotations.models.company.Employee in project tests by datanucleus.
the class MultithreadedTest method processQueryAndDetachOneManyJoinBidir.
protected String processQueryAndDetachOneManyJoinBidir(boolean transaction) {
List<Employee> results = null;
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
if (transaction) {
tx.begin();
}
Query q = em.createQuery("SELECT e FROM " + Employee.class.getName() + " e");
List<Employee> emps = q.getResultList();
for (Employee e : emps) {
Manager mgr = e.getManager();
if (e instanceof Manager) {
// Get our subordinates loaded
((Manager) e).getSubordinates();
} else {
// Get subordinates of our Manager loaded
mgr.getSubordinates();
}
e.getBestFriend();
}
results = new ArrayList<Employee>(emps);
if (transaction) {
tx.commit();
}
} catch (Throwable thr) {
LOG.error("Exception query objects", thr);
return "Exception in query : " + thr.getMessage();
} finally {
if (transaction && tx.isActive()) {
tx.rollback();
}
// Detached the Employees and their loaded fields
em.close();
}
for (Employee e : results) {
try {
LOG.debug(">> Employee: " + e.getFirstName() + " " + e.getLastName() + " bestFriend=" + e.getBestFriend());
if (e instanceof Manager) {
Set subs = ((Manager) e).getSubordinates();
if (subs == null) {
return "Manager object didnt have its subordinates detached!";
} else if (subs.size() != 100) {
return "Manager had " + subs.size() + " subordinates instead of 100";
}
} else {
Manager mgr = e.getManager();
if (mgr == null) {
return "Employee=" + e + " didnt have its manager set!";
} else {
Set<Employee> subs = mgr.getSubordinates();
if (subs == null) {
return "Employee=" + e + " didnt have its subordinates set!";
} else if (subs.size() != 100) {
return "Employee=" + e + " has Manager with " + subs.size() + " subordinates instead of 100";
}
for (Employee subE : subs) {
subE.toString();
}
}
}
} catch (Exception exc) {
LOG.error(">> Exception thrown on check of results", exc);
return "Exception checking results : " + exc.getMessage();
}
}
return null;
}
Aggregations