use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class PersistenceManagerTest method testNormalFCOCollectionFieldPersistence2.
/**
* Test that deleting an object that is a member of a Collection field
* throws an exception
*/
public void testNormalFCOCollectionFieldPersistence2() {
/*
* If constraints aren't being used then the assumptions of this test don't hold.
*/
if (!getConfigurationForPMF(pmf).getBooleanProperty("datanucleus.validateConstraints")) {
return;
}
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();
try {
pm.deletePersistent(emp1);
tx.commit();
fail("Commit transaction was successful but shouldn't have been");
} catch (javax.jdo.JDODataStoreException e) {
if (tx.isActive())
tx.rollback();
}
} finally {
if (tx.isActive()) {
tx.rollback();
pm.close();
fail("Failed to perform test");
}
pm.close();
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class PersistenceManagerTest method createNewManager.
private Manager createNewManager(PersistenceManager pm, int dataset) throws Exception {
boolean successful = false;
Manager p = new Manager(dataset, FIRSTNAME[dataset], LASTNAME[dataset], EMAIL[dataset], EMP_SALARY[dataset], EMP_SERIAL[dataset]);
// add many employees as available, excluding the manager previously created
for (int i = 0; i < FIRSTNAME.length; i++) {
if (i != dataset) {
p.addSubordinate(new Employee(i, FIRSTNAME[i], LASTNAME[i], EMAIL[i], EMP_SALARY[i], EMP_SERIAL[i]));
}
}
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(p);
successful = true;
} finally {
if (successful)
tx.commit();
else
tx.rollback();
}
return p;
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class PersistenceManagerTest method testPersistenceOfOneToOneRelations.
/**
* Test that persisting a PC will also persist all PCs that are referenced.
*/
public void testPersistenceOfOneToOneRelations() {
try {
Manager manager = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
Employee employee = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object managerID = null;
try {
tx.begin();
pm.makePersistent(employee);
employee.setManager(manager);
tx.commit();
managerID = pm.getObjectId(manager);
} finally {
if (tx.isActive()) {
tx.rollback();
pm.close();
fail("Failed to persist object and commit transaction");
}
pm.close();
}
// Check the contents
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent clnManager = pm.getExtent(Manager.class, false);
// should only have one Primitive object
Iterator it = clnManager.iterator();
assertTrue(it.hasNext());
Manager m = (Manager) it.next();
assertEquals(managerID, pm.getObjectId(m));
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class PersistenceManagerTest method tearDown.
/**
* Invoked after each test is run
*/
public void tearDown() throws java.lang.Exception {
super.tearDown();
// TODO Remove all of this when each test cleans out its own data.
Extent ext = null;
java.util.Iterator it = null;
PersistenceManager pm = pmf.getPersistenceManager();
try {
// delete all InstanceCallbackContainer objects
pm.currentTransaction().begin();
ext = pm.getExtent(InstanceCallbackContainer.class, false);
it = ext.iterator();
while (it.hasNext()) {
InstanceCallbackContainer owner = (InstanceCallbackContainer) it.next();
pm.deletePersistent(owner);
}
pm.currentTransaction().commit();
// delete all InstanceCallbackTester objects
pm.currentTransaction().begin();
ext = pm.getExtent(InstanceCallbackTester.class, false);
it = ext.iterator();
while (it.hasNext()) {
InstanceCallbackTester tester = (InstanceCallbackTester) it.next();
// necesaary to avoid exception from jdoPreDelete() for this class only
tester.setTransientValue("");
pm.deletePersistent(tester);
}
pm.currentTransaction().commit();
// delete all InversePrimitive objects
pm.currentTransaction().begin();
ext = pm.getExtent(org.datanucleus.samples.widget.InversePrimitive.class, false);
it = ext.iterator();
while (it.hasNext()) {
InversePrimitive ip = (InversePrimitive) it.next();
ip.setTester(null);
pm.deletePersistent(ip);
}
pm.currentTransaction().commit();
// delete all CollectionFieldTester objects
pm.currentTransaction().begin();
ext = pm.getExtent(CollectionFieldTester.class, false);
it = ext.iterator();
while (it.hasNext()) {
CollectionFieldTester t = (CollectionFieldTester) it.next();
if (t.getPrimitiveCollection() != null) {
t.getPrimitiveCollection().clear();
}
if (t.getInversePrimitiveCollection() != null) {
t.getInversePrimitiveCollection().clear();
}
pm.deletePersistent(t);
}
pm.currentTransaction().commit();
// delete all Primative objects
pm.currentTransaction().begin();
ext = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
it = ext.iterator();
while (it.hasNext()) {
Primitive p = (Primitive) it.next();
pm.deletePersistent(p);
}
pm.currentTransaction().commit();
// disassociate all Employees and Departments from their Managers
pm.currentTransaction().begin();
ext = pm.getExtent(Manager.class, false);
it = ext.iterator();
while (it.hasNext()) {
Manager mgr = (Manager) it.next();
if (mgr.getSubordinates() != null) {
mgr.getSubordinates().clear();
}
if (mgr.getDepartments() != null) {
mgr.getDepartments().clear();
}
}
pm.currentTransaction().commit();
// delete all Employee objects
pm.currentTransaction().begin();
ext = pm.getExtent(Employee.class, false);
it = ext.iterator();
while (it.hasNext()) {
Employee emp = (Employee) it.next();
pm.deletePersistent(emp);
}
pm.currentTransaction().commit();
pm.currentTransaction().begin();
// dekete all Department objects
ext = pm.getExtent(Department.class, false);
it = ext.iterator();
while (it.hasNext()) {
Department d = (Department) it.next();
pm.deletePersistent(d);
}
pm.currentTransaction().commit();
// delete all Manager objects
pm.currentTransaction().begin();
ext = pm.getExtent(Manager.class, false);
it = ext.iterator();
while (it.hasNext()) {
Manager mgr = (Manager) it.next();
pm.deletePersistent(mgr);
}
pm.currentTransaction().commit();
// delete all Person objects
pm.currentTransaction().begin();
ext = pm.getExtent(Person.class, true);
it = ext.iterator();
while (it.hasNext()) {
Person person = (Person) it.next();
pm.deletePersistent(person);
}
pm.currentTransaction().commit();
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().commit();
pm.close();
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class JDOQLEvaluatorTest method testFilterNegate.
/**
* Test of filter with !(condition).
*/
public void testFilterNegate() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create some instances to query over
List<Person> instances = new ArrayList<>();
Person p1 = new Person(101, "Mickey", "Mouse", "mickey.mouse@warnerbros.com");
p1.setAge(34);
Employee p2 = new Employee(102, "Donald", "Duck", "donald.duck@warnerbros.com", 13400.0f, "12345");
p2.setAge(38);
Person p3 = new Person(103, "Minnie", "Mouse", "minnie.mouse@warnerbros.com");
p3.setAge(31);
instances.add(p1);
instances.add(p2);
instances.add(p3);
// Compile the query
JDOQuery q = (JDOQuery) pm.newQuery(Person.class, "!(age > 32)");
Query query = q.getInternalQuery();
ClassLoaderResolver clr = query.getExecutionContext().getClassLoaderResolver();
JavaQueryCompiler compiler = new JDOQLCompiler(query.getExecutionContext().getNucleusContext(), clr, null, query.getCandidateClass(), null, query.getFilter(), query.getParsedImports(), query.getOrdering(), query.getResult(), query.getGrouping(), query.getHaving(), query.getExplicitParametersDeclaration(), query.getExplicitVariablesDeclaration(), null);
QueryCompilation compilation = compiler.compile(new HashMap(), null);
// Execute the query
JavaQueryInMemoryEvaluator eval = new JDOQLInMemoryEvaluator(query, instances, compilation, null, clr);
List results = (List) eval.execute(true, true, true, true, true);
assertEquals("Number of result instances was wrong", 1, results.size());
Person p = (Person) results.get(0);
assertEquals("Result instance has wrong first name", "Minnie", p.getFirstName());
assertEquals("Result instance has wrong last name", "Mouse", p.getLastName());
assertEquals("Person number of result instance is wrong", 103, p.getPersonNum());
assertEquals("Age of result instance is wrong", 31, p.getAge());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown during query execution " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations