use of javax.jdo.JDOUserException in project tests by datanucleus.
the class JDOQLBasicTest method testStaticFields.
/**
* Test for the use of the static fields in query method.
*/
public void testStaticFields() {
try {
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");
// Beep! Beep!
Employee roadrunner = new Employee(4, "Road", "Runner", "road.runner@warnerbros.com", 11, "serial 4");
// I hate the gravity
Employee coyote = new Employee(5, "Wile", "E. Coyote", "wile.coyote@acme.com", 9, "serial 5");
// paranoid, and neurotic
Employee duck = new Employee(6, "Daffy", "Duck", "daffy.duck@warnerbros.com", 7, "serial 6");
// You are my peanut.
Employee pepe = new Employee(7, "Pepe", "le Pew", "pepe.lepew@warnerbros.com", 8, "serial 7");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(woody);
pm.makePersistent(bart);
pm.makePersistent(bunny);
pm.makePersistent(roadrunner);
pm.makePersistent(coyote);
pm.makePersistent(duck);
pm.makePersistent(pepe);
tx.commit();
tx.begin();
try {
Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("firstName == org.jpox.samples.models.company.Employee.FIRSTNAME");
HashSet results = new HashSet((Collection) q.execute());
assertEquals("received: " + results, 1, results.size());
assertEquals(org.jpox.samples.models.company.Employee.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
q.closeAll();
q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("firstName == org.jpox.samples.models.company.Person.FIRSTNAME");
results = new HashSet((Collection) q.execute());
assertEquals("received: " + results, 1, results.size());
assertEquals(org.jpox.samples.models.company.Person.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
q.closeAll();
q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("firstName == Person.FIRSTNAME");
results = new HashSet((Collection) q.execute());
assertEquals("received: " + results, 1, results.size());
assertEquals(org.jpox.samples.models.company.Person.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
q.closeAll();
q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("firstName == Employee.FIRSTNAME");
results = new HashSet((Collection) q.execute());
assertEquals("received: " + results, 1, results.size());
assertEquals(org.jpox.samples.models.company.Employee.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
q.closeAll();
q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("firstName == FIRSTNAME");
results = new HashSet((Collection) q.execute());
assertEquals("received: " + results, 1, results.size());
assertEquals(org.jpox.samples.models.company.Employee.FIRSTNAME, ((Person) results.iterator().next()).getFirstName());
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Employee.class);
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class JDOQLBasicTest method runTestCandidateCollection.
protected void runTestCandidateCollection(PersistenceManager pm) {
Transaction tx = pm.currentTransaction();
try {
tx.begin();
try {
Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
HashSet results = new HashSet((Collection) q.execute());
Collection resultIds = new HashSet();
Iterator iter = results.iterator();
while (iter.hasNext()) {
resultIds.add(JDOHelper.getObjectId(iter.next()));
}
assertTrue("Expected result was all candidates elements, but received: " + StringUtils.collectionToString(resultIds), resultIds.equals(allEmployeeIds));
q.closeAll();
} catch (JDOUserException e) {
LOG.error("Exception thrown in querying ", e);
fail(e.getMessage());
}
try {
Collection candidates = new HashSet();
candidates.addAll(pm.getObjectsById(warnerEmployeeIds));
Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class, candidates);
HashSet results = new HashSet((Collection) q.execute());
Collection resultIds = new HashSet();
Iterator iter = results.iterator();
while (iter.hasNext()) {
resultIds.add(JDOHelper.getObjectId(iter.next()));
}
assertTrue("Expected result was warner candidate elements, but received: " + StringUtils.collectionToString(resultIds), resultIds.equals(warnerEmployeeIds));
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
try {
Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class, (Collection) null);
HashSet results = new HashSet((Collection) q.execute());
Collection resultIds = new HashSet();
Iterator iter = results.iterator();
while (iter.hasNext()) {
resultIds.add(JDOHelper.getObjectId(iter.next()));
}
assertTrue("Expected result was all candidate elements, but received: " + StringUtils.collectionToString(resultIds), resultIds.equals(allEmployeeIds));
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
try {
Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class);
q.setCandidates((Collection) null);
HashSet results = new HashSet((Collection) q.execute());
Collection resultIds = new HashSet();
Iterator iter = results.iterator();
while (iter.hasNext()) {
resultIds.add(JDOHelper.getObjectId(iter.next()));
}
assertTrue("Expected result was warner candidate elements, but received: " + StringUtils.collectionToString(resultIds), resultIds.equals(allEmployeeIds));
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
try {
Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class, new HashSet());
HashSet results = new HashSet((Collection) q.execute());
assertEquals("Number of elements is wrong", 0, results.size());
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
try {
Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class);
q.setCandidates(new HashSet());
HashSet results = new HashSet((Collection) q.execute());
assertEquals("Number of elements is wrong", 0, results.size());
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
try {
Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class);
q.setCandidates(new HashSet());
// Replaces collection with null, so all candidates
q.setCandidates((Collection) null);
HashSet results = new HashSet((Collection) q.execute());
Collection resultIds = new HashSet();
Iterator iter = results.iterator();
while (iter.hasNext()) {
resultIds.add(JDOHelper.getObjectId(iter.next()));
}
assertTrue("Expected result was all candidate elements, but received: " + StringUtils.collectionToString(resultIds), resultIds.equals(allEmployeeIds));
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
try {
Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class);
q.setCandidates((Extent) null);
HashSet results = new HashSet((Collection) q.execute());
Collection resultIds = new HashSet();
Iterator iter = results.iterator();
while (iter.hasNext()) {
resultIds.add(JDOHelper.getObjectId(iter.next()));
}
assertTrue("Expected result was all candidate elements, but received: " + StringUtils.collectionToString(resultIds), resultIds.equals(allEmployeeIds));
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
try {
Collection candidates = new HashSet();
candidates.addAll(pm.getObjectsById(warnerEmployeeIds));
Query q = pm.newQuery(org.jpox.samples.models.company.Employee.class, candidates, "salary > 10");
HashSet results = new HashSet((Collection) q.execute());
Collection resultIds = new HashSet();
Iterator iter = results.iterator();
while (iter.hasNext()) {
resultIds.add(JDOHelper.getObjectId(iter.next()));
}
assertTrue("Expected result was all expected elements, but received: " + StringUtils.collectionToString(resultIds), resultIds.equals(expectedEmployeeIds));
q.closeAll();
} catch (JDOUserException e) {
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
}
use of javax.jdo.JDOUserException in project tests by datanucleus.
the class JDOQLBasicTest method testStringStartsWith.
/**
* Test the use of the String.startsWith() method.
*/
public void testStringStartsWith() {
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");
// Beep! Beep!
Employee roadrunner = new Employee(4, "Road", "Runner", "Road.runner@warnerbros.com", 11, "serial 4");
// I hate the gravity
Employee coyote = new Employee(5, "Wile", "E. Coyote", "Wile.coyote@acme.com", 9, "serial 5");
// paranoid, and neurotic
Employee duck = new Employee(6, "Daffy", "Duck", "Daffy.duck@warnerbros.com", 7, "serial 6");
// You are my peanut.
Employee pepe = new Employee(7, "Pepe Changed", "le Pew", "Pepe.lepew@warnerbros.com", 8, "serial 7");
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(woody);
pm.makePersistent(bart);
pm.makePersistent(bunny);
pm.makePersistent(roadrunner);
pm.makePersistent(coyote);
pm.makePersistent(duck);
pm.makePersistent(pepe);
tx.commit();
tx.begin();
try {
// test 1
Query q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("emailAddress.startsWith(\"Road\")");
HashSet results = new HashSet((Collection) q.execute());
assertTrue("received: " + results, results.size() == 1);
q.closeAll();
// test 2
q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("emailAddress.startsWith(firstName)");
results = new HashSet((Collection) q.execute());
assertTrue("received: " + results, results.size() == 6);
q.closeAll();
// test 3
q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.setFilter("\"Road Runner\".startsWith(firstName)");
results = new HashSet((Collection) q.execute());
assertTrue("received: " + results, results.size() == 1);
q.closeAll();
// test 4
q = pm.newQuery(pm.getExtent(org.jpox.samples.models.company.Employee.class, false));
q.declareParameters("java.lang.String var1");
q.setFilter("var1.startsWith(firstName)");
results = new HashSet((Collection) q.execute("Road Runner"));
assertTrue("received: " + results, results.size() == 1);
q.closeAll();
} catch (JDOUserException e) {
LOG.error("Exception during test", e);
fail(e.getMessage());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
// Clean out our data
clean(Employee.class);
}
}
use of javax.jdo.JDOUserException 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 javax.jdo.JDOUserException in project tests by datanucleus.
the class JDOQLContainerTest method testQueryUsesContainsKeyOnceOnOneUnboundVariableImplicitVariables.
/**
* Test for the Map.containsKey() method.
*/
public void testQueryUsesContainsKeyOnceOnOneUnboundVariableImplicitVariables() {
try {
Gym gym1 = new Gym();
gym1.setName("Cinema");
gym1.setLocation("First floor");
Wardrobe w1 = new Wardrobe();
Wardrobe w2 = new Wardrobe();
Wardrobe w3 = new Wardrobe();
Wardrobe w4 = new Wardrobe();
w1.setModel("2 door");
w2.setModel("3 door");
w3.setModel("4 door");
w4.setModel("5 door");
gym1.getWardrobes2().put(w1, w1.getModel());
gym1.getWardrobes2().put(w2, w2.getModel());
gym1.getWardrobes2().put(w3, w3.getModel());
gym1.getWardrobes2().put(w4, w4.getModel());
Gym gym2 = new Gym();
gym2.setName("Shopping");
gym2.setLocation("Second floor");
gym2.getWardrobes2().put(w1, w1.getModel());
gym2.getWardrobes2().put(w2, w2.getModel());
Cloth c1 = new Cloth();
c1.setName("green shirt");
Cloth c2 = new Cloth();
c2.setName("red shirt");
Cloth c3 = new Cloth();
c3.setName("blue shirt");
GymEquipment ge1 = new GymEquipment();
ge1.setName("Weight");
GymEquipment ge2 = new GymEquipment();
ge2.setName("Yoga");
GymEquipment ge3 = new GymEquipment();
ge3.setName("Pilates");
GymEquipment ge4 = new GymEquipment();
ge4.setName("Abdominal");
gym1.getEquipments2().put(ge1, ge1.getName());
gym1.getEquipments2().put(ge2, ge2.getName());
gym2.getEquipments2().put(ge3, ge3.getName());
gym2.getEquipments2().put(ge4, ge4.getName());
gym1.getPartners2().put(gym2, gym2.getName());
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(gym1);
pm.makePersistent(gym2);
tx.commit();
tx.begin();
Query q = pm.newQuery(Gym.class);
q.setFilter("wardrobes2.containsKey(w1) && w1.model == \"4 door\"");
Collection c = (Collection) q.execute();
assertEquals(1, c.size());
q = pm.newQuery(Gym.class);
q.setFilter("wardrobes2.containsKey(w1) && (w1.model == \"2 door\" || w1.model == \"3 door\")");
c = (Collection) q.execute();
assertEquals(2, c.size());
q = pm.newQuery(Gym.class);
q.setFilter("wardrobes2.containsKey(w1) && (w1.model == \"4 door\" || w1.model == \"5 door\")");
c = (Collection) q.execute();
assertEquals(1, c.size());
try {
q = pm.newQuery(Gym.class);
q.setFilter("equipments2.containsKey(e) && g.equipments2.containsKey(e) && e.name =='Yoga'");
c = (Collection) q.execute();
fail("expected JDOUserException");
} catch (JDOUserException e) {
// expected
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
FitnessHelper.cleanFitnessData(pmf);
}
}
Aggregations