use of org.jpox.samples.models.company.Project in project tests by datanucleus.
the class PersistenceTest method testDatastoreIdPersist.
/**
* Test that persists using datastore identity.
*/
public void testDatastoreIdPersist() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
try {
tx.begin();
Project p = new Project("DataNucleus", 1000000);
pm.makePersistent(p);
tx.commit();
id = pm.getObjectId(p);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Project p = (Project) pm.getObjectById(id);
assertEquals("DataNucleus", p.getName());
assertEquals(1000000, p.getBudget());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Project.class);
}
}
use of org.jpox.samples.models.company.Project in project tests by datanucleus.
the class JDOQLContainerTest method testContainsResultVariableNestedContains.
public void testContainsResultVariableNestedContains() {
try {
Manager homer = new Manager(1, "Homer", "Simpson", "homer@simpson.com", 1, "serial 1");
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");
Manager boss2 = new Manager(4, "Boss", "WakesUp2", "boss2@wakes.up", 5, "serial 4");
Manager boss4 = new Manager(6, "Boss", "WakesUp4", "boss4@wakes.up", 7, "serial 6");
Department deptA = new Department("deptA");
Department deptB = new Department("deptB");
Department deptC = new Department("deptC");
Department deptD = new Department("deptD");
Department deptE = new Department("deptE");
Project projA = new Project("projA", 1);
Project projB = new Project("projB", 2);
Project projC = new Project("projC", 3);
Project projD = new Project("projD", 4);
Project projE = new Project("projE", 5);
deptA.addProject(projA);
deptA.addProject(projB);
deptB.addProject(projC);
deptC.addProject(projA);
deptC.addProject(projB);
deptD.addProject(projD);
deptE.addProject(projA);
deptE.addProject(projE);
boss.addDepartment(deptA);
boss.addDepartment(deptB);
boss.addDepartment(deptC);
boss2.addDepartment(deptD);
boss2.addDepartment(deptE);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(bart);
pm.makePersistent(homer);
pm.makePersistent(boss);
pm.makePersistent(boss2);
pm.makePersistent(boss4);
tx.commit();
tx.begin();
Query q = pm.newQuery(Manager.class);
q.setFilter("departments.contains(d) && d.projects.contains(p)");
q.setResult("d, p");
q.declareVariables("Department d; Project p");
Collection c = (Collection) q.execute();
assertEquals(8, c.size());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Project in project tests by datanucleus.
the class JDOQLContainerTest method testContainsResultVariableNestedContains2.
/**
* Test of collection(A) contains C1 and C1.contains(B) contains D1
*/
public void testContainsResultVariableNestedContains2() {
try {
Manager mgr1 = new Manager(101, "Fred", "Flintstone", "fred.flintstone@jpox.com", (float) 250.0, "12345");
Manager mgr2 = new Manager(102, "Barney", "Rubble", "barney.rubble@jpox.com", (float) 220.0, "12356");
Department dept1 = new Department("Gravel");
Department dept2 = new Department("Coal");
Department dept3 = new Department("Cement");
Department dept4 = new Department("Sales");
Department dept5 = new Department("Marketing");
Project proj1 = new Project("proj1", 12000);
Project proj2 = new Project("proj2", 24000);
Project proj3 = new Project("proj3", 18000);
mgr1.addDepartment(dept1);
mgr1.addDepartment(dept2);
mgr1.addDepartment(dept3);
mgr2.addDepartment(dept4);
mgr2.addDepartment(dept5);
dept1.addProject(proj1);
dept2.addProject(proj1);
dept2.addProject(proj2);
dept3.addProject(proj1);
dept3.addProject(proj2);
dept4.addProject(proj3);
dept5.addProject(proj3);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(mgr1);
pm.makePersistent(mgr2);
tx.commit();
tx.begin();
Query q = pm.newQuery(Manager.class);
q.setFilter("departments.contains(d) && d.projects.contains(p)");
q.setResult("d, p");
q.declareVariables("Department d; Project p");
Collection c = (Collection) q.execute();
assertEquals(7, c.size());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Project in project tests by datanucleus.
the class PersistenceManagerTest method testClose.
/**
* Test of closure of the PM.
*/
public void testClose() {
PersistenceManager pm = pmf.getPersistenceManager();
pm.currentTransaction().begin();
Project prj = new Project("Top secret", 123456);
pm.makePersistent(prj);
Object prjId = pm.getObjectId(prj);
// Causes rollback of current transaction
pm.close();
// Check that object wasn't persisted
pm = pmf.getPersistenceManager();
try {
pm.currentTransaction().begin();
Object prjDB = pm.getObjectById(prjId);
LOG.error("Object " + prjDB + " was persisted yet should have been rolled back when PM was closed with active transaction");
pm.currentTransaction().commit();
} catch (JDOObjectNotFoundException onfe) {
// Expected
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
// test close twice
pm = pmf.getPersistenceManager();
assertFalse("Before close(), isClosed() should return false", pm.isClosed());
pm.close();
assertTrue("After close(), isClosed() should return true", pm.isClosed());
try {
pm.close();
} catch (JDOUserException e) {
// JDO2.3 spec 12.6 allows redundant calls to close().
fail("Should not have raised an exception when closing a closed PM");
}
}
use of org.jpox.samples.models.company.Project in project tests by datanucleus.
the class ApplicationIdPersistenceTest method testOneToMany.
public void testOneToMany() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object dept1Id = null;
try {
tx.begin();
Department dept1 = new Department("Finance");
Project prj1 = new Project("Cost Cutting", 150000);
Project prj2 = new Project("Restructuring", 100000);
dept1.addProject(prj1);
dept1.addProject(prj2);
pm.makePersistent(dept1);
tx.commit();
dept1Id = pm.getObjectId(dept1);
} catch (Exception e) {
LOG.error("Exception during 1-N persist", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// No L2 cache interference
pmf.getDataStoreCache().evictAll();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Check number of objects present
Query q1 = pm.newQuery(Department.class);
List<Department> results1 = (List<Department>) q1.execute();
assertEquals(1, results1.size());
Query q2 = pm.newQuery(Project.class);
List<Project> results2 = (List<Project>) 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();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Department dept1 = null;
try {
dept1 = (Department) pm.getObjectById(dept1Id);
assertEquals("Finance", dept1.getName());
} catch (JDOObjectNotFoundException onfe) {
fail("Department dept1 not found");
}
assertNotNull("Department projects should not be null", dept1.getProjects());
Set<Project> projects = dept1.getProjects();
assertEquals("Number of projects is incorrect", 2, projects.size());
Iterator<Project> iter = projects.iterator();
Project prj1 = iter.next();
Project prj2 = iter.next();
if (prj1.getName().equals("Cost Cutting")) {
assertEquals("Budget of Cost Cutting incorrect", 150000, prj1.getBudget());
assertEquals("Second project unexpected", "Restructuring", prj2.getName());
assertEquals("Second project budget incorrect", 100000, prj2.getBudget());
} else if (prj1.getName().equals("Restructuring")) {
assertEquals("Budget of Restructring incorrect", 100000, prj1.getBudget());
assertEquals("Second project unexpected", "Cost Cutting", prj2.getName());
assertEquals("Second project budget incorrect", 150000, prj2.getBudget());
} else {
fail("First project unexpected");
}
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(Department.class);
clean(Project.class);
}
}
Aggregations