use of javax.jdo.Extent in project tests by datanucleus.
the class RelationshipTest method test1toNAbstractBaseUsingSubclassTable.
/**
* Test case for 1-N relations with base container/element using subclass-table and single subclass.
*/
public void test1toNAbstractBaseUsingSubclassTable() throws Exception {
try {
// prepare data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
try {
tx.begin();
Sky s = new Sky();
Cloud cl1 = new Cloud();
cl1.setId(101);
Cloud cl2 = new Cloud();
cl2.setId(102);
s.addCloud(cl1);
s.addCloud(cl2);
cl1.setSky(s);
cl2.setSky(s);
pm.makePersistent(s);
tx.commit();
id = JDOHelper.getObjectId(s);
} catch (Exception e) {
LOG.error("Exception in test", e);
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// test
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Sky s = (Sky) pm.getObjectById(id);
List<Cloud> clouds = s.getClouds();
assertEquals("Number of clouds in sky was wrong", 2, clouds.size());
Cloud cl1 = clouds.get(0);
assertEquals("First cloud is wrong", 101, cl1.getId().longValue());
Cloud cl2 = clouds.get(1);
assertEquals("Second cloud is wrong", 102, cl2.getId().longValue());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// getExtent of Sky
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent ex = pm.getExtent(Sky.class);
Iterator iter = ex.iterator();
int size = 0;
while (iter.hasNext()) {
iter.next();
size++;
}
assertEquals("Number of sky objects is incorrect from Extent", 1, size);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// getExtent of Cloud
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent ex = pm.getExtent(Cloud.class);
Iterator iter = ex.iterator();
int size = 0;
while (iter.hasNext()) {
iter.next();
size++;
}
assertEquals("Number of clouds is incorrect from Extent", 2, size);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out all data
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.begin();
for (Iterator iter = ((Collection) pm.newQuery(Sky.class).execute()).iterator(); iter.hasNext(); ) {
Sky sky = (Sky) iter.next();
Iterator<Cloud> clIter = sky.getClouds().iterator();
while (clIter.hasNext()) {
Cloud cl = clIter.next();
cl.setSky(null);
sky.removeCloud(cl);
}
}
tx.commit();
pm.close();
clean(Sky.class);
clean(Cloud.class);
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class AbstractClassesTest method perform1to1Retrieval.
/**
* Test of the retrieval of the abstract contained objects and of the
* holder with its related abstract object.
*/
private void perform1to1Retrieval(Class holderClass, Class abstractBaseClass, Class concreteClass1, Class concreteClass2) throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
// Retrieve the abstract objects including (concrete) subclasses
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Extent e = pm.getExtent(abstractBaseClass, true);
Iterator iter = e.iterator();
while (iter.hasNext()) {
iter.next();
}
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during retrieval of abstract objects.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
// Retrieve the abstract objects excluding (concrete) subclasses
tx = pm.currentTransaction();
try {
tx.begin();
Extent e = pm.getExtent(abstractBaseClass, false);
Iterator iter = e.iterator();
assertTrue("Retrieval of Extent for abstract class without subclasses did not return 0!", !iter.hasNext());
while (iter.hasNext()) {
iter.next();
}
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during retrieval of abstract objects.", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
// Retrieve the holders
tx = pm.currentTransaction();
try {
tx.begin();
Extent e = pm.getExtent(holderClass, false);
Iterator iter = e.iterator();
while (iter.hasNext()) {
Object holder = iter.next();
Method getNameMethod = holderClass.getMethod("getAbstract1", new Class[] {});
getNameMethod.invoke(holder, new Object[] {});
}
tx.commit();
} catch (JDOUserException ue) {
assertTrue("Exception thrown during retrieval of holder of abstract objects", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class AbstractClassesTest method perform1toNJoinTableListRetrieval.
/**
* Test of the retrieval of the abstract contained objects and of the
* holder with its related abstract object.
*/
private void perform1toNJoinTableListRetrieval(Class holderClass, Class element1Class, Class element2Class) throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Find the holder
Extent e = pm.getExtent(holderClass, true);
int numberOfHolders = 0;
Iterator iter = e.iterator();
while (iter.hasNext()) {
Object holder = iter.next();
numberOfHolders++;
// Extract the Set of elements
Method getSetMethod = holderClass.getMethod("getAbstractList1", new Class[] {});
Object obj = getSetMethod.invoke(holder, new Object[] {});
assertTrue("Elements for holder is NULL, but shouldn't be", obj != null);
assertTrue("Elements type is not a List", obj instanceof List);
List elements = (List) obj;
assertEquals("Number of elements is incorrect", 2, elements.size());
Object elem1 = elements.get(0);
Object elem2 = elements.get(1);
assertEquals("First element is of incorrect type", element1Class.getName(), elem1.getClass().getName());
assertEquals("First element is of incorrect type", element2Class.getName(), elem2.getClass().getName());
}
assertEquals("Number of container objects was incorrect.", 1, numberOfHolders);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown retrieving objects with abstract superclass", e);
e.printStackTrace();
fail("Exception thrown during persistence : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class AbstractClassesTest method perform1to1Deletion.
/**
* Method to perform a test for deletion of abstract objects
* @param holderClass The holder
* @param abstractBaseClass The abstract base class
* @param concreteClass1 Concrete class 1
* @param concreteClass2 Concrete class 2
* @throws Exception Thrown if an error occurs
*/
private void perform1to1Deletion(Class holderClass, Class abstractBaseClass, Class concreteClass1, Class concreteClass2) throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// Retrieve the ids of the abstract objects
Object abstract_id_1 = null;
Object abstract_id_2 = null;
tx.begin();
Extent e1 = pm.getExtent(abstractBaseClass, true);
Iterator iter1 = e1.iterator();
while (iter1.hasNext()) {
Object base = iter1.next();
if (abstract_id_1 == null) {
abstract_id_1 = pm.getObjectId(base);
} else {
abstract_id_2 = pm.getObjectId(base);
}
}
tx.commit();
// Retrieve the ids of the holder objects
Object holder_id_1 = null;
Object holder_id_2 = null;
tx.begin();
Extent e2 = pm.getExtent(holderClass, false);
Iterator iter2 = e2.iterator();
while (iter2.hasNext()) {
Object holder = iter2.next();
if (holder_id_1 == null) {
holder_id_1 = pm.getObjectId(holder);
} else {
holder_id_2 = pm.getObjectId(holder);
}
}
tx.commit();
// Delete the objects
tx.begin();
Object holder_1 = pm.getObjectById(holder_id_1, false);
pm.deletePersistent(holder_1);
Object holder_2 = pm.getObjectById(holder_id_2, false);
pm.deletePersistent(holder_2);
Object abstract_1 = pm.getObjectById(abstract_id_1, false);
pm.deletePersistent(abstract_1);
Object abstract_2 = pm.getObjectById(abstract_id_2, false);
pm.deletePersistent(abstract_2);
tx.commit();
} catch (JDOUserException ue) {
ue.printStackTrace();
LOG.error(ue);
assertTrue("Exception thrown during retrieval and deletion of holder and abstract objects", false);
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class CompanyHelper method clearCompanyData.
/**
* Convenience method to clean out all Company data
* @param pmf The PMF managing the company data
*/
public static void clearCompanyData(PersistenceManagerFactory pmf) {
if (pmf == null) {
throw new RuntimeException("CompanyHelper.clearCompanyData : PMF was null");
}
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// disassociate the Person objects from their best friends
tx.begin();
Extent ext = pm.getExtent(Person.class, true);
Iterator it = ext.iterator();
while (it.hasNext()) {
Person p = (Person) it.next();
p.setBestFriend(null);
}
tx.commit();
// disassociate all Employees and Departments from their Managers
tx.begin();
ext = pm.getExtent(Manager.class, false);
it = ext.iterator();
while (it.hasNext()) {
Manager mgr = (Manager) it.next();
mgr.getSubordinates().clear();
mgr.getDepartments().clear();
Iterator<Department> it2 = mgr.getDepartments().iterator();
while (it2.hasNext()) {
Department dept = it2.next();
dept.getProjects().clear();
}
}
tx.commit();
tx.begin();
ext = pm.getExtent(Employee.class, false);
it = ext.iterator();
while (it.hasNext()) {
Employee emp = (Employee) it.next();
emp.setManager(null);
// Clear link to best friend
emp.setBestFriend(null);
}
tx.commit();
// Disassociate the Qualification and Person objects
tx.begin();
ext = pm.getExtent(Qualification.class, false);
it = ext.iterator();
while (it.hasNext()) {
Qualification q = (Qualification) it.next();
q.setPerson(null);
}
tx.commit();
// disassociate the Manager objects
tx.begin();
ext = pm.getExtent(Manager.class, false);
it = ext.iterator();
while (it.hasNext()) {
Manager mgr = (Manager) it.next();
mgr.setManager(null);
}
tx.commit();
// disassociate the Departments from the Offices
tx.begin();
ext = pm.getExtent(Office.class, false);
it = ext.iterator();
while (it.hasNext()) {
Office off = (Office) it.next();
off.clearDepartments();
}
tx.commit();
JDOPersistenceTestCase.cleanClassForPMF(pmf, Manager.class);
JDOPersistenceTestCase.cleanClassForPMF(pmf, Employee.class);
JDOPersistenceTestCase.cleanClassForPMF(pmf, Qualification.class);
JDOPersistenceTestCase.cleanClassForPMF(pmf, Department.class);
JDOPersistenceTestCase.cleanClassForPMF(pmf, Person.class);
JDOPersistenceTestCase.cleanClassForPMF(pmf, Office.class);
JDOPersistenceTestCase.cleanClassForPMF(pmf, Account.class);
JDOPersistenceTestCase.cleanClassForPMF(pmf, Project.class);
} catch (Exception e) {
NucleusLogger.GENERAL.error("Exception thrown clearing out Company data", e);
return;
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations