use of org.jpox.samples.one_many.bidir.Animal in project tests by datanucleus.
the class RelationshipTest method test1toNBidirFK.
/**
* Test case for 1-N bidir FK relations.
*/
public void test1toNBidirFK() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
pm.setProperty(PropertyNames.PROPERTY_MANAGE_RELATIONSHIPS, "" + manageRelationships);
Transaction tx = pm.currentTransaction();
Object farmId = null;
// Check the persistence of owner and elements
try {
tx.begin();
// Create some data
Farm farm = new Farm("Giles Farm");
Animal animal1 = new Animal("Shep");
Animal animal2 = new Animal("Grunter");
farm.addAnimal(animal1);
farm.addAnimal(animal2);
animal1.setFarm(farm);
animal2.setFarm(farm);
pm.makePersistent(farm);
tx.commit();
farmId = pm.getObjectId(farm);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while creating 1-N bidir FK relationships : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
// Check the retrieval of owner and elements
tx = pm.currentTransaction();
try {
tx.begin();
Farm farm = (Farm) pm.getObjectById(farmId);
assertNotNull("Unable to retrieve container object for 1-N bidir FK relationship", farm);
Collection animals = farm.getAnimals();
assertNotNull("Holder of elements with 1-N bidir FK relation is null!", farm);
assertEquals("Number of elements in 1-N bidir FK relationship is wrong", 2, animals.size());
Iterator iter = animals.iterator();
while (iter.hasNext()) {
iter.next();
}
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown while querying 1-N bidir FK relationships : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
pm.close();
} finally {
// Clean out our data
clean(Farm.class);
clean(Animal.class);
}
}
use of org.jpox.samples.one_many.bidir.Animal in project tests by datanucleus.
the class AttachDetachTest method testDetachAttach_OneToManyBidir.
/**
* Test for a 1-N bidirectional relationship which, by its nature,
* provides a recursive detach test.
*/
public void testDetachAttach_OneToManyBidir() {
try {
PersistenceManager pm = newPM();
Transaction tx = pm.currentTransaction();
tx.setNontransactionalRead(true);
Object id = null;
try {
// Persist some objects
tx.begin();
Farm farm = new Farm("North End Farm");
Animal duck = new Animal("Donald");
Animal cow = new Animal("Gertrude");
Animal horse = new Animal("Shergar");
farm.addAnimal(duck);
farm.addAnimal(cow);
farm.addAnimal(horse);
pm.makePersistent(farm);
tx.commit();
id = pm.getObjectId(farm);
} catch (Exception e) {
fail("Exception thrown while persisting 1-N bidirectional objects for the recursive attach/detach test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = newPM();
tx = pm.currentTransaction();
Farm detachedFarm = null;
try {
tx.begin();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
pm.getFetchPlan().setMaxFetchDepth(2);
Farm farm = (Farm) pm.getObjectById(id, false);
assertTrue("Error retrieving the Farm object that was just persisted", farm != null);
LOG.info("Retrieved Farm \"" + farm.toString() + "\"");
detachedFarm = (Farm) pm.detachCopy(farm);
tx.commit();
} catch (Exception e) {
fail("Exception thrown while detaching 1-N bidrectional objects in the recursive attach/detach test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Play with the farm
try {
LOG.info("Detached farm : " + detachedFarm);
Set<Animal> animals = detachedFarm.getAnimals();
Iterator<Animal> animalsIter = animals.iterator();
while (animalsIter.hasNext()) {
LOG.info("Detached animal : " + animalsIter.next());
}
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception thrown on use of allegedly detached farm " + e.getMessage());
}
} finally {
clean(Farm.class);
clean(Animal.class);
}
}
use of org.jpox.samples.one_many.bidir.Animal in project tests by datanucleus.
the class JDOQLContainerTest method testNotContainsFieldOfCandidate.
/**
* Tests !contains() of the value of a field in the candidate.
*/
public void testNotContainsFieldOfCandidate() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create some data
tx.begin();
Farm farm1 = new Farm("Jones Farm");
Animal animal1 = new Animal("Dog");
Animal animal2 = new Animal("Cow 1");
Animal animal3 = new Animal("Cow 2");
farm1.getAnimals().add(animal2);
farm1.getAnimals().add(animal3);
farm1.getAnimals().add(animal1);
animal1.setFarm(farm1);
animal2.setFarm(farm1);
animal3.setFarm(farm1);
farm1.setPet(animal1);
Farm farm2 = new Farm("Smith Farm");
Animal animal4 = new Animal("Pig 1");
Animal animal5 = new Animal("Pig 2");
farm2.getAnimals().add(animal4);
farm2.getAnimals().add(animal5);
animal4.setFarm(farm2);
animal5.setFarm(farm2);
Animal animal6 = new Animal("Cat");
farm2.setPet(animal6);
pm.makePersistent(farm1);
pm.makePersistent(farm2);
pm.flush();
// Query the data
Query q = pm.newQuery("SELECT FROM " + Farm.class.getName() + " WHERE !animals.contains(pet)");
List results = (List) q.execute();
assertEquals(1, results.size());
assertEquals("Smith Farm", ((Farm) results.get(0)).getName());
tx.rollback();
} catch (Exception e) {
LOG.error("Exception thrown during test", e);
fail("Exception thrown while performing test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Farm.class);
clean(Animal.class);
}
}
use of org.jpox.samples.one_many.bidir.Animal in project tests by datanucleus.
the class JDOQLContainerTest method testBulkFetchUsingFK.
/**
* Tests use of bulk-fetch on collection via FK.
*/
public void testBulkFetchUsingFK() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Create some data
tx.begin();
Farm farm1 = new Farm("Jones Farm");
Animal animal1 = new Animal("Dog");
Animal animal2 = new Animal("Cow 1");
Animal animal3 = new Animal("Cow 2");
farm1.getAnimals().add(animal2);
farm1.getAnimals().add(animal3);
farm1.getAnimals().add(animal1);
animal1.setFarm(farm1);
animal2.setFarm(farm1);
animal3.setFarm(farm1);
farm1.setPet(animal1);
Farm farm2 = new Farm("Smith Farm");
Animal animal4 = new Animal("Pig 1");
Animal animal5 = new Animal("Pig 2");
farm2.getAnimals().add(animal4);
farm2.getAnimals().add(animal5);
animal4.setFarm(farm2);
animal5.setFarm(farm2);
pm.makePersistent(farm1);
pm.makePersistent(farm2);
pm.flush();
// Query the data with bulk-fetch enabled
// Note : this doesn't actually check how many SQL were issued just that it passes
Query q = pm.newQuery("SELECT FROM " + Farm.class.getName() + " WHERE name == :farmName");
q.addExtension("datanucleus.rdbms.query.multivaluedFetch", "exists");
q.getFetchPlan().setGroup("all");
List results = (List) q.execute("Jones Farm");
assertEquals(1, results.size());
assertEquals("Jones Farm", ((Farm) results.get(0)).getName());
tx.rollback();
} catch (Exception e) {
LOG.error("Exception thrown during test", e);
fail("Exception thrown while performing test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Farm.class);
clean(Animal.class);
}
}
use of org.jpox.samples.one_many.bidir.Animal in project tests by datanucleus.
the class PersistenceManagerTest method testMakeTransientOwnerAndElements.
/**
* Test of makeTransient() for owner and makeTransientAll() for the owner elements.
*/
public void testMakeTransientOwnerAndElements() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// JoinTable 1-N relationship
// Create a Manager with subordinate number 0
createNewManager(pm, 0);
// Make the Manager transient with all subordinates
tx.begin();
Manager m = (Manager) pm.getExtent(Manager.class, true).iterator().next();
pm.retrieveAll(m.getSubordinates());
pm.retrieveAll(m.getDepartments());
pm.makeTransient(m);
pm.makeTransientAll(m.getSubordinates());
pm.makeTransientAll(m.getDepartments());
tx.commit();
// Check the result
assertNull(JDOHelper.getObjectId(m));
// Compare the managers
Manager m1 = queryManager(0, pm);
tx.begin();
Assert.assertEquals(m.getBestFriend(), m1.getBestFriend());
Assert.assertEquals(m.getLastName(), m1.getLastName());
Assert.assertEquals(m.getFirstName(), m1.getFirstName());
Assert.assertEquals(m.getEmailAddress(), m1.getEmailAddress());
Assert.assertEquals(m.getPersonNum(), m1.getPersonNum());
Assert.assertTrue("subordinates are not the same", Manager.compareSet(m.getSubordinates(), m1.getSubordinates()));
Assert.assertTrue("departments are not the same", Manager.compareSet(m.getDepartments(), m1.getDepartments()));
tx.commit();
// FK 1-N relationship
// Create owner with 2 elements.
tx = pm.currentTransaction();
tx.begin();
Farm farm = new Farm("Giles Farm");
Animal an1 = new Animal("Dog");
an1.setFarm(farm);
Animal an2 = new Animal("Cow");
an2.setFarm(farm);
farm.addAnimal(an1);
farm.addAnimal(an2);
pm.makePersistent(farm);
tx.commit();
Object id = pm.getObjectId(farm);
// Make the owner and its elements transient
tx = pm.currentTransaction();
tx.begin();
pm.getFetchPlan().addGroup(FetchPlan.ALL);
Farm transientFarm = (Farm) pm.getObjectById(id, true);
pm.retrieveAll(transientFarm.getAnimals());
pm.makeTransient(transientFarm);
pm.makeTransientAll(transientFarm.getAnimals());
tx.commit();
// Check the result
Set transientAnimals = transientFarm.getAnimals();
assertFalse("Owner object is not transient!", JDOHelper.isPersistent(transientFarm));
assertEquals("Number of elements in transient owner is incorrect", transientAnimals.size(), 2);
Iterator transientAnimalsIter = transientAnimals.iterator();
boolean dogPresent = false;
boolean cowPresent = false;
while (transientAnimalsIter.hasNext()) {
Animal transientAnimal = (Animal) transientAnimalsIter.next();
assertFalse("Element object is not transient!", JDOHelper.isPersistent(transientAnimal));
// Check that the Animal name field is the same as that persisted
if (transientAnimal.getName().equals("Dog")) {
dogPresent = true;
} else if (transientAnimal.getName().equals("Cow")) {
cowPresent = true;
}
// Check that the owner is the transient one
assertEquals("Owner of transient animal is incorrect", StringUtils.toJVMIDString(transientAnimal.getFarm()), StringUtils.toJVMIDString(transientFarm));
}
assertTrue("First value animal is not present in the transient set", dogPresent);
assertTrue("Second value animal is not present in the transient set", cowPresent);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Farm.class);
clean(Animal.class);
clean(Manager.class);
clean(Widget.class);
}
}
Aggregations