use of org.jpox.samples.one_many.map.MapHolder in project tests by datanucleus.
the class RelationshipTest method test1toNUnidirFKMap.
/**
* Test case for 1-N unidir FK, using a Map<String, PC>.
*/
public void test1toNUnidirFKMap() 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
MapHolder holder = new MapHolder("First");
MapFKValueItem value1 = new MapFKValueItem("Value 1", "First value", "1");
MapFKValueItem value2 = new MapFKValueItem("Value 2", "Second value", "2");
holder.getFkMapKey().put(value1.getKey(), value1);
holder.getFkMapKey().put(value2.getKey(), value2);
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 (Map) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
// Check the retrieval of the owner and values
tx = pm.currentTransaction();
try {
tx.begin();
MapHolder holder = (MapHolder) pm.getObjectById(holderId);
assertNotNull("Unable to retrieve container object for 1-N unidir FK relationship (Map)", holder);
Collection values = holder.getFkMapKey().values();
Iterator iter = values.iterator();
while (iter.hasNext()) {
iter.next();
}
assertEquals("Number of values in 1-N unidir FK relationship (Map) is wrong", 2, values.size());
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while querying 1-N unidir FK relationships (Map) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
pm.close();
} finally {
// Clean out our data
clean(MapHolder.class);
clean(MapFKValueItem.class);
}
}
use of org.jpox.samples.one_many.map.MapHolder in project tests by datanucleus.
the class RelationshipTest method test1toNUnidirFKMapToSelf.
/**
* Test case for 1-N inverse unidirectional, using a Map.
* Hotel has a collection of hotels. innerHotels knows nothing about Hotel.
* This should add a FK on the Hotel table.
*/
public void test1toNUnidirFKMapToSelf() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object holder1Id = null;
// Check the persistence of owner and elements
try {
tx.begin();
MapHolder holder1 = new MapHolder("First");
MapHolder holder2 = new MapHolder("Second");
MapHolder holder3 = new MapHolder("Third");
holder1.getFkMapKey2().put(holder2.getName(), holder2);
holder1.getFkMapKey2().put(holder3.getName(), holder3);
pm.makePersistent(holder1);
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 (Map) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
// Check the retrieval of the owner and values
tx = pm.currentTransaction();
try {
tx.begin();
MapHolder holder = (MapHolder) pm.getObjectById(holder1Id);
assertNotNull("Unable to retrieve container object for 1-N unidir FK relationship (Map)", holder);
Collection innerHolders = holder.getFkMapKey2().values();
assertEquals("Number of values in 1-N unidir FK relationship (Map) 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 relationships (Map) : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
pm.close();
} finally {
// Clean out our data
clean(MapHolder.class);
}
}
use of org.jpox.samples.one_many.map.MapHolder in project tests by datanucleus.
the class JDOQLEvaluatorTest method testFilterMapContainsKeyNonPC.
/**
* Test of filter with mapField.containsKey(nonPC).
*/
public void testFilterMapContainsKeyNonPC() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create some instances to query over
List<MapHolder> instances = new ArrayList<>();
MapHolder holder1 = new MapHolder("First");
MapHolder holder2 = new MapHolder("Second");
MapHolder holder3 = new MapHolder("Third");
holder1.getJoinMapNonNon().put("First", "First Element");
holder2.getJoinMapNonNon().put("First", "First Element");
holder2.getJoinMapNonNon().put("Second", "Second Element");
holder3.getJoinMapNonNon().put("Second", "Second Element");
holder3.getJoinMapNonNon().put("Third", "Third Element");
instances.add(holder1);
instances.add(holder2);
instances.add(holder3);
// Compile the query
JDOQuery q = (JDOQuery) pm.newQuery(MapHolder.class, "joinMapNonNon.containsKey('Third')");
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());
MapHolder holder = (MapHolder) 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.map.MapHolder in project tests by datanucleus.
the class JDOQLEvaluatorTest method testFilterMapContainsValueNonPC.
/**
* Test of filter with mapField.containsValue(nonPC).
*/
public void testFilterMapContainsValueNonPC() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create some instances to query over
List<MapHolder> instances = new ArrayList<>();
MapHolder holder1 = new MapHolder("First");
MapHolder holder2 = new MapHolder("Second");
MapHolder holder3 = new MapHolder("Third");
holder1.getJoinMapNonNon().put("First", "First Element");
holder2.getJoinMapNonNon().put("First", "First Element");
holder2.getJoinMapNonNon().put("Second", "Second Element");
holder3.getJoinMapNonNon().put("Second", "Second Element");
holder3.getJoinMapNonNon().put("Third", "Third Element");
instances.add(holder1);
instances.add(holder2);
instances.add(holder3);
// Compile the query
JDOQuery q = (JDOQuery) pm.newQuery(MapHolder.class, "joinMapNonNon.containsValue('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());
MapHolder holder = (MapHolder) 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.map.MapHolder in project tests by datanucleus.
the class AttachDetachTest method testPersistWithDetachedRelativeInMap.
/**
* Test using a Map<String,PC> and tests the persistence of a value on its own
* then the attach of the value in a Map, as well as detecting new values added
* while the map was detached.
*/
public void testPersistWithDetachedRelativeInMap() throws Exception {
try {
PersistenceManager pm;
Transaction tx;
MapValueItem detachedItem1 = null;
// Create an instance, persist it and detach it
pm = newPM();
tx = pm.currentTransaction();
try {
tx.begin();
MapValueItem item1 = new MapValueItem("First", "First item");
pm.makePersistent(item1);
pm.getFetchPlan().addGroup(FetchPlan.ALL);
detachedItem1 = (MapValueItem) pm.detachCopy(item1);
tx.commit();
} catch (JDOUserException ue) {
LOG.error("Exception during test", ue);
fail("Exception thrown while performing test : " + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
MapHolder container = new MapHolder();
container.getJoinMapNonPC().put("Original", detachedItem1);
// To complete the test, we add a new - not yet persisted - item
container.getJoinMapNonPC().put("New", new MapValueItem("Second", "Second item"));
MapValueItem detachedItem3 = null;
MapHolder detachedContainer = null;
Object containerId = null;
// Persist the new container, with the detached item, and the new item
pm = newPM();
tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(container);
// Detach the container and check its contents
pm.getFetchPlan().addGroup(FetchPlan.ALL);
detachedContainer = (MapHolder) pm.detachCopy(container);
assertTrue("Detached container doesnt contain first item ", detachedContainer.getJoinMapNonPC().get("Original") != null);
assertTrue("Detached container doesnt contain second item ", detachedContainer.getJoinMapNonPC().get("New") != null);
// Persist and detach a new item
MapValueItem item3 = new MapValueItem("Third", "Third Item");
pm.makePersistent(item3);
detachedItem3 = (MapValueItem) pm.detachCopy(item3);
tx.commit();
containerId = JDOHelper.getObjectId(container);
} catch (JDOUserException ue) {
LOG.error("Exception during test", ue);
fail("Exception thrown while performing test : " + ue.getMessage());
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
// Add a detached object to the Map, and add a new (transient) object to the Map.
// Attach the updated container object with its 4 map values.
detachedContainer.getJoinMapNonPC().put("Next", detachedItem3);
detachedContainer.getJoinMapNonPC().put("Latest", new MapValueItem("Fourth", "Fourth Item"));
pm = newPM();
tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(detachedContainer);
tx.commit();
} catch (JDOUserException ue) {
LOG.error("Exception during test", ue);
fail("Exception thrown while performing test : " + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Check, whether our container has all map values
pm = newPM();
tx = pm.currentTransaction();
try {
tx.begin();
container = (MapHolder) pm.getObjectById(containerId);
assertTrue("Container doesnt contain first item ", container.getJoinMapNonPC().get("Original") != null);
assertTrue("Container doesnt contain second item ", container.getJoinMapNonPC().get("New") != null);
assertTrue("Container doesnt contain second item ", container.getJoinMapNonPC().get("Next") != null);
assertTrue("Container doesnt contain second item ", container.getJoinMapNonPC().get("Latest") != null);
tx.commit();
} catch (JDOUserException ue) {
LOG.error("Exception during test", ue);
fail("Exception thrown while performing test : " + ue.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(MapHolder.class);
}
}
Aggregations