use of org.jpox.samples.one_many.collection.ListHolder in project tests by datanucleus.
the class PersistenceModelsTest method testClassWithOnlyContainerFieldsInFetchPlan.
/**
* Test if a class with only a container (Collection or List) fields works with
* query, delete, getObjectById, persistent.
*/
public void testClassWithOnlyContainerFieldsInFetchPlan() {
try {
ListHolder[] objs = new ListHolder[5];
for (int i = 0; i < objs.length; i++) {
objs[i] = new ListHolder(i + 1);
}
Object[] ids = new Object[objs.length];
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
try {
tx.begin();
pm.makePersistentAll(objs);
tx.commit();
for (int i = 0; i < objs.length; i++) {
ids[i] = pm.getObjectId(objs[i]);
}
// Retrieve an object (getObjectById)
tx.begin();
for (int i = 0; i < objs.length; i++) {
assertEquals(JDOHelper.getObjectId(objs[i]).toString(), JDOHelper.getObjectId(pm.getObjectById(ids[i], true)).toString());
}
tx.commit();
// Query the objects
tx.begin();
Collection c = (Collection) pm.newQuery(ListHolder.class).execute();
assertEquals(c.size(), objs.length);
tx.commit();
// Delete an object
ListHolder objToDel = new ListHolder(6);
tx.begin();
pm.makePersistent(objToDel);
tx.commit();
Object idToDel = pm.getObjectId(objToDel);
tx.begin();
pm.deletePersistent(pm.getObjectById(idToDel, true));
tx.commit();
tx.begin();
boolean success = false;
try {
pm.getObjectById(idToDel, true);
} catch (JDOObjectNotFoundException e) {
success = true;
}
assertTrue("should have been raised exception", success);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(ListHolder.class);
}
}
use of org.jpox.samples.one_many.collection.ListHolder in project tests by datanucleus.
the class RelationshipTest method test1toNUnidirFKListInheritanceTarget.
/**
* Test 1-N uni FK List relation having subclasses on the target side (1=source;N=target)
*/
public void test1toNUnidirFKListInheritanceTarget() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object holderId = null;
// Persist some objects
try {
tx.begin();
ListHolder holder = new ListHolder();
PCFKListElementSub1 sub1 = new PCFKListElementSub1("Element 1");
PCFKListElementSub2 sub2 = new PCFKListElementSub2("Element 2");
holder.getFkListPC().add(sub1);
holder.getFkListPC().add(sub2);
pm.makePersistent(holder);
tx.commit();
holderId = pm.getObjectId(holder);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Query the objects
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
ListHolder holder = (ListHolder) pm.getObjectById(holderId, true);
List elements = holder.getFkListPC();
assertEquals("Number of elements in 1-N unidir FK with inheritance is incorrect", 2, elements.size());
Object element = null;
element = elements.get(0);
assertTrue("First element of 1-N uni FK list is incorrect type", element instanceof PCFKListElementSub1);
element = elements.get(1);
assertTrue("Second element of 1-N uni FK list is incorrect type", element instanceof PCFKListElementSub2);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.toString());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(ListHolder.class);
clean(PCFKListElementSub1.class);
clean(PCFKListElementSub1.class);
}
}
use of org.jpox.samples.one_many.collection.ListHolder in project tests by datanucleus.
the class RelationshipTest method test1toNUnidirListSharedFK.
/**
* Test case for 1-N unidirectional relationships using a shared foreign-key (List).
*/
public void test1toNUnidirListSharedFK() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// Create sample data
Object holderId = null;
try {
tx.begin();
ListHolder holder = new ListHolder();
PCFKListElementShared elem1 = new PCFKListElementShared("First");
PCFKListElementShared elem2 = new PCFKListElementShared("Second");
PCFKListElementShared elem3 = new PCFKListElementShared("Third");
holder.getFkListPCShared1().add(elem1);
holder.getFkListPCShared1().add(elem3);
holder.getFkListPCShared2().add(elem2);
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 List with shared foreign-key : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the record and check the data
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
ListHolder holder = (ListHolder) pm.getObjectById(holderId);
List coll1 = holder.getFkListPCShared1();
List coll2 = holder.getFkListPCShared2();
assertTrue("Collection 1 should have elements but is null!", coll1 != null);
assertEquals("Collection 1 has incorrect number of elements", coll1.size(), 2);
assertTrue("Collection 2 should have elements but is null!", coll2 != null);
assertEquals("Collection 2 has incorrect number of elements", coll2.size(), 1);
boolean hasElem1 = false;
boolean hasElem2 = false;
boolean hasElem3 = false;
Iterator iter1 = coll1.iterator();
while (iter1.hasNext()) {
PCFKListElementShared elem = (PCFKListElementShared) iter1.next();
if (elem.getName().equals("First")) {
hasElem1 = true;
} else if (elem.getName().equals("Third")) {
hasElem3 = true;
}
}
Iterator iter2 = coll2.iterator();
while (iter2.hasNext()) {
PCFKListElementShared elem = (PCFKListElementShared) iter2.next();
if (elem.getName().equals("Second")) {
hasElem2 = true;
}
}
assertTrue("Collection 1 is missing element 1", hasElem1);
assertTrue("Collection 1 is missing element 3", hasElem3);
assertTrue("Collection 2 is missing element 2", hasElem2);
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown while interrogating data for 1-N unidirectional List with shared foreign-key : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(ListHolder.class);
clean(PCFKListElementShared.class);
}
}
use of org.jpox.samples.one_many.collection.ListHolder 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