Search in sources :

Example 26 with Extent

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);
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) Person(org.jpox.samples.models.company.Person)

Example 27 with Extent

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();
    }
}
Also used : Department(org.jpox.samples.models.company.Department) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Collection(java.util.Collection) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) TransactionNotReadableException(org.datanucleus.api.jdo.exceptions.TransactionNotReadableException) SQLException(java.sql.SQLException) JDOUserCallbackException(javax.jdo.JDOUserCallbackException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) TransactionNotWritableException(org.datanucleus.api.jdo.exceptions.TransactionNotWritableException) JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException)

Example 28 with Extent

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();
    }
}
Also used : InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) CollectionFieldTester(org.datanucleus.samples.widget.CollectionFieldTester) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) TransactionNotReadableException(org.datanucleus.api.jdo.exceptions.TransactionNotReadableException) SQLException(java.sql.SQLException) JDOUserCallbackException(javax.jdo.JDOUserCallbackException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) TransactionNotWritableException(org.datanucleus.api.jdo.exceptions.TransactionNotWritableException) JDOUnsupportedOptionException(javax.jdo.JDOUnsupportedOptionException)

Example 29 with Extent

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);
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) Iterator(java.util.Iterator) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 30 with Extent

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();
    }
}
Also used : Iterator(java.util.Iterator) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Extent(javax.jdo.Extent) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) InstanceCallbackContainer(org.datanucleus.samples.instancecallback.InstanceCallbackContainer) Manager(org.jpox.samples.models.company.Manager) StoreManager(org.datanucleus.store.StoreManager) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) InstanceCallbackTester(org.datanucleus.samples.instancecallback.InstanceCallbackTester) CollectionFieldTester(org.datanucleus.samples.widget.CollectionFieldTester) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) Department(org.jpox.samples.models.company.Department) Employee(org.jpox.samples.models.company.Employee) Person(org.jpox.samples.models.company.Person)

Aggregations

Extent (javax.jdo.Extent)72 PersistenceManager (javax.jdo.PersistenceManager)72 Transaction (javax.jdo.Transaction)70 Iterator (java.util.Iterator)62 JDOUserException (javax.jdo.JDOUserException)35 Collection (java.util.Collection)22 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)22 Query (javax.jdo.Query)15 Manager (org.jpox.samples.models.company.Manager)13 StoreManager (org.datanucleus.store.StoreManager)11 Method (java.lang.reflect.Method)10 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)10 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)10 Employee (org.jpox.samples.models.company.Employee)10 JDOException (javax.jdo.JDOException)9 SQLException (java.sql.SQLException)8 Department (org.jpox.samples.models.company.Department)8 InversePrimitive (org.datanucleus.samples.widget.InversePrimitive)7 Primitive (org.datanucleus.samples.widget.Primitive)6 List (java.util.List)5