use of org.jpox.samples.models.company.Person in project tests by datanucleus.
the class JDOQLTest method testFilterUsingAndNestedAnd.
/**
* Query with a simple filter with two ANDed clauses
* "(field >= value1 && field < value2)" and "(field >= value3 && field < value4)".
* @throws Exception
*/
public void testFilterUsingAndNestedAnd() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Person p1 = new Person();
p1.setPersonNum(1);
p1.setGlobalNum("1");
p1.setFirstName("Bugs");
p1.setLastName("Bunny");
p1.setAge(15);
Person p2 = new Person();
p2.setPersonNum(2);
p2.setGlobalNum("2");
p2.setFirstName("Daffy");
p2.setLastName("Duck");
p2.setAge(16);
Employee e3 = new Employee();
e3.setFirstName("Barney");
e3.setLastName("Rubble");
e3.setPersonNum(103);
e3.setGlobalNum("103");
e3.setSalary(124.50f);
e3.setAge(18);
pm.makePersistent(p1);
pm.makePersistent(p2);
pm.makePersistent(e3);
tx.commit();
} catch (Exception e) {
LOG.error("Exception during persist", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q1 = pm.newQuery("SELECT FROM " + Person.class.getName() + " WHERE (personNum >= 1 && personNum <=2) && (age >= 14 && age < 18)");
List<Person> results1 = (List<Person>) q1.execute();
assertEquals(2, results1.size());
Iterator<Person> iter = results1.iterator();
Person p1 = iter.next();
Person p2 = iter.next();
boolean daffy = false;
boolean bugs = false;
if (p1.getPersonNum() == 1) {
assertEquals("Bugs", p1.getFirstName());
assertEquals("Bunny", p1.getLastName());
assertEquals(1, p1.getPersonNum());
bugs = true;
} else {
assertEquals("Daffy", p1.getFirstName());
assertEquals("Duck", p1.getLastName());
assertEquals(2, p1.getPersonNum());
daffy = true;
}
if (p2.getPersonNum() == 1) {
assertEquals("Bugs", p2.getFirstName());
assertEquals("Bunny", p2.getLastName());
assertEquals(1, p2.getPersonNum());
bugs = true;
} else {
assertEquals("Daffy", p2.getFirstName());
assertEquals("Duck", p2.getLastName());
assertEquals(2, p2.getPersonNum());
daffy = true;
}
assertTrue("Bugs not present in results!", bugs);
assertTrue("Daffy not present in results!", daffy);
tx.commit();
} catch (Exception e) {
LOG.error("Exception during query", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Employee.class);
clean(Person.class);
}
}
use of org.jpox.samples.models.company.Person in project tests by datanucleus.
the class CacheTest method testRefreshWhenUpdatedExternally.
/**
* Test to check the refreshing of an object and whether the refreshed values are put in the L2 cache.
*/
public void testRefreshWhenUpdatedExternally() {
try {
// Create a PM and add an object
Object id = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Person p1 = new Person(102, "George", "Bush", "george.bush@whitehouse.gov");
pm.makePersistent(p1);
id = pm.getObjectId(p1);
tx.commit();
// Person should be pinned
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
pm.getFetchPlan().setGroup("all");
try {
tx.begin();
Person p = (Person) pm.getObjectById(id);
// Update a field in the datastore via different mechanism so it doesn't affect the L2 cache
updateFieldInDatastore();
LOG.info(">> Refreshing fields of Person");
pm.refresh(p);
assertEquals("George W", p.getFirstName());
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception during retrieve", e);
fail("Exception thrown while retrieving object to store in L2 cache with only few fields : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Refresh the object, so should pull in change
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
LOG.info(">> pm.getObjectById (from L2 cache?)");
// Retrieve the object, should be taken from L2 cache
Person p1 = (Person) pm.getObjectById(id);
assertEquals("George W", p1.getFirstName());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown while retrieving object. L2 cache not updated? : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out created data
clean(Person.class);
}
}
use of org.jpox.samples.models.company.Person in project tests by datanucleus.
the class JDOQLTest method testResult.
public void testResult() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Person p1 = new Person();
p1.setPersonNum(1);
p1.setGlobalNum("1");
p1.setFirstName("Bugs");
p1.setLastName("Bunny");
Person p2 = new Person();
p2.setPersonNum(2);
p2.setGlobalNum("2");
p2.setFirstName("Daffy");
p2.setLastName("Duck");
Employee e3 = new Employee();
e3.setFirstName("Barney");
e3.setLastName("Rubble");
e3.setPersonNum(103);
e3.setGlobalNum("103");
e3.setSalary(124.50f);
pm.makePersistent(p1);
pm.makePersistent(p2);
pm.makePersistent(e3);
tx.commit();
} catch (Exception e) {
LOG.error("Exception during persist", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.getDataStoreCache().evictAll();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q2 = pm.newQuery("SELECT this.firstName, this.lastName FROM " + Person.class.getName());
List<Object[]> results2 = (List<Object[]>) q2.execute();
assertEquals(3, results2.size());
tx.commit();
} catch (Exception e) {
LOG.error("Exception during query", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Employee.class);
clean(Person.class);
}
}
use of org.jpox.samples.models.company.Person in project tests by datanucleus.
the class JDOQLTest method testCandidateWithSubclasses.
public void testCandidateWithSubclasses() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Person p1 = new Person();
p1.setPersonNum(1);
p1.setGlobalNum("1");
p1.setFirstName("Bugs");
p1.setLastName("Bunny");
Person p2 = new Person();
p2.setPersonNum(2);
p2.setGlobalNum("2");
p2.setFirstName("Daffy");
p2.setLastName("Duck");
Employee e3 = new Employee();
e3.setFirstName("Barney");
e3.setLastName("Rubble");
e3.setPersonNum(103);
e3.setGlobalNum("103");
e3.setSalary(124.50f);
pm.makePersistent(p1);
pm.makePersistent(p2);
pm.makePersistent(e3);
tx.commit();
} catch (Exception e) {
LOG.error("Exception during persist", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.getDataStoreCache().evictAll();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Check number of objects present
Query q1 = pm.newQuery(Person.class);
List<Person> results1 = (List<Person>) q1.execute();
assertEquals(3, results1.size());
Iterator<Person> iter = results1.iterator();
int numEmployees = 0;
int numPeople = 0;
while (iter.hasNext()) {
Person p = iter.next();
if (p instanceof Employee) {
numEmployees++;
} else {
numPeople++;
}
}
assertEquals("Number of Employees wrong", 1, numEmployees);
assertEquals("Number of Person wrong", 2, numPeople);
Query q2 = pm.newQuery("SELECT FROM " + Person.class.getName() + " EXCLUDE SUBCLASSES");
List<Person> results2 = (List<Person>) q2.execute();
assertEquals(2, results2.size());
tx.commit();
} catch (Exception e) {
LOG.error("Exception during 1-N retrieve and check", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Employee.class);
clean(Person.class);
}
}
use of org.jpox.samples.models.company.Person in project tests by datanucleus.
the class ReachabilityTest method testDeepReachabilityByClass.
/**
* Tests if a BaseItem is reachable through a BaseContainer by interface
* field and does have the correct states at different times.
*/
/*public void testSimpleReachabilityOnOptimisticTxsDatastoreAttributedId()
{
try
{
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
tx.setOptimistic(true);
tx.begin();
//test with datastore attributed ids
Rate rate0 = new Rate();
Currency usd0 = new Currency("USD0");
pm.makePersistent(rate0);
pm.makePersistent(usd0);
tx.commit();
tx.begin();
rate0.setTarget(usd0);
//test with datastore attributed ids
Rate rate1 = new Rate();
Currency usd1 = new Currency("USD1");
rate1.setTarget(usd1);
pm.makePersistent(rate1);
tx.commit();
assertTrue("reachable persistent rate: isPersistent() == false", JDOHelper.isPersistent(rate1));
assertTrue("reachable persistent usd1: isPersistent() == false", JDOHelper.isPersistent(usd1));
}
finally
{
if (tx.isActive())
{
tx.rollback();
}
pm.close();
}
}
finally
{
// Clean out our data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
tx.begin();
Extent ex = pm.getExtent(Rate.class);
Iterator iter = ex.iterator();
while (iter.hasNext())
{
Rate rate = (Rate)iter.next();
if (rate.getSource() != null)
{
Currency source = rate.getSource();
rate.setSource(null);
source.setRates(null);
}
if (rate.getTarget() != null)
{
rate.setTarget(null);
}
}
tx.commit();
}
finally
{
if (tx.isActive())
{
tx.rollback();
}
pm.close();
}
clean(Rate.class);
clean(Currency.class);
}
}*/
/**
* Test if a Person is reachable through an Employee through a Manager
* and that it has the correct state at various times.
*/
public void testDeepReachabilityByClass() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object per1Id = null;
Object emp1Id = null;
Object mgr1Id = null;
try {
tx.begin();
Employee emp1 = new Employee(101, "Barney", "Rubble", "barney.rubble@jpox.com", (float) 123.45, "12346");
Manager mgr1 = new Manager(102, "Fred", "Flintstone", "fred.flintstone@jpox.com", (float) 240.00, "12348");
Person per1 = new Person(103, "Rob", "Rock", "rob.rock@yahoo.com");
assertFalse("newly created Person: isPersistent() == true", JDOHelper.isPersistent(per1));
assertFalse("newly created Person: isNew() == true", JDOHelper.isNew(per1));
assertFalse("newly created Person: isDirty() == true", JDOHelper.isDirty(per1));
// persist chain of objects Manager -> Employee -> Person
emp1.setBestFriend(per1);
mgr1.addSubordinate(emp1);
emp1.setManager(mgr1);
pm.makePersistent(mgr1);
assertTrue("reachable persistent Person: isPersistent() == false", JDOHelper.isPersistent(per1));
assertTrue("reachable persistent Person: isNew() == false", JDOHelper.isNew(per1));
assertTrue("reachable persistent Person: isDirty() == false", JDOHelper.isDirty(per1));
tx.commit();
// assert Person is now persistent clean or hollow
assertTrue("committed Person: isPersistent() == false", JDOHelper.isPersistent(per1));
assertFalse("committed Person: isNew() == true", JDOHelper.isNew(per1));
assertFalse("committed Person: isDirty() == true", JDOHelper.isDirty(per1));
per1Id = JDOHelper.getObjectId(per1);
emp1Id = JDOHelper.getObjectId(emp1);
mgr1Id = JDOHelper.getObjectId(mgr1);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
// assert the DB contains the correct data
tx.begin();
Person checkPer1 = (Person) pm.getObjectById(per1Id);
assertTrue("Person not in database", checkPer1 != null);
Employee checkEmp1 = (Employee) pm.getObjectById(emp1Id);
assertTrue("Employee not in database", checkEmp1 != null);
Manager checkMgr1 = (Manager) pm.getObjectById(mgr1Id);
assertTrue("Manager not in database", checkMgr1 != null);
assertSame("Employee by query not the same as Employee by navigation", checkEmp1, checkMgr1.getSubordinates().iterator().next());
assertSame("Person by query not the same as Person by navigation", checkPer1, checkEmp1.getBestFriend());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
Aggregations