use of org.jpox.samples.one_many.map.MapValueItem in project tests by datanucleus.
the class SerializationTest method testSerialisedMapValues.
/**
* Test for serialisation of map values.
*/
public void testSerialisedMapValues() {
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_SERIALISED_MAP_VALUE)) {
return;
}
try {
Object holderId = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
// Persist the object with serialised fields
try {
tx.begin();
MapHolder holder = new MapHolder("Holder(4)");
MapValueItem val = new MapValueItem("Value 1", "Value 1 Desc");
holder.getJoinMapNonPCSerial().put("1", val);
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 map values field : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the object
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
MapHolder holder = (MapHolder) pm.getObjectById(holderId);
assertTrue("Holder of serialised map values could not be retrieved!", holder != null);
assertTrue("Holder name is incorrect", holder.getName().equals("Holder(4)"));
assertEquals("Number of serialised values is incorrect", holder.getJoinMapNonPCSerial().size(), 1);
MapValueItem val = (MapValueItem) holder.getJoinMapNonPCSerial().values().iterator().next();
assertEquals("Serialised map value has incorrect description", val.getName(), "Value 1");
// Add 2 new values and remove original
holder.getJoinMapNonPCSerial().clear();
holder.getJoinMapNonPCSerial().put("2", new MapValueItem("Value 2", "Value 2 desc"));
holder.getJoinMapNonPCSerial().put("3", new MapValueItem("Value 3", "Value 3 desc"));
holder.setName("Holder(5)");
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception thrown in test", e);
fail("Exception thrown while retrieving object with serialised map values : " + 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();
MapHolder holder = (MapHolder) pm.getObjectById(holderId);
assertTrue("Holder of serialised map values could not be retrieved!", holder != null);
assertTrue("Holder name is incorrect", holder.getName().equals("Holder(5)"));
assertEquals("Number of serialised values is incorrect", holder.getJoinMapNonPCSerial().size(), 2);
Iterator valuesIter = holder.getJoinMapNonPCSerial().values().iterator();
boolean containsVal2 = false;
boolean containsVal3 = false;
while (valuesIter.hasNext()) {
MapValueItem val = (MapValueItem) valuesIter.next();
if (val.getName().equals("Value 2")) {
containsVal2 = true;
} else if (val.getName().equals("Value 3")) {
containsVal3 = true;
}
}
assertTrue("Element 2 is missing from map with serialised values", containsVal2);
assertTrue("Element 3 is missing from map with serialised values", containsVal3);
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception thrown in test", e);
fail("Exception thrown while retrieving object with serialised map values field : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean up our data
clean(MapHolder.class);
clean(MapValueItem.class);
}
}
use of org.jpox.samples.one_many.map.MapValueItem in project tests by datanucleus.
the class JDOQLBasicTest method testMultipleActiveQueryMapGet.
/**
* Test for multithreaded queries for Map.get().
*/
public void testMultipleActiveQueryMapGet() {
int THREAD_SIZE = 40;
Thread[] threads = new Thread[THREAD_SIZE];
MultithreadRunnerMapGet[] runner = new MultithreadRunnerMapGet[THREAD_SIZE];
PersistenceManager pm = pmf.getPersistenceManager();
try {
Object idHolder1 = null;
Object idValue1 = null;
Object idValue2 = null;
Object idValue3 = null;
Transaction tx = pm.currentTransaction();
try {
tx.begin();
MapHolder holder1 = new MapHolder("First");
MapHolder holder2 = new MapHolder("Second");
MapValueItem item1 = new MapValueItem("Item 1", "First Item");
MapValueItem item2 = new MapValueItem("Item 2", "Second Item");
MapValueItem item3 = new MapValueItem("Item 3", "Third Item");
holder1.getJoinMapNonPC().put(item1.getName(), item1);
holder1.getJoinMapNonPC().put(item2.getName(), item2);
holder1.getJoinMapNonPC().put(item3.getName(), item3);
holder2.getJoinMapNonPC().put(item3.getName(), item3);
pm.makePersistent(holder1);
pm.makePersistent(holder2);
tx.commit();
idHolder1 = JDOHelper.getObjectId(holder1);
idValue1 = JDOHelper.getObjectId(item1);
idValue2 = JDOHelper.getObjectId(item2);
idValue3 = JDOHelper.getObjectId(item3);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
try {
for (int i = 0; i < THREAD_SIZE; i++) {
runner[i] = new MultithreadRunnerMapGet(idHolder1, idValue1, idValue2, idValue3);
threads[i] = new Thread(runner[i]);
threads[i].start();
}
for (int i = 0; i < THREAD_SIZE; i++) {
threads[i].join();
if (runner[i].exception != null) {
LOG.error("Exception in query", runner[i].exception);
fail("Map.get thread runner failed; consult the log for details : " + runner[i].exception.getMessage());
}
}
} catch (RuntimeException e) {
fail(e.getMessage());
} catch (InterruptedException e) {
fail(e.getMessage());
}
} finally {
clean(MapHolder.class);
clean(MapValueItem.class);
}
}
use of org.jpox.samples.one_many.map.MapValueItem 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