use of org.jpox.samples.one_many.collection.PCFKListElement in project tests by datanucleus.
the class RelationshipTest method test1toNUnidirOrderedListDetachAttach.
/**
* Test case for 1-N unidirectional relationships using an ordered List with detachment.
*/
public void test1toNUnidirOrderedListDetachAttach() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// Create sample data and detach it
Object holderId = null;
ListHolder detachedHolder = null;
try {
tx.begin();
ListHolder holder = new ListHolder();
PCFKListElement elem1 = new PCFKListElement("First");
PCFKListElement elem2 = new PCFKListElement("Middle");
PCFKListElement elem3 = new PCFKListElement("Last");
holder.getFkListPCOrdered().add(elem1);
holder.getFkListPCOrdered().add(elem2);
holder.getFkListPCOrdered().add(elem3);
pm.makePersistent(holder);
holderId = JDOHelper.getObjectId(holder);
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown while creating data for 1-N unidirectional ordered List : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve holder and detach it
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
pm.getFetchPlan().setMaxFetchDepth(-1);
pm.getFetchPlan().setGroup(FetchPlan.ALL);
ListHolder holder = (ListHolder) pm.getObjectById(holderId);
detachedHolder = (ListHolder) pm.detachCopy(holder);
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown while detaching data for 1-N unidirectional ordered List : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Delete an element while detached
assertEquals("Number of elements whilst detached is wrong", 3, detachedHolder.getFkListPCOrdered().size());
detachedHolder.getFkListPCOrdered().remove(1);
// Reattach
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(detachedHolder);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown during reattach", e);
fail("Exception thrown while attaching data for 1-N unidirectional ordered List : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve and check it
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the holder and check the ordered data
ListHolder holder = (ListHolder) pm.getObjectById(holderId);
List coll1 = holder.getFkListPCOrdered();
assertTrue("Collection should have elements but is null!", coll1 != null);
assertEquals("Collection has incorrect number of elements", coll1.size(), 2);
PCFKListElement elem1 = (PCFKListElement) coll1.get(0);
PCFKListElement elem2 = (PCFKListElement) coll1.get(1);
assertEquals("First retrieved element (after remove) is incorrect", "First", elem1.getName());
assertEquals("Second retrieved element (after remove) is incorrect", "Middle", elem2.getName());
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown while interrogating data for 1-N unidirectional ordered List : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(ListHolder.class);
clean(PCFKListElement.class);
}
}
use of org.jpox.samples.one_many.collection.PCFKListElement in project tests by datanucleus.
the class RelationshipTest method test1toNUnidirFKList.
/**
* Test case for 1-N inverse unidirectional, using a List.
*/
public void test1toNUnidirFKList() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object holderId = null;
// Check the persistence of owner and elements
try {
tx.begin();
// Create some data
ListHolder holder = new ListHolder();
PCFKListElement elem1 = new PCFKListElement("Element 1");
PCFKListElement elem2 = new PCFKListElement("Element 2");
holder.getFkListPC().add(elem1);
holder.getFkListPC().add(elem2);
pm.makePersistent(holder);
tx.commit();
holderId = pm.getObjectId(holder);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while creating 1-N unidir FK relationships (List) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
// Check the retrieval of the owner and elements
tx = pm.currentTransaction();
try {
tx.begin();
ListHolder holder = (ListHolder) pm.getObjectById(holderId);
assertNotNull("Unable to retrieve container object for 1-N unidir FK relationship (List)", holder);
Collection elements = holder.getFkListPC();
assertNotNull("Elements in Holder 1-N unidir FK List is null", elements);
assertEquals("Number of elements in 1-N unidir FK relationship (List) is wrong", 2, elements.size());
Iterator iter = elements.iterator();
int i = 0;
while (iter.hasNext()) {
PCFKListElement elem = (PCFKListElement) iter.next();
if (i == 0) {
assertEquals("The first element in the List is wrong", elem.getName(), "Element 1");
} else if (i == 1) {
assertEquals("The second element in the List is wrong", elem.getName(), "Element 2");
}
i++;
}
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown while querying 1-N unidir FK relationships (List) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
pm.close();
} finally {
// Clean out our data
clean(ListHolder.class);
clean(PCFKListElement.class);
}
}
use of org.jpox.samples.one_many.collection.PCFKListElement in project tests by datanucleus.
the class RelationshipTest method test1toNUnidirOrderedList.
/**
* Test case for 1-N unidirectional relationships using an ordered List.
*/
public void test1toNUnidirOrderedList() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// Create sample data
Object holderId = null;
try {
tx.begin();
ListHolder holder = new ListHolder();
PCFKListElement elem1 = new PCFKListElement("First");
PCFKListElement elem2 = new PCFKListElement("Middle");
PCFKListElement elem3 = new PCFKListElement("Last");
holder.getFkListPCOrdered().add(elem1);
holder.getFkListPCOrdered().add(elem2);
holder.getFkListPCOrdered().add(elem3);
pm.makePersistent(holder);
holderId = JDOHelper.getObjectId(holder);
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown while creating data for 1-N unidirectional ordered List : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Retrieve the holder and check the ordered data
ListHolder holder = (ListHolder) pm.getObjectById(holderId);
List coll1 = holder.getFkListPCOrdered();
assertTrue("Collection should have elements but is null!", coll1 != null);
assertEquals("Collection has incorrect number of elements", coll1.size(), 3);
PCFKListElement elem1 = (PCFKListElement) coll1.get(0);
PCFKListElement elem2 = (PCFKListElement) coll1.get(1);
PCFKListElement elem3 = (PCFKListElement) coll1.get(2);
assertEquals("First retrieved element is incorrect", "First", elem1.getName());
assertEquals("Second retrieved element is incorrect", "Last", elem2.getName());
assertEquals("Third retrieved element is incorrect", "Middle", elem3.getName());
// Remove an element
coll1.remove(elem1);
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown while interrogating data for 1-N unidirectional ordered List : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
Object elem1Id = null;
Object elem2Id = null;
try {
tx.begin();
// Retrieve the holder and check the ordered data
ListHolder holder = (ListHolder) pm.getObjectById(holderId);
List coll1 = holder.getFkListPCOrdered();
assertTrue("Collection should have elements but is null!", coll1 != null);
assertEquals("Collection has incorrect number of elements", coll1.size(), 2);
PCFKListElement elem1 = (PCFKListElement) coll1.get(0);
PCFKListElement elem2 = (PCFKListElement) coll1.get(1);
assertEquals("First retrieved element (after remove) is incorrect", "Last", elem1.getName());
assertEquals("Second retrieved element (after remove) is incorrect", "Middle", elem2.getName());
elem1Id = pm.getObjectId(elem1);
elem2Id = pm.getObjectId(elem2);
// Remove the holder
pm.deletePersistent(holder);
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown while interrogating data for 1-N unidirectional ordered List : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Try to retrieve the holder
try {
pm.getObjectById(holderId);
fail("Retrieved ListHolder yet should have been deleted");
} catch (JDOObjectNotFoundException onfe) {
// Expected
}
try {
PCFKListElement elem1 = (PCFKListElement) pm.getObjectById(elem1Id);
assertEquals("Element 1 has incorrect name", "Last", elem1.getName());
} catch (JDOObjectNotFoundException onfe) {
fail("Failed to retrieve list element1 when should still be persistent");
}
try {
PCFKListElement elem2 = (PCFKListElement) pm.getObjectById(elem2Id);
assertEquals("Element 2 has incorrect name", "Middle", elem2.getName());
} catch (JDOObjectNotFoundException onfe) {
fail("Failed to retrieve list element2 when should still be persistent");
}
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown while interrogating data for 1-N unidirectional ordered List : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(ListHolder.class);
clean(PCFKListElement.class);
}
}
Aggregations