use of org.jpox.samples.models.company.Person in project tests by datanucleus.
the class ApplicationIdPersistenceTest method testAccessJdoConnection.
public void testAccessJdoConnection() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Person p = new Person();
p.setPersonNum(1);
p.setGlobalNum("1");
p.setFirstName("Bugs");
p.setLastName("Bunny");
Person p2 = new Person();
p2.setPersonNum(2);
p2.setGlobalNum("2");
p2.setFirstName("My");
p2.setLastName("Friend");
pm.makePersistent(p);
pm.makePersistent(p2);
JDOConnection conn = pm.getDataStoreConnection();
assertTrue("Native connection should be instanceof com.mongodb.DB!", conn.getNativeConnection() instanceof DB);
conn.close();
tx.commit();
} catch (Exception e) {
LOG.error("Exception during persist with connection access", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Person.class);
}
}
use of org.jpox.samples.models.company.Person in project tests by datanucleus.
the class JDOQLTest method testNextOnEmptyIterator.
/**
* Query that returns an empty list, and calling next on its iterator.
* @throws Exception
*/
public void testNextOnEmptyIterator() 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();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q1 = pm.newQuery("SELECT FROM " + Person.class.getName() + " WHERE firstName == 'Doofy'");
List<Person> results1 = (List<Person>) q1.execute();
Iterator<Person> iter = results1.iterator();
assertFalse(iter.hasNext());
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 testFilterSingle.
/**
* Query with a simple filter with one clause ("field == value")
* @throws Exception
*/
public void testFilterSingle() 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();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q1 = pm.newQuery("SELECT FROM " + Person.class.getName() + " WHERE firstName == 'Daffy'");
List<Person> results1 = (List<Person>) q1.execute();
assertEquals(1, results1.size());
Iterator<Person> iter = results1.iterator();
Person p = iter.next();
assertEquals("Daffy", p.getFirstName());
assertEquals("Duck", p.getLastName());
assertEquals(2, p.getPersonNum());
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();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Check number of objects present
Query q1 = pm.newQuery(pm.getExtent(Person.class, true));
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(pm.getExtent(Person.class, false));
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 JDOQLTest method testFilterOneToOne.
/**
* Query with a filter on 1-1 relation value.
* @throws Exception
*/
public void testFilterOneToOne() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object p1Id = null;
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");
p2.setBestFriend(p1);
pm.makePersistent(p1);
pm.makePersistent(p2);
tx.commit();
p1Id = pm.getObjectId(p1);
} 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();
Person p1 = (Person) pm.getObjectById(p1Id);
Query q1 = pm.newQuery("SELECT FROM " + Person.class.getName() + " WHERE bestFriend == :p");
List<Person> results1 = (List<Person>) q1.execute(p1);
assertEquals(1, results1.size());
Iterator<Person> iter = results1.iterator();
Person p = iter.next();
assertEquals("Daffy", p.getFirstName());
assertEquals("Duck", p.getLastName());
assertEquals(2, p.getPersonNum());
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);
}
}
Aggregations