use of org.jpox.samples.one_many.collection.SetHolder in project tests by datanucleus.
the class RelationshipTest method test1toNUnidirFKToSelf.
/**
* Test case for 1-N inverse unidirectional to itself.
* Hotel has a collection of Hotels. innerHotels knows nothing about Hotel owner.
* This should add a FK on the Hotel table.
*/
public void test1toNUnidirFKToSelf() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object holder1Id = null;
// Check the persistence of owner and elements
try {
tx.begin();
SetHolder holder1 = new SetHolder("First");
SetHolder holder2 = new SetHolder("Second");
SetHolder holder3 = new SetHolder("Third");
pm.makePersistent(holder1);
pm.makePersistent(holder2);
pm.makePersistent(holder3);
holder1.getFkSetPC2().add(holder2);
holder1.getFkSetPC2().add(holder3);
tx.commit();
holder1Id = pm.getObjectId(holder1);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while creating 1-N unidir FK self-referring relationships : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
pm.close();
pm = pmf.getPersistenceManager();
// Check the retrieval of the owner and elements
tx = pm.currentTransaction();
try {
tx.begin();
SetHolder holder = (SetHolder) pm.getObjectById(holder1Id, true);
assertNotNull("Unable to retrieve container object for 1-N unidir FK relationship", holder);
Collection innerHolders = holder.getFkSetPC2();
assertEquals("Number of elements in 1-N unidir FK relationship is incorrect", 2, innerHolders.size());
Iterator iter = innerHolders.iterator();
while (iter.hasNext()) {
iter.next();
}
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while querying 1-N unidir FK self-referring relationships : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
pm.close();
} finally {
// Clean out our data
clean(SetHolder.class);
}
}
use of org.jpox.samples.one_many.collection.SetHolder in project tests by datanucleus.
the class RelationshipTest method test1toNunidirFK.
/**
* Test case for 1-N unidir using FK.
*/
public void test1toNunidirFK() 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
SetHolder holder = new SetHolder("First");
PCFKSetElement elem1 = new PCFKSetElement("Element 1");
PCFKSetElement elem2 = new PCFKSetElement("Element 2");
holder.getFkSetPC().add(elem1);
holder.getFkSetPC().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 uni FK relationships : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check the retrieval of the owner and elements
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
SetHolder holder = (SetHolder) pm.getObjectById(holderId);
assertNotNull("Unable to retrieve container object for 1-N uni FK relationship", holder);
Collection elements = holder.getFkSetPC();
assertNotNull("Elements in holder are null!", elements);
assertEquals("Number of elements in holder is incorrect", 2, elements.size());
Iterator roomsIter = elements.iterator();
while (roomsIter.hasNext()) {
roomsIter.next();
}
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while querying 1-N uni FK relationships : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
pm.close();
} finally {
// Clean out our data
clean(SetHolder.class);
clean(PCFKSetElement.class);
}
}
use of org.jpox.samples.one_many.collection.SetHolder in project tests by datanucleus.
the class RelationshipTest method test1toNUnidirFKSetInheritanceTarget.
/**
* Test 1-N uni FK Set relation having subclasses on the target side (1=source;N=target)
*/
public void test1toNUnidirFKSetInheritanceTarget() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object holderId = null;
// Persist some objects
try {
tx.begin();
SetHolder holder = new SetHolder("First");
PCFKSetElementSub1 sub1 = new PCFKSetElementSub1("Element 1");
PCFKSetElementSub2 sub2 = new PCFKSetElementSub2("Element 2");
holder.getFkSetPC().add(sub1);
holder.getFkSetPC().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();
SetHolder holder = (SetHolder) pm.getObjectById(holderId, true);
Collection elements = holder.getFkSetPC();
assertEquals("Number of elements in 1-N unidir FK with inheritance is incorrect", 2, elements.size());
boolean containsElem1 = false;
boolean containsElem2 = false;
for (Iterator iter = elements.iterator(); iter.hasNext(); ) {
Object element = iter.next();
if (element instanceof PCFKSetElementSub1) {
containsElem1 = true;
} else if (element instanceof PCFKSetElementSub2) {
containsElem2 = true;
}
}
assertTrue(containsElem1);
assertTrue(containsElem2);
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(SetHolder.class);
clean(PCFKSetElementSub1.class);
clean(PCFKSetElementSub1.class);
}
}
use of org.jpox.samples.one_many.collection.SetHolder in project tests by datanucleus.
the class RelationshipTest method test1toNUnidirSetSharedJoin.
/**
* Test case for 1-N unidirectional relationships using a shared join table (Set).
*/
public void test1toNUnidirSetSharedJoin() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// Create sample data
Object holderId = null;
try {
tx.begin();
SetHolder holder = new SetHolder();
PCJoinElement elem1 = new PCJoinElement("First");
PCJoinElement elem2 = new PCJoinElement("Second");
PCJoinElement elem3 = new PCJoinElement("Third");
holder.getJoinSetPCShared1().add(elem1);
holder.getJoinSetPCShared1().add(elem3);
holder.getJoinSetPCShared2().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 Set with shared join table : " + 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();
SetHolder holder = (SetHolder) pm.getObjectById(holderId);
Set coll1 = holder.getJoinSetPCShared1();
Set coll2 = holder.getJoinSetPCShared2();
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()) {
PCJoinElement elem = (PCJoinElement) iter1.next();
if (elem.getName().equals("First")) {
hasElem1 = true;
} else if (elem.getName().equals("Third")) {
hasElem3 = true;
}
}
Iterator iter2 = coll2.iterator();
while (iter2.hasNext()) {
PCJoinElement elem = (PCJoinElement) 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 Set with shared join table : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(SetHolder.class);
clean(PCJoinElement.class);
}
}
use of org.jpox.samples.one_many.collection.SetHolder in project tests by datanucleus.
the class CollectionPrimitiveTest method testCollectionNonPCSerialised.
/**
* Test of collections of Strings and Dates persisted serialised.
*/
public void testCollectionNonPCSerialised() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
try {
tx.begin();
SetHolder coll = new SetHolder();
coll.getSetNonPCSerial1().add(new String("First string"));
coll.getSetNonPCSerial1().add(new String("Second string"));
coll.getSetNonPCSerial1().add(new String("Third string"));
coll.getSetNonPCSerial2().add(new java.util.Date(1000));
coll.getSetNonPCSerial2().add(new java.util.Date(120000000));
coll.getSetNonPCSerial2().add(new java.util.Date());
pm.makePersistent(coll);
id = pm.getObjectId(coll);
tx.commit();
// Retrieve the container object
tx.begin();
coll = (SetHolder) pm.getObjectById(id, false);
assertNotNull("SetHolder", coll);
Collection dates = coll.getSetNonPCSerial2();
assertNotNull("Date Collection", dates);
assertEquals("Expected number of dates", 3, dates.size());
Iterator iter = dates.iterator();
while (iter.hasNext()) {
Object o = iter.next();
assertTrue("object [" + o + "] should be a Date." + o, o instanceof java.util.Date);
}
Collection strings = coll.getSetNonPCSerial1();
assertNotNull("String collection", strings);
assertEquals("strings.size()", 3, strings.size());
iter = strings.iterator();
while (iter.hasNext()) {
Object o = iter.next();
assertTrue("object [" + o + "] should be a String." + o, o instanceof String);
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out the data we have created
clean(SetHolder.class);
}
}
Aggregations