use of javax.jdo.Extent in project tests by datanucleus.
the class PersistenceManagerTest method testJavaIdentity.
/**
* Test that getObjectId() returns the same value when called on a newly persistent PC as when
* called on a query result that should return that same persistent object.
*/
public void testJavaIdentity() {
try {
Person p = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
tx.begin();
pm.makePersistent(p);
Object oid = pm.getObjectId(p);
tx.commit();
try {
tx.begin();
Extent ext = pm.getExtent(Person.class, false);
// should only have one Person object
Iterator it = ext.iterator();
assertTrue(it.hasNext());
p = (Person) it.next();
assertTrue(!it.hasNext());
assertEquals(oid, pm.getObjectId(p));
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
} finally {
clean(Person.class);
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class PersistenceManagerTest method testFKCollectionFieldPersistenceByReachability1.
/**
* Test that when a FK collection is persisted the element is persisted also.
* TODO Move to reachability tests.
*/
public void testFKCollectionFieldPersistenceByReachability1() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
try {
Department d = new Department("Engineering");
d.setManager(mgr);
mgr.addDepartment(d);
tx.begin();
pm.makePersistent(mgr);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown when persisting FK collection using reachability", e);
fail("Exception thrown when persisting FK collection using reachability " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// get a fresh PM to ensure that any results aren't coming from the cache
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent ext = pm.getExtent(Manager.class, false);
java.util.Iterator it = ext.iterator();
assertTrue(it.hasNext());
mgr = (Manager) it.next();
Collection c = mgr.getDepartments();
assertEquals(1, c.size());
ext = pm.getExtent(Department.class, false);
it = ext.iterator();
assertTrue(it.hasNext());
Department d = (Department) it.next();
assertTrue(c.contains(d));
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class PersistenceManagerTest method testNormalFCOCollectionFieldPersistence4.
/**
* Test adding and removing elements from Collections whose members are defined as a non-PC superclass or interface
*/
public void testNormalFCOCollectionFieldPersistence4() {
if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_SERIALISED_COLLECTION_ELEMENT)) {
return;
}
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
Primitive p = new Primitive();
setPrimitiveValues(p);
CollectionFieldTester tester = new CollectionFieldTester();
try {
tester.getObjectCollection().add(mgr);
tx.begin();
pm.makePersistent(tester);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent ex = pm.getExtent(CollectionFieldTester.class, true);
java.util.Iterator it = ex.iterator();
assertTrue(it.hasNext());
tester = (CollectionFieldTester) it.next();
assertEquals(1, tester.getObjectCollection().size());
mgr = (Manager) tester.getObjectCollection().iterator().next();
assertEquals(0, mgr.getPersonNum());
tx.commit();
} catch (Exception e) {
LOG.error(">> Exception thrown in test", e);
fail("Exception thrown in ObjectCollection test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
pm.close();
fail("Failed to persist object and commit transaction");
}
pm.close();
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class PersistenceManagerTest method testPersistenceOfOneToOneRelations.
/**
* Test that persisting a PC will also persist all PCs that are referenced.
*/
public void testPersistenceOfOneToOneRelations() {
try {
Manager manager = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
Employee employee = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object managerID = null;
try {
tx.begin();
pm.makePersistent(employee);
employee.setManager(manager);
tx.commit();
managerID = pm.getObjectId(manager);
} finally {
if (tx.isActive()) {
tx.rollback();
pm.close();
fail("Failed to persist object and commit transaction");
}
pm.close();
}
// Check the contents
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Extent clnManager = pm.getExtent(Manager.class, false);
// should only have one Primitive object
Iterator it = clnManager.iterator();
assertTrue(it.hasNext());
Manager m = (Manager) it.next();
assertEquals(managerID, pm.getObjectId(m));
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
CompanyHelper.clearCompanyData(pmf);
}
}
use of javax.jdo.Extent in project tests by datanucleus.
the class PersistenceManagerTest method tearDown.
/**
* Invoked after each test is run
*/
public void tearDown() throws java.lang.Exception {
super.tearDown();
// TODO Remove all of this when each test cleans out its own data.
Extent ext = null;
java.util.Iterator it = null;
PersistenceManager pm = pmf.getPersistenceManager();
try {
// delete all InstanceCallbackContainer objects
pm.currentTransaction().begin();
ext = pm.getExtent(InstanceCallbackContainer.class, false);
it = ext.iterator();
while (it.hasNext()) {
InstanceCallbackContainer owner = (InstanceCallbackContainer) it.next();
pm.deletePersistent(owner);
}
pm.currentTransaction().commit();
// delete all InstanceCallbackTester objects
pm.currentTransaction().begin();
ext = pm.getExtent(InstanceCallbackTester.class, false);
it = ext.iterator();
while (it.hasNext()) {
InstanceCallbackTester tester = (InstanceCallbackTester) it.next();
// necesaary to avoid exception from jdoPreDelete() for this class only
tester.setTransientValue("");
pm.deletePersistent(tester);
}
pm.currentTransaction().commit();
// delete all InversePrimitive objects
pm.currentTransaction().begin();
ext = pm.getExtent(org.datanucleus.samples.widget.InversePrimitive.class, false);
it = ext.iterator();
while (it.hasNext()) {
InversePrimitive ip = (InversePrimitive) it.next();
ip.setTester(null);
pm.deletePersistent(ip);
}
pm.currentTransaction().commit();
// delete all CollectionFieldTester objects
pm.currentTransaction().begin();
ext = pm.getExtent(CollectionFieldTester.class, false);
it = ext.iterator();
while (it.hasNext()) {
CollectionFieldTester t = (CollectionFieldTester) it.next();
if (t.getPrimitiveCollection() != null) {
t.getPrimitiveCollection().clear();
}
if (t.getInversePrimitiveCollection() != null) {
t.getInversePrimitiveCollection().clear();
}
pm.deletePersistent(t);
}
pm.currentTransaction().commit();
// delete all Primative objects
pm.currentTransaction().begin();
ext = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
it = ext.iterator();
while (it.hasNext()) {
Primitive p = (Primitive) it.next();
pm.deletePersistent(p);
}
pm.currentTransaction().commit();
// disassociate all Employees and Departments from their Managers
pm.currentTransaction().begin();
ext = pm.getExtent(Manager.class, false);
it = ext.iterator();
while (it.hasNext()) {
Manager mgr = (Manager) it.next();
if (mgr.getSubordinates() != null) {
mgr.getSubordinates().clear();
}
if (mgr.getDepartments() != null) {
mgr.getDepartments().clear();
}
}
pm.currentTransaction().commit();
// delete all Employee objects
pm.currentTransaction().begin();
ext = pm.getExtent(Employee.class, false);
it = ext.iterator();
while (it.hasNext()) {
Employee emp = (Employee) it.next();
pm.deletePersistent(emp);
}
pm.currentTransaction().commit();
pm.currentTransaction().begin();
// dekete all Department objects
ext = pm.getExtent(Department.class, false);
it = ext.iterator();
while (it.hasNext()) {
Department d = (Department) it.next();
pm.deletePersistent(d);
}
pm.currentTransaction().commit();
// delete all Manager objects
pm.currentTransaction().begin();
ext = pm.getExtent(Manager.class, false);
it = ext.iterator();
while (it.hasNext()) {
Manager mgr = (Manager) it.next();
pm.deletePersistent(mgr);
}
pm.currentTransaction().commit();
// delete all Person objects
pm.currentTransaction().begin();
ext = pm.getExtent(Person.class, true);
it = ext.iterator();
while (it.hasNext()) {
Person person = (Person) it.next();
pm.deletePersistent(person);
}
pm.currentTransaction().commit();
} finally {
if (pm.currentTransaction().isActive())
pm.currentTransaction().commit();
pm.close();
}
}
Aggregations