use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class PersistenceManagerTest method testNormalFCOCollectionFieldPersistence3.
/**
* Test that removing a member of a normal Collection field does NOT delete
* the object that was removed
*/
public void testNormalFCOCollectionFieldPersistence3() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
Employee emp1 = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
try {
mgr.addSubordinate(emp1);
tx.begin();
pm.makePersistent(mgr);
tx.commit();
tx.begin();
mgr.getSubordinates().remove(emp1);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
pm.close();
fail("Failed to persist object and commit transaction");
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent ex = pm.getExtent(Employee.class, false);
java.util.Iterator it = ex.iterator();
assertTrue(it.hasNext());
Employee emp = (Employee) it.next();
assertEquals(1, emp.getPersonNum());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class PersistenceManagerTest method testTransientObjectCollections.
/**
* Tests that objects can be added to a Collection owned by a persistent object made transient.
*/
public void testTransientObjectCollections() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Employee emp = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
try {
tx.begin();
pm.makePersistent(mgr);
tx.commit();
tx = pm.currentTransaction();
tx.begin();
pm.retrieve(mgr);
pm.retrieveAll(mgr.getSubordinates());
pm.retrieveAll(mgr.getDepartments());
pm.makeTransient(mgr);
mgr.addSubordinate(emp);
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception thrown in test", e);
fail("Exception thrown while making object with collection transient : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class ExtentTest method testExtentSubclasses.
/**
* Test use of pm.getExtent and use of the subclasses flag.
*/
public void testExtentSubclasses() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Employee empl = new Employee(0, "Homer", "Simpson", "homer@simpsons.com", (float) 200000.0, "123");
Manager mgr = new Manager(1, "Matt", "Groening", "matt@simpsons.com", (float) 500000.0, "1");
pm.makePersistent(empl);
pm.makePersistent(mgr);
pm.flush();
// test subclasses argument == false (should contain Employee only)
Extent<Employee> extent = pm.getExtent(Employee.class, false);
java.util.Iterator<Employee> it = extent.iterator();
Employee empl2 = it.next();
assertEquals(empl.getPersonNum(), empl2.getPersonNum());
assertEquals(false, it.hasNext());
tx.commit();
// test subclasses argument == true (should contain Employee AND Manager)
tx.begin();
extent = pm.getExtent(Employee.class, true);
it = extent.iterator();
empl2 = it.next();
if (empl2 instanceof Manager) {
assertEquals(1, empl2.getPersonNum());
pm.deletePersistent(empl2);
empl2 = (Employee) it.next();
assertEquals(0, empl2.getPersonNum());
pm.deletePersistent(empl2);
} else {
assertEquals(0, empl2.getPersonNum());
pm.deletePersistent(empl2);
empl2 = (Manager) it.next();
assertEquals(1, empl2.getPersonNum());
pm.deletePersistent(empl2);
}
assertEquals(false, it.hasNext());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Manager.class);
clean(Employee.class);
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class JDOQLTest method testRange.
public void testRange() 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 q2 = pm.newQuery("SELECT FROM " + Person.class.getName() + " ORDER BY this.personNum RANGE 1,3");
List<Person> results2 = (List<Person>) q2.execute();
assertEquals(2, results2.size());
Iterator<Person> iter = results2.iterator();
boolean daffyPresent = false;
boolean barneyPresent = false;
while (iter.hasNext()) {
Person p = iter.next();
if (p.getFirstName().equals("Barney")) {
barneyPresent = true;
} else if (p.getFirstName().equals("Daffy")) {
daffyPresent = true;
}
}
assertTrue(daffyPresent);
assertTrue(barneyPresent);
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.Employee in project tests by datanucleus.
the class JDOQLTest method testStringToUpperCase.
public void testStringToUpperCase() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Employee e = new Employee();
e.setFirstName("Barney");
e.setLastName("Rubble");
e.setPersonNum(103);
e.setGlobalNum("103");
e.setSalary(124.50f);
pm.makePersistent(e);
pm.flush();
Query q1 = pm.newQuery("SELECT FROM " + Employee.class.getName() + " WHERE this.firstName.toUpperCase() == 'BARNEY'");
List<Employee> results1 = (List<Employee>) q1.execute();
assertEquals(1, results1.size());
Employee e1 = results1.iterator().next();
assertEquals("Wrong Employee", "Rubble", e1.getLastName());
tx.rollback();
} catch (Exception e) {
LOG.error("Exception during JDOQL String toUpperCase test", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
}
}
Aggregations