use of org.jpox.samples.one_many.collection.SetHolder in project tests by datanucleus.
the class JDOQLEvaluatorTest method testFilterCollectionContainsNonPC.
/**
* Test of filter with collectionField.contains(nonPC).
*/
public void testFilterCollectionContainsNonPC() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create some instances to query over
List<SetHolder> instances = new ArrayList<>();
SetHolder holder1 = new SetHolder("First");
SetHolder holder2 = new SetHolder("Second");
SetHolder holder3 = new SetHolder("Third");
holder1.getJoinSetNonPC1().add("First Element");
holder2.getJoinSetNonPC1().add("First Element");
holder2.getJoinSetNonPC1().add("Second Element");
holder3.getJoinSetNonPC1().add("Second Element");
holder3.getJoinSetNonPC1().add("Third Element");
instances.add(holder1);
instances.add(holder2);
instances.add(holder3);
// Compile the query
JDOQuery q = (JDOQuery) pm.newQuery(SetHolder.class, "joinSetNonPC1.contains('Third Element')");
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());
SetHolder holder = (SetHolder) results.get(0);
assertEquals("Result instance has wrong name", "Third", holder.getName());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown during query execution " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.jpox.samples.one_many.collection.SetHolder in project tests by datanucleus.
the class SerializationTest method testSerialisedCollectionElements.
/**
* Test for serialisation of collection elements.
*/
public void testSerialisedCollectionElements() {
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_SERIALISED_COLLECTION_ELEMENT)) {
return;
}
try {
Object holderId = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// Persist the object with serialised fields
try {
tx.begin();
SetHolder holder = new SetHolder("Holder(3)");
PCJoinElement elem = new PCJoinElement("Element 1");
holder.getJoinSetPCSerial().add(elem);
pm.makePersistent(holder);
tx.commit();
holderId = pm.getObjectId(holder);
} catch (Exception e) {
LOG.error(">> Exception thrown in test", e);
fail("Exception thrown while persisted object with serialised collection elements field : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
SetHolder holder = (SetHolder) pm.getObjectById(holderId);
assertTrue("Holder of serialised collection elements could not be retrieved!", holder != null);
assertTrue("Holder name is incorrect", holder.getName().equals("Holder(3)"));
assertEquals("Number of serialised elements is incorrect", holder.getJoinSetPCSerial().size(), 1);
PCJoinElement elem = holder.getJoinSetPCSerial().iterator().next();
assertEquals("Serialised collection element has incorrect description", elem.getName(), "Element 1");
// Add 2 new elements and remove original
holder.getJoinSetPCSerial().clear();
holder.getJoinSetPCSerial().add(new PCJoinElement("Element 2"));
holder.getJoinSetPCSerial().add(new PCJoinElement("Element 3"));
holder.setName("Holder(4)");
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception thrown in test", e);
fail("Exception thrown while retrieving object with serialised collection elements : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object again to check the most recent update
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
SetHolder holder = (SetHolder) pm.getObjectById(holderId);
assertTrue("Holder of serialised collection elements could not be retrieved!", holder != null);
assertTrue("Holder name is incorrect", holder.getName().equals("Holder(4)"));
assertEquals("Number of serialised elements is incorrect", holder.getJoinSetPCSerial().size(), 2);
Iterator elementsIter = holder.getJoinSetPCSerial().iterator();
boolean containsElem2 = false;
boolean containsElem3 = false;
while (elementsIter.hasNext()) {
PCJoinElement elem = (PCJoinElement) elementsIter.next();
if (elem.getName().equals("Element 2")) {
containsElem2 = true;
} else if (elem.getName().equals("Element 3")) {
containsElem3 = true;
}
}
assertTrue("Element 2 is missing from collection with serialised elements", containsElem2);
assertTrue("Element 3 is missing from collection with serialised elements", containsElem3);
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception thrown in test", e);
fail("Exception thrown while retrieving object with serialised collection elements field : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean up our data
clean(SetHolder.class);
}
}
use of org.jpox.samples.one_many.collection.SetHolder in project tests by datanucleus.
the class RelationshipTest method test1toNUnidirSetSharedFK.
/**
* Test case for 1-N unidirectional relationships using a shared foreign-key (Set).
*/
public void test1toNUnidirSetSharedFK() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// Create sample data
Object holderId = null;
try {
tx.begin();
SetHolder holder = new SetHolder();
PCFKSetElementShared elem1 = new PCFKSetElementShared("First");
PCFKSetElementShared elem2 = new PCFKSetElementShared("Second");
PCFKSetElementShared elem3 = new PCFKSetElementShared("Third");
holder.getFkSetPCShared1().add(elem1);
holder.getFkSetPCShared1().add(elem3);
holder.getFkSetPCShared2().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 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();
SetHolder holder = (SetHolder) pm.getObjectById(holderId);
Set coll1 = holder.getFkSetPCShared1();
Set coll2 = holder.getFkSetPCShared2();
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()) {
PCFKSetElementShared elem = (PCFKSetElementShared) iter1.next();
if (elem.getName().equals("First")) {
hasElem1 = true;
} else if (elem.getName().equals("Third")) {
hasElem3 = true;
}
}
Iterator iter2 = coll2.iterator();
while (iter2.hasNext()) {
PCFKSetElementShared elem = (PCFKSetElementShared) 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 foreign-key : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(SetHolder.class);
clean(PCFKSetElementShared.class);
}
}
use of org.jpox.samples.one_many.collection.SetHolder in project tests by datanucleus.
the class CollectionPrimitiveTest method testCollectionNonPCWithoutJoinTable.
/**
* Test of collections of Strings and Dates persisted without join table, hence serialised.
*/
public void testCollectionNonPCWithoutJoinTable() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
try {
tx.begin();
SetHolder coll = new SetHolder();
coll.getSetNonPC1().add(new String("First string"));
coll.getSetNonPC1().add(new String("Second string"));
coll.getSetNonPC1().add(new String("Third string"));
coll.getSetNonPC2().add(new java.util.Date(1000));
coll.getSetNonPC2().add(new java.util.Date(120000000));
coll.getSetNonPC2().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("CollectionNonPC", coll);
Collection dates = coll.getSetNonPC2();
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.getSetNonPC1();
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);
}
}
use of org.jpox.samples.one_many.collection.SetHolder in project tests by datanucleus.
the class CollectionPrimitiveTest method testCollectionNonPCUsingJoinTable.
/**
* Test of collections of Strings and Dates persisted into JoinTable.
*/
public void testCollectionNonPCUsingJoinTable() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
try {
tx.begin();
SetHolder coll = new SetHolder();
coll.getJoinSetNonPC1().add(new String("First string"));
coll.getJoinSetNonPC1().add(new String("Second string"));
coll.getJoinSetNonPC1().add(new String("Third string"));
coll.getJoinSetNonPC2().add(new java.util.Date(1000));
coll.getJoinSetNonPC2().add(new java.util.Date(120000000));
coll.getJoinSetNonPC2().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.getJoinSetNonPC2();
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.getJoinSetNonPC1();
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