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 DependentFieldTest method clearDependentData.
/**
* Convenience method to clean out dependent data
* @param pmf The pmf to use
*/
private void clearDependentData(PersistenceManagerFactory pmf) {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// disassociate all dependent fields
tx.begin();
Extent ext = pm.getExtent(DependentHolder.class, false);
Iterator it = ext.iterator();
while (it.hasNext()) {
DependentHolder holder = (DependentHolder) it.next();
DependentElement element = holder.getElement();
if (element != null) {
element.setOwner(null);
element.setKey(null);
pm.flush();
}
holder.setElement(null);
holder.setListDependent1(null);
holder.setListNonDependent1(null);
holder.setSetDependent1(null);
holder.setSetNonDependent1(null);
holder.setMapDependent1(null);
holder.setMapNonDependent1(null);
holder.setMapDependentKeys1(null);
holder.setMapDependentValues1(null);
holder.setMapNonDependentKeys1(null);
holder.setMapNonDependentValues1(null);
pm.flush();
}
ext = pm.getExtent(DependentHolder.class, false);
it = ext.iterator();
while (it.hasNext()) {
DependentHolder holder = (DependentHolder) it.next();
DependentElement element = holder.getElement();
if (element != null) {
element.setOwner(null);
element.setKey(null);
pm.flush();
}
holder.setElement(null);
holder.setListDependent2(null);
holder.setListNonDependent2(null);
holder.setSetDependent2(null);
holder.setSetNonDependent2(null);
holder.setMapDependent2(null);
holder.setMapNonDependent2(null);
holder.setMapDependentKeys2(null);
holder.setMapDependentValues2(null);
holder.setMapNonDependentKeys2(null);
holder.setMapNonDependentValues2(null);
pm.flush();
}
ext = pm.getExtent(DependentElement1.class, false);
it = ext.iterator();
while (it.hasNext()) {
DependentElement1 field = (DependentElement1) it.next();
field.setKey(null);
field.setOwner(null);
pm.flush();
}
ext = pm.getExtent(DependentElement2.class, false);
it = ext.iterator();
while (it.hasNext()) {
DependentElement2 field = (DependentElement2) it.next();
field.setKey(null);
field.setOwner(null);
pm.flush();
}
ext = pm.getExtent(DependentElement3.class, false);
it = ext.iterator();
while (it.hasNext()) {
DependentElement3 field = (DependentElement3) it.next();
field.setKey(null);
field.setOwner(null);
pm.flush();
}
ext = pm.getExtent(DependentElement4.class, false);
it = ext.iterator();
while (it.hasNext()) {
DependentElement4 field = (DependentElement4) it.next();
field.setKey(null);
field.setOwner(null);
pm.flush();
}
ext = pm.getExtent(DependentElement5.class, false);
it = ext.iterator();
while (it.hasNext()) {
DependentElement5 field = (DependentElement5) it.next();
field.setKey(null);
field.setOwner(null);
pm.flush();
}
ext = pm.getExtent(DependentElement6.class, false);
it = ext.iterator();
while (it.hasNext()) {
DependentElement6 field = (DependentElement6) it.next();
field.setKey(null);
field.setOwner(null);
pm.flush();
}
ext = pm.getExtent(DependentElement7.class, false);
it = ext.iterator();
while (it.hasNext()) {
DependentElement7 field = (DependentElement7) it.next();
field.setKey(null);
field.setOwner(null);
pm.flush();
}
ext = pm.getExtent(DependentElement8.class, false);
it = ext.iterator();
while (it.hasNext()) {
DependentElement8 field = (DependentElement8) it.next();
field.setKey(null);
field.setOwner(null);
pm.flush();
}
ext = pm.getExtent(DependentElement9.class, false);
it = ext.iterator();
while (it.hasNext()) {
DependentElement9 field = (DependentElement9) it.next();
field.setKey(null);
field.setOwner(null);
pm.flush();
}
ext = pm.getExtent(DependentElement10.class, false);
it = ext.iterator();
while (it.hasNext()) {
DependentElement10 field = (DependentElement10) it.next();
field.setKey(null);
field.setOwner(null);
pm.flush();
}
tx.commit();
clean(pmf, DependentHolder.class);
clean(pmf, DependentElement1.class);
clean(pmf, DependentElement2.class);
clean(pmf, DependentElement3.class);
clean(pmf, DependentElement4.class);
clean(pmf, DependentElement5.class);
clean(pmf, DependentElement6.class);
clean(pmf, DependentElement7.class);
clean(pmf, DependentElement8.class);
clean(pmf, DependentElement9.class);
clean(pmf, DependentElement10.class);
clean(pmf, DependentHolder.class);
clean(pmf, DepInterfaceImpl1.class);
clean(pmf, DepInterfaceImpl2.class);
} 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(PersistenceManager pm) {
Transaction tx = pm.currentTransaction();
try {
// disassociate all Employees and Departments from their Managers
tx.begin();
Extent ext = pm.getExtent(Manager.class, false);
Iterator it = ext.iterator();
while (it.hasNext()) {
Manager mgr = (Manager) it.next();
Set<Employee> subordinates = mgr.getSubordinates();
if (subordinates != null && subordinates.size() > 0) {
Iterator<Employee> empIter = subordinates.iterator();
while (empIter.hasNext()) {
Employee emp = empIter.next();
emp.setManager(null);
}
mgr.clearSubordinates();
}
Iterator deptIter = mgr.getDepartments().iterator();
while (deptIter.hasNext()) {
Department dept = (Department) deptIter.next();
dept.setManager(null);
dept.getProjects().clear();
}
mgr.clearDepartments();
}
tx.commit();
tx.begin();
ext = pm.getExtent(Employee.class, true);
it = ext.iterator();
while (it.hasNext()) {
Employee emp = (Employee) it.next();
emp.setAccount(null);
emp.setManager(null);
}
tx.commit();
tx.begin();
ext = pm.getExtent(Person.class, true);
it = ext.iterator();
while (it.hasNext()) {
Person pers = (Person) it.next();
// Clear link to best friend
pers.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.cleanClassForPM(pm, Manager.class);
JDOPersistenceTestCase.cleanClassForPM(pm, Employee.class);
JDOPersistenceTestCase.cleanClassForPM(pm, Qualification.class);
JDOPersistenceTestCase.cleanClassForPM(pm, Department.class);
JDOPersistenceTestCase.cleanClassForPM(pm, Person.class);
JDOPersistenceTestCase.cleanClassForPM(pm, Office.class);
JDOPersistenceTestCase.cleanClassForPM(pm, Account.class);
JDOPersistenceTestCase.cleanClassForPM(pm, Project.class);
} catch (Exception e) {
LOG.error("Exception thrown during clear", e);
throw new NucleusUserException("Exception thrown during clear() : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class AttachDetachReplicateTest method testMoveAcrossDatastoresWithRelation.
/**
* Test of detaching from one datastore and persisting the objects to another.
* As we copy a group of related objects JPOX should serialize SQLs in the
* way that do not break foreign key constraints.
*/
public void testMoveAcrossDatastoresWithRelation() {
PersistenceManagerFactory pmf2 = getPersistenceManagerFactory2();
try {
PersistenceManager pm1 = pmf.getPersistenceManager();
Detail detail = null;
// Persist in first DB
Transaction tx = null;
try {
tx = pm1.currentTransaction();
tx.begin();
// Create some dummy records, so we offset the identity values and make a valid test
pm1.makePersistent(new Master());
pm1.makePersistent(new Master());
pm1.makePersistent(new Master());
pm1.makePersistent(new Detail());
pm1.makePersistent(new Detail());
pm1.makePersistent(new Detail());
pm1.makePersistent(new OtherDetail());
pm1.makePersistent(new OtherDetail());
pm1.makePersistent(new OtherDetail());
// Create our test objects, Master with related Detail and OtherDetail
Master master1 = new Master();
master1.setId("master");
Detail detail1 = new Detail();
detail1.setId("detail");
master1.addDetail(detail1);
detail1.setMaster(master1);
OtherDetail otherDetail1 = new OtherDetail();
otherDetail1.setId("otherDetail1");
master1.addOtherDetail(otherDetail1);
otherDetail1.setMaster(master1);
pm1.makePersistent(detail1);
// Detach it for copying
pm1.getFetchPlan().addGroup("all");
pm1.getFetchPlan().setMaxFetchDepth(2);
detail = (Detail) pm1.detachCopy(detail1);
tx.commit();
} catch (JDOUserException ue) {
LOG.error(ue);
fail("Exception thrown while creating object in first datastore : " + ue.getMessage());
} finally {
if (tx != null && tx.isActive()) {
tx.rollback();
}
pm1.close();
}
// Check our detached objects
assertNotNull(detail.getMaster());
assertEquals(detail.getMaster().getOtherDetails().size(), 1);
// Copy to other DB
PersistenceManager pm2 = pmf2.getPersistenceManager();
try {
tx = pm2.currentTransaction();
tx.begin();
// Persist graph of three transient objects to see if this datastore works
Master master2 = new Master();
master2.setId("master2");
Detail detail2 = new Detail();
detail2.setId("detail2");
master2.addDetail(detail2);
detail2.setMaster(master2);
OtherDetail otherDetail2 = new OtherDetail();
otherDetail2.setId("otherDetail2");
master2.addOtherDetail(otherDetail2);
otherDetail2.setMaster(master2);
pm2.makePersistent(detail2);
// Replicate object graph of three detached objects from datastore 1
pm2.makePersistent(detail);
tx.commit();
} catch (JDOUserException ue) {
LOG.error(ue);
fail("Exception thrown while copying object into second datastore : " + ue.getMessage());
} finally {
if (tx != null && tx.isActive()) {
tx.rollback();
}
}
// Check the persistence in the second datastore
try {
tx = pm2.currentTransaction();
tx.begin();
// Use Extent since PM may have just put object in cache.
Extent e = pm2.getExtent(Master.class);
Iterator iter = e.iterator();
int noOfMasters = 0;
while (iter.hasNext()) {
noOfMasters++;
iter.next();
}
assertTrue("Number of masters retrieved from second datastore is incorrect : was " + noOfMasters + " but should have been 2", noOfMasters == 2);
e = pm2.getExtent(OtherDetail.class);
iter = e.iterator();
int noOfOtherDetails = 0;
while (iter.hasNext()) {
noOfOtherDetails++;
iter.next();
}
assertTrue("Number of otherdetails retrieved from second datastore is incorrect : was " + noOfOtherDetails + " but should have been 2", noOfOtherDetails == 2);
tx.commit();
} catch (JDOUserException ue) {
LOG.error(ue);
fail("Exception thrown while querying object in second datastore : " + ue.getMessage());
} finally {
if (tx != null && tx.isActive()) {
tx.rollback();
}
pm2.close();
}
} finally {
// Clean up our data in the main DB
clean(Detail.class);
clean(OtherDetail.class);
clean(Master.class);
// Clean up data in the other DB
PersistenceManager pm = pmf2.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// delete all Detail objects
tx.begin();
Extent ext = pm.getExtent(Detail.class, true);
Iterator it = ext.iterator();
while (it.hasNext()) {
Object o = it.next();
pm.deletePersistent(o);
}
tx.commit();
// delete all OtherDetail objects
tx.begin();
ext = pm.getExtent(OtherDetail.class, true);
it = ext.iterator();
while (it.hasNext()) {
Object o = it.next();
pm.deletePersistent(o);
}
tx.commit();
// delete all Master objects
tx.begin();
ext = pm.getExtent(Master.class, true);
it = ext.iterator();
while (it.hasNext()) {
Object o = it.next();
pm.deletePersistent(o);
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class SchemaTest method testReadOnlyDatastore.
/**
* Test of the Read-Only datastore facility.
* Should prevent all attempts to write to the datastore.
*/
public void testReadOnlyDatastore() {
try {
// Create the necessary table and create a few objects
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Make sure our read-write PMF has schema for this class
pm.getExtent(Developer.class);
// Make sure our read-write PMF has schema for this class
pm.getExtent(Manager.class);
Employee e = new Employee(123, "Barney", "Rubble", "barney.rubble@warnerbros.com", (float) 123.45, "1245C");
pm.makePersistent(e);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Create a PMF for our read-only schema
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_DATASTORE_READONLY, "true");
PersistenceManagerFactory pmf2 = getPMF(1, userProps);
assertTrue("The PMF should have had the ReadOnlyDatastore property, yet hasn't", getConfigurationForPMF(pmf2).getBooleanProperty(PropertyNames.PROPERTY_DATASTORE_READONLY));
PersistenceManager pm2 = pmf2.getPersistenceManager();
// a). Try makePersistent
Transaction tx2 = pm2.currentTransaction();
try {
tx2.begin();
Employee e = new Employee(123, "Barney", "Rubble", "barney.rubble@warnerbros.com", (float) 123.45, "1245C");
pm2.makePersistent(e);
tx2.commit();
assertTrue("Should have thrown an exception when trying makePersistent on ReadOnly datastore", false);
} catch (Exception e) {
LOG.error(e);
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
}
// b). Try deletePersistent
tx2 = pm2.currentTransaction();
try {
tx2.begin();
Extent ex = pm2.getExtent(Employee.class, true);
Iterator iter = ex.iterator();
while (iter.hasNext()) {
Employee e = (Employee) iter.next();
pm2.deletePersistent(e);
}
tx2.commit();
assertTrue("Should have thrown an exception when trying deletePersistent on ReadOnly datastore", false);
} catch (Exception e) {
LOG.error(e);
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
}
// c). Try update
tx2 = pm2.currentTransaction();
try {
tx2.begin();
Extent ex = pm2.getExtent(Employee.class, true);
Iterator iter = ex.iterator();
while (iter.hasNext()) {
Employee e = (Employee) iter.next();
e.setAge(23);
}
tx2.commit();
assertTrue("Should have thrown an exception when modifying an object on ReadOnly datastore", false);
} catch (Exception e) {
LOG.error(e);
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
}
// d). Try query
tx2 = pm2.currentTransaction();
try {
tx2.begin();
Query q = pm2.newQuery(Employee.class);
Collection results = (Collection) q.execute();
Iterator resultsIter = results.iterator();
while (resultsIter.hasNext()) {
resultsIter.next();
}
tx2.commit();
} catch (Exception e) {
assertTrue("Should have been able to access objects on a ReadOnly datastore", false);
LOG.error(e);
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
}
pm2.close();
pmf2.close();
} finally {
clean(Employee.class);
}
}
Aggregations