use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class JDOQLBasicTest method testSerialiseQueryResult.
/**
* Test for serialisation of the query results.
*/
public void testSerialiseQueryResult() {
Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(woody);
pm.makePersistent(bart);
pm.makePersistent(bunny);
tx.commit();
tx.begin();
try {
Query q = pm.newQuery(Employee.class);
List results = (List) q.execute();
assertEquals("received: " + results, 3, results.size());
try {
// serialise the results
FileOutputStream fos = new FileOutputStream("query_results.serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(results);
oos.flush();
oos.close();
} catch (Exception e) {
LOG.error("Exception serialising results", e);
fail("Exception serialising results : " + e.getMessage());
}
// Deserialise the results
try {
FileInputStream fis = new FileInputStream("query_results.serial");
ObjectInputStream ois = new ObjectInputStream(fis);
Object object2 = ois.readObject();
ois.close();
assertTrue("Deserialised form is not correct type", object2 instanceof List);
List deserialisedResults = (List) object2;
assertEquals("Invalid number of deserialised elements", 3, deserialisedResults.size());
} catch (Exception e) {
LOG.error("Exception deserialising results", e);
fail("Exception deserialising results : " + e.getMessage());
}
} catch (JDOUserException e) {
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
// Delete file
File file = new File("query_results.serial");
file.delete();
// Clean out our data
clean(Employee.class);
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class JDOQLBasicTest method testQueryWithDetachedObjects.
public void testQueryWithDetachedObjects() {
try {
Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
Manager boss = new Manager(3, "Boss", "WakesUp", "boss@wakes.up", 4, "serial 3");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.setNontransactionalRead(true);
try {
tx.begin();
pm.makePersistent(woody);
pm.makePersistent(boss);
pm.makePersistent(bart);
tx.commit();
// non transactional read
Collection c = (Collection) pm.newQuery(Employee.class).execute();
Employee detachedEmployee = (Employee) pm.detachCopy(c.iterator().next());
pm.close();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
tx.begin();
Query q = pm.newQuery(Employee.class, "this == p");
q.declareParameters("Employee p");
c = (Collection) q.execute(detachedEmployee);
assertEquals(JDOHelper.getObjectId(detachedEmployee), JDOHelper.getObjectId(c.iterator().next()));
tx.commit();
} catch (Exception e) {
LOG.error("Exception during test", e);
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class JDOQLContainerTest method testCollectionSize.
/**
* Test for the Collection.size() method.
*/
public void testCollectionSize() {
try {
Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
Employee lisa = new Employee(3, "Lisa", "Simpson", "lisa@simpson.com", 4, "serial 3");
Manager homersBrother = new Manager(4, "Homer Jr", "Simpson", "homerjr@simpson.com", 1, "serial 4");
Employee lisasSister = new Employee(5, "Lisa Sr", "Simpson", "lisasr@simpson.com", 4, "serial 5");
bart.setManager(homer);
lisa.setManager(homer);
homer.addSubordinate(lisa);
homer.addSubordinate(bart);
lisasSister.setManager(homersBrother);
homersBrother.addSubordinate(lisasSister);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(bart);
pm.makePersistent(homer);
pm.makePersistent(lisa);
pm.makePersistent(homersBrother);
pm.makePersistent(lisasSister);
tx.commit();
tx.begin();
Query q = pm.newQuery(Manager.class);
q.setFilter("subordinates.size() == 2");
Collection c = (Collection) q.execute();
assertEquals(1, c.size());
assertEquals(((Manager) c.iterator().next()).getFirstName(), "Homer");
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown during test", e);
fail("Exception thrown while executing query with collection.size()");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class JDOQLContainerTest method testCollectionIsEmpty.
/**
* Test for the Collection.isEmpty() method.
*/
public void testCollectionIsEmpty() {
try {
Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
Employee lisa = new Employee(3, "Lisa", "Simpson", "lisa@simpson.com", 4, "serial 3");
Manager homersBrother = new Manager(4, "Homer Jr", "Simpson", "homerjr@simpson.com", 1, "serial 4");
bart.setManager(homer);
lisa.setManager(homer);
homer.addSubordinate(lisa);
homer.addSubordinate(bart);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(bart);
pm.makePersistent(homer);
pm.makePersistent(lisa);
pm.makePersistent(homersBrother);
tx.commit();
// Try isEmpty() - should just return "homersBrother" since has no subordinates
tx.begin();
Query q = pm.newQuery(Manager.class);
q.setFilter("subordinates.isEmpty()");
Collection c = (Collection) q.execute();
assertEquals(1, c.size());
assertEquals(((Manager) c.iterator().next()).getFirstName(), "Homer Jr");
tx.commit();
// Try !isEmpty() - should just return "homer" since has subordinates
tx.begin();
q = pm.newQuery(Manager.class);
q.setFilter("!subordinates.isEmpty()");
c = (Collection) q.execute();
assertEquals(1, c.size());
assertEquals(((Manager) c.iterator().next()).getFirstName(), "Homer");
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown during test", e);
fail("Exception thrown while executing query with collection.isEmpty()");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class JDOQLResultTest method testRangeAndUnique.
/**
* Test case to use the JDO 2.0 setUnique() and setRange() methods to
* control the number and type of objects returned from the query.
*/
public void testRangeAndUnique() {
Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1");
Employee bart = new Employee(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
// Eh, what's up, doc?
Employee bunny = new Employee(3, "Bugs", "Bunny", "bugs.bunny@warnerbros.com", 12, "serial 3");
// Meep! Meep!
Employee roadrunner = new Employee(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(woody);
pm.makePersistent(bart);
pm.makePersistent(bunny);
pm.makePersistent(roadrunner);
tx.commit();
tx.begin();
try {
Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("firstName == \"Bugs\"");
q.setUnique(true);
Object results = q.execute();
assertTrue("setUnique() test returned an object of an incorrect type ", results instanceof Employee);
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
boolean success = false;
try {
Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setUnique(true);
q.execute();
q.closeAll();
} catch (JDOUserException e) {
success = true;
}
assertTrue("expected JdoUserException for unique == true and returned more than one instance", success);
try {
Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setRange(1, 3);
Collection results = (Collection) q.execute();
assertTrue("setRange() test returned an incorrect number of results : should have been 2 but was " + results.size(), results.size() == 2);
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
// Clean out our data
clean(Employee.class);
}
}
Aggregations