use of org.jpox.samples.dependentfield.DepInterfaceImpl1 in project tests by datanucleus.
the class DependentFieldTest method testInterfaceDependentFields.
/**
* Test of dependent fields using interface 1-1 relationship.
* @throws Exception
*/
public void testInterfaceDependentFields() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
DependentHolder holder1 = new DependentHolder(101, "Holder 1");
DepInterfaceImpl1 impl1 = new DepInterfaceImpl1("Impl1", 23);
holder1.setIntf(impl1);
DependentHolder holder2 = new DependentHolder(102, "Holder 2");
DepInterfaceImpl2 impl2 = new DepInterfaceImpl2("Impl2", 56.7);
holder2.setIntf(impl2);
pm.makePersistent(holder1);
pm.makePersistent(holder2);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown while creating interface dependent objects : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check the numbers of objects created
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(DependentHolder.class);
List results = (List) q.execute();
assertEquals("Number of DepInterfaceHolder objects created is incorrect", results.size(), 2);
q = pm.newQuery(DepInterfaceImpl1.class);
results = (List) q.execute();
assertEquals("Number of DepInterfaceImpl1 objects created is incorrect", results.size(), 1);
q = pm.newQuery(DepInterfaceImpl2.class);
results = (List) q.execute();
assertEquals("Number of DepInterfaceImpl2 objects created is incorrect", results.size(), 1);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown while checking interface dependent objects : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Try to delete the implementations (checks if any FKs are created)
if (storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_FOREIGN_KEYS)) {
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(DepInterfaceImpl2.class);
List results = (List) q.execute();
Iterator resultsIter = results.iterator();
while (resultsIter.hasNext()) {
DepInterfaceImpl2 sq = (DepInterfaceImpl2) resultsIter.next();
pm.deletePersistent(sq);
}
tx.commit();
fail("Managed to delete a DepInterfaceImpl2 object that was referenced by another object. " + "This should not have happened due to FK constraints. Maybe this RDBMS doesnt manage FKs correctly, or not an RDBMS?");
} catch (Exception e) {
// Should come through here since it should throw an exception due to FK constraints
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
// Delete the holders. This *should* delete the Shapes as well
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(DependentHolder.class);
List results = (List) q.execute();
Iterator resultsIter = results.iterator();
while (resultsIter.hasNext()) {
DependentHolder holder = (DependentHolder) resultsIter.next();
pm.deletePersistent(holder);
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
fail("Exception thrown while deleting interface dependent objects : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check the numbers of objects persisted
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(DependentHolder.class);
List results = (List) q.execute();
assertEquals("Number of DepInterfaceHolder objects persisted is incorrect", results.size(), 0);
q = pm.newQuery(DepInterfaceImpl1.class);
results = (List) q.execute();
assertEquals("Number of DepInterfaceImpl1 objects persisted is incorrect", results.size(), 0);
q = pm.newQuery(DepInterfaceImpl2.class);
results = (List) q.execute();
assertEquals("Number of DepInterfaceImpl2 objects persisted is incorrect", results.size(), 0);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while checking interface dependent objects : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clearDependentData(pmf);
}
}
Aggregations