use of javax.jdo.Extent in project tests by datanucleus.
the class CompoundIdentityTest method testOneToManyUniJoinTableDouble.
/**
* Test of compound identity 1-N relations using join table with double compound elements.
*/
public void testOneToManyUniJoinTableDouble() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
CompoundDoubleTarget[] elements = new CompoundDoubleTarget[6];
try {
tx.begin();
CompoundHolder holder1 = new CompoundHolder("First Holder");
CompoundRelated related1 = new CompoundRelated("First Related");
elements[0] = new CompoundDoubleTarget(holder1, related1, 1.0);
elements[1] = new CompoundDoubleTarget(holder1, related1, 2.0);
elements[2] = new CompoundDoubleTarget(holder1, related1, 3.0);
elements[3] = new CompoundDoubleTarget(holder1, related1, 4.0);
elements[4] = new CompoundDoubleTarget(holder1, related1, 5.0);
elements[5] = new CompoundDoubleTarget(holder1, related1, 6.0);
holder1.getList2().add(elements[0]);
holder1.getList2().add(elements[1]);
holder1.getList2().add(elements[2]);
holder1.getList2().add(elements[3]);
holder1.getList2().add(elements[4]);
holder1.getList2().add(elements[5]);
pm.makePersistent(holder1);
tx.commit();
id = pm.getObjectId(holder1);
tx.begin();
CompoundHolder holder2 = (CompoundHolder) pm.getObjectById(id, true);
assertEquals(6, holder2.getList2().size());
assertEquals(elements[0], holder2.getList2().get(0));
assertEquals(elements[1], holder2.getList2().get(1));
assertEquals(elements[2], holder2.getList2().get(2));
assertEquals(elements[3], holder2.getList2().get(3));
assertEquals(elements[4], holder2.getList2().get(4));
assertEquals(elements[5], holder2.getList2().get(5));
tx.commit();
} catch (Exception e) {
LOG.error(e);
e.printStackTrace();
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Extent ex = pm.getExtent(CompoundHolder.class);
Iterator iter = ex.iterator();
while (iter.hasNext()) {
CompoundHolder holder = (CompoundHolder) iter.next();
holder.getList1().clear();
holder.getList2().clear();
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
clean(CompoundHolder.class);
clean(CompoundDoubleTarget.class);
clean(CompoundRelated.class);
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class SCOCollectionTests method checkQuery.
/**
* Utility for checking the use of queries with JDOQL on a container.
* Uses contains(), isEmpty() operations.
* @param pmf The PersistenceManager factory
* @param container_class The container class e.g HashSetNormal
* @param item_class_parent The parent element class
* @param db_vendor_id Name of RDBMS. (TODO Remove this)
*/
public static void checkQuery(PersistenceManagerFactory pmf, Class container_class, Class item_class_parent, String db_vendor_id) throws Exception {
int NO_OF_ITEMS = 5;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create a container
CollectionHolder container = null;
container = createContainer(container_class, container);
// Create a few items and add them to a Collection
java.util.Collection c = new java.util.HashSet();
for (int i = 0; i < NO_OF_ITEMS; i++) {
Object item = SCOHolderUtilities.createItemParent(item_class_parent, "Item " + i, 0.00 + (10.00 * i), i);
c.add(item);
}
// Add the items to the container
SCOHolderUtilities.addItemsToCollection(container, c);
pm.makePersistent(container);
JDOHelper.getObjectId(container);
tx.commit();
} catch (JDOUserException e) {
LOG.error("Exception in test", e);
Assert.assertTrue("Exception thrown while creating " + container_class.getName() + " " + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Query the containers
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// TODO : Remove this restriction when MySQL supports subqueries
if (db_vendor_id == null || !db_vendor_id.equals("mysql")) {
Extent e1 = pm.getExtent(container_class, true);
Query q1 = pm.newQuery(e1, "items.isEmpty()");
java.util.Collection c1 = (java.util.Collection) q1.execute();
Assert.assertTrue("No of containers that are empty is incorrect (" + c1.size() + ") : should have been 0", c1.size() == 0);
}
// Get all containers containing a particular value
Extent e2 = pm.getExtent(container_class, true);
Query q2 = pm.newQuery(e2, "items.contains(element) && element.name==\"Item 1\"");
q2.declareImports("import " + item_class_parent.getName());
q2.declareVariables(item_class_parent.getName() + " element");
java.util.Collection c2 = (java.util.Collection) q2.execute();
Assert.assertTrue("No of containers with the specified value is incorrect (" + c2.size() + ") : should have been 1", c2.size() == 1);
tx.commit();
} catch (JDOUserException e) {
e.printStackTrace();
Assert.assertTrue("Exception thrown while querying container objects " + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Query the containers for the container containing an element.
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Get all elements
Extent e = pm.getExtent(item_class_parent, true);
Iterator e_iter = e.iterator();
while (e_iter.hasNext()) {
Object obj = e_iter.next();
Query q = pm.newQuery(pm.getExtent(container_class, true), "items.contains(element)");
q.declareImports("import " + item_class_parent.getName());
q.declareParameters(item_class_parent.getName() + " element");
java.util.Collection c = (java.util.Collection) q.execute(obj);
Assert.assertTrue("No of containers with the specified element is incorrect (" + c.size() + ") : should have been 1", c.size() == 1);
}
tx.commit();
} catch (JDOUserException e) {
e.printStackTrace();
Assert.assertTrue("Exception thrown while querying container objects " + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class SCOMapTests method checkQueryNonPrimitiveKey.
/**
* Utility for checking the use of queries with JDOQL on a Map with
* primitive key/value objects. This is separate from the checkQuery
* because with primitive (String) value a different table is created.
* Uses containsKey(),containsValue(),containsEntry() methods.
* @param pmf The PersistenceManager factory
* @param container_class The container class e.g HashMapNormal
* @param key_class The key class
* @param value_class The value class
*/
public static void checkQueryNonPrimitiveKey(PersistenceManagerFactory pmf, Class container_class, Class key_class, Class value_class) throws Exception {
int NO_OF_ITEMS = 5;
Object key_id = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Create a container
MapHolder container = null;
try {
container = (MapHolder) container_class.newInstance();
} catch (Exception e1) {
LOG.error(e1);
Assert.fail("Failed to find Container class " + container_class.getName());
}
// Create a few items and add them to a Map
java.util.Map m = new java.util.HashMap();
Object saved_key = null;
for (int i = 0; i < NO_OF_ITEMS; i++) {
// Create an item
Object item = SCOHolderUtilities.createItemParent(value_class, "Item " + i, 0.00 + (10.00 * i), i);
Object k;
if (key_class.getName().equals(String.class.getName())) {
k = new String("Key" + i);
} else {
k = SCOHolderUtilities.createItemParent(key_class, "Key " + i, 0.00 + (11.13 * i), i);
}
m.put(k, item);
// Save the first key in the map for future use
if (i == 0) {
saved_key = k;
}
}
for (int i = 0; i < NO_OF_ITEMS; i++) {
// Create an item
Object item = SCOHolderUtilities.createItemParent(value_class, "Item " + i, 0.00 + (10.00 * i), i);
m.put(item, item);
}
// Add the items to the container
SCOHolderUtilities.addItemsToMap(container, m);
pm.makePersistent(container);
JDOHelper.getObjectId(container);
key_id = pm.getObjectId(saved_key);
tx.commit();
} catch (JDOUserException e) {
LOG.error(e);
Assert.assertTrue("Exception thrown while creating " + container_class.getName() + " " + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Query the Map
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Object saved_key = pm.getObjectById(key_id, false);
// Get all Maps that contain the key "Key1" - should return 1
Extent e1 = pm.getExtent(container_class, true);
Query q1 = pm.newQuery(e1, "items.containsKey(theKey)");
q1.declareParameters(key_class.getName() + " theKey");
java.util.Collection c1 = (java.util.Collection) q1.execute(saved_key);
Assert.assertTrue("containsKey : No of containers with a key with id \"" + key_id + "\" is incorrect (" + c1.size() + ") : should have been 1", c1.size() == 1);
tx.commit();
} catch (JDOUserException e) {
LOG.error(e);
e.printStackTrace();
Assert.assertTrue("Exception thrown while querying container objects " + e.getMessage(), false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class RelationshipTest method testParentChildLinkRelation.
/**
* Test case for 1-N inverse unidirectional to itself.
*/
public void testParentChildLinkRelation() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object primaryObjId = null;
// Check the persistence of owner and elements
try {
tx.begin();
ParentChildLink secondaryObj = new ParentChildLink("secondaryObj", null);
ParentChildLink primaryObj = new ParentChildLink("primaryObj", secondaryObj);
ParentChildLink childA = new ParentChildLink("childA", null);
ParentChildLink childB = new ParentChildLink("childB", null);
pm.makePersistent(primaryObj);
pm.makePersistent(secondaryObj);
pm.makePersistent(childA);
pm.makePersistent(childB);
primaryObj.addChild(childA);
primaryObj.addChild(childB);
tx.commit();
primaryObjId = pm.getObjectId(primaryObj);
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown while creating 1-N unidirectional 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();
tx = pm.currentTransaction();
try {
tx.begin();
ParentChildLink toSelf = (ParentChildLink) pm.getObjectById(primaryObjId, true);
assertNotNull("Unable to retrieve container object for unidirectional 1-N FK relationship", toSelf);
assertEquals("Number of elements in unidirectional 1-N FK relationship is wrong : was " + toSelf.getChildren().size() + " but should have been 2", toSelf.getChildren().size(), 2);
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Exception thrown while querying 1-N unidirectional relationships : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Extent ex = pm.getExtent(ParentChildLink.class);
Iterator iter = ex.iterator();
while (iter.hasNext()) {
ParentChildLink link = (ParentChildLink) iter.next();
link.getChildren().clear();
link.clearNextObject();
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
clean(ParentChildLink.class);
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class RelationshipTest method test1toNBidir_1to1Unidir.
/**
* Test case for 1-N bidir relationships + 1-1 unidir relationships
*/
public void test1toNBidir_1to1Unidir() throws Exception {
// This test currently relies on identity fields
if (!storeMgr.supportsValueGenerationStrategy("identity")) {
// Lets just say it passed :-)
return;
}
try {
// prepare data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object idEUR = null;
Object idUSD = null;
Object idCHF = null;
try {
tx.begin();
List cs = new ArrayList();
Currency chf = new Currency("CHF");
Currency eur = new Currency("EUR");
Currency usd = new Currency("USD");
cs.add(chf);
cs.add(eur);
cs.add(usd);
List c = new ArrayList();
c.add(new Rate(usd, eur, 0.9));
c.add(new Rate(usd, chf, 1.3));
usd.setRates(c);
c = new ArrayList();
c.add(new Rate(eur, chf, 1.5));
c.add(new Rate(eur, usd, 1.1));
eur.setRates(c);
c = new ArrayList();
c.add(new Rate(chf, eur, 0.7));
c.add(new Rate(chf, usd, 0.8));
chf.setRates(c);
pm.makePersistentAll(cs);
tx.commit();
idEUR = pm.getObjectId(eur);
idUSD = pm.getObjectId(usd);
idCHF = pm.getObjectId(chf);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while creating 1-N bi-directional relationships + 1-1 unidirecional relationships data : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Currency chf = (Currency) pm.getObjectById(idCHF, true);
Currency eur = (Currency) pm.getObjectById(idEUR, true);
Currency usd = (Currency) pm.getObjectById(idUSD, true);
assertEquals("CHF", "CHF", chf.getCurrencyCode());
assertEquals("CHF", 2, chf.getRates().size());
assertEquals("CHF", "EUR", ((Rate) chf.getRates().get(0)).getTarget().getCurrencyCode());
assertEquals("CHF", 0.7, ((Rate) chf.getRates().get(0)).getRate(), 0);
assertEquals("CHF", "USD", ((Rate) chf.getRates().get(1)).getTarget().getCurrencyCode());
assertEquals("CHF", 0.8, ((Rate) chf.getRates().get(1)).getRate(), 0);
assertEquals("EUR", "EUR", eur.getCurrencyCode());
assertEquals("EUR", 2, eur.getRates().size());
assertEquals("EUR", "CHF", ((Rate) eur.getRates().get(0)).getTarget().getCurrencyCode());
assertEquals("EUR", 1.5, ((Rate) eur.getRates().get(0)).getRate(), 0);
assertEquals("EUR", "USD", ((Rate) eur.getRates().get(1)).getTarget().getCurrencyCode());
assertEquals("EUR", 1.1, ((Rate) eur.getRates().get(1)).getRate(), 0);
assertEquals("USD", "USD", usd.getCurrencyCode());
assertEquals("USD", 2, usd.getRates().size());
assertEquals("USD", "EUR", ((Rate) usd.getRates().get(0)).getTarget().getCurrencyCode());
assertEquals("USD", 0.9, ((Rate) usd.getRates().get(0)).getRate(), 0);
assertEquals("USD", "CHF", ((Rate) usd.getRates().get(1)).getTarget().getCurrencyCode());
assertEquals("USD", 1.3, ((Rate) usd.getRates().get(1)).getRate(), 0);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while creating 1-N bi-directional relationships + 1-1 unidirecional relationships data : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out any created data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Extent ex = pm.getExtent(Rate.class);
Iterator iter = ex.iterator();
while (iter.hasNext()) {
Rate rate = (Rate) iter.next();
if (rate.getSource() != null) {
Currency source = rate.getSource();
rate.setSource(null);
source.setRates(null);
}
if (rate.getTarget() != null) {
rate.setTarget(null);
}
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
clean(Rate.class);
clean(Currency.class);
}
}
Aggregations