Search in sources :

Example 36 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class PersistenceManagerTest method testFKCollectionFieldPersistenceByReachability2.

/**
 * Test that when an element with N-1 relation with FK collection is persisted, the owning PC is persisted also.
 * TODO Move to reachability tests.
 */
public void testFKCollectionFieldPersistenceByReachability2() {
    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(d);
        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));
        tx.commit();
    } 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 37 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class PersistenceManagerTest method testNormalFCOCollectionFieldPersistence1.

/**
 * Test that FCOs added to a Collection field are persisted when the owning PC is persisted.
 */
public void testNormalFCOCollectionFieldPersistence1() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Manager mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
    Employee emp1 = new Employee(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1], EMP_SALARY[1], EMP_SERIAL[1]);
    try {
        pm.currentTransaction().begin();
        mgr.addSubordinate(emp1);
        pm.makePersistent(mgr);
        pm.currentTransaction().commit();
    } finally {
        if (pm.currentTransaction().isActive()) {
            pm.currentTransaction().rollback();
            pm.close();
            fail("Failed to persist object and commit transaction");
        }
        pm.close();
    }
    // get a fresh PM to ensure that any results aren't coming from the cache
    try {
        pm = pmf.getPersistenceManager();
        pm.currentTransaction().begin();
        Extent ext = pm.getExtent(Manager.class, false);
        java.util.Iterator it = ext.iterator();
        assertTrue(it.hasNext());
        mgr = (Manager) it.next();
        Collection c = mgr.getSubordinates();
        assertEquals(1, c.size());
        ext = pm.getExtent(Employee.class, false);
        it = ext.iterator();
        assertTrue(it.hasNext());
        Employee emp = (Employee) it.next();
        assertTrue(c.contains(emp));
    } finally {
        if (pm.currentTransaction().isActive())
            pm.currentTransaction().rollback();
        pm.close();
    }
}
Also used : Employee(org.jpox.samples.models.company.Employee) 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)

Example 38 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class PersistenceManagerTest method testMakeCollectionFieldsPersistent.

/**
 * Test that updates of collection and inverse collection fields update
 * the underlying collection
 */
public void testMakeCollectionFieldsPersistent() throws Exception {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_SERIALISED_COLLECTION_ELEMENT)) {
        return;
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    CollectionFieldTester collectionTester = new CollectionFieldTester();
    Transaction tx = pm.currentTransaction();
    try {
        // data for testing collections
        Person[] personArray = new Person[3];
        InversePrimitive[] inversePrimitiveArray = new InversePrimitive[3];
        personArray[0] = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]);
        personArray[1] = new Person(1, FIRSTNAME[1], LASTNAME[1], EMAIL[1]);
        personArray[2] = new Person(2, FIRSTNAME[2], LASTNAME[2], EMAIL[2]);
        inversePrimitiveArray[0] = new InversePrimitive(collectionTester);
        inversePrimitiveArray[1] = new InversePrimitive(collectionTester);
        inversePrimitiveArray[2] = new InversePrimitive(collectionTester);
        tx.begin();
        pm.makePersistent(collectionTester);
        tx.commit();
        tx.begin();
        // set up Set field data
        {
            HashSet<Person> s = new HashSet<Person>(3);
            s.add(personArray[0]);
            s.add(personArray[1]);
            s.add(personArray[2]);
            collectionTester.setPersonSet(s);
        }
        // set up inverse element data
        {
            HashSet<InversePrimitive> s = new HashSet<InversePrimitive>(3);
            s.add(inversePrimitiveArray[0]);
            s.add(inversePrimitiveArray[1]);
            s.add(inversePrimitiveArray[2]);
            collectionTester.setInversePrimitiveCollection(s);
        }
        tx.commit();
        collectionTester = null;
        // test results
        tx.begin();
        {
            Extent clnTest = pm.getExtent(CollectionFieldTester.class, false);
            // should only have one Primitive object
            java.util.Iterator it = clnTest.iterator();
            collectionTester = (CollectionFieldTester) it.next();
        }
        assertNotNull(collectionTester);
        // test that Set data was stored
        {
            Set s = collectionTester.getPersonSet();
            assertTrue(s.contains(personArray[0]));
            assertTrue(s.contains(personArray[1]));
            assertTrue(s.contains(personArray[2]));
            assertEquals(3, s.size());
            Collection c = collectionTester.getInversePrimitiveCollection();
            assertTrue(c.contains(inversePrimitiveArray[0]));
            assertTrue(c.contains(inversePrimitiveArray[1]));
            assertTrue(c.contains(inversePrimitiveArray[2]));
            assertEquals(3, c.size());
        }
        tx.commit();
    } finally {
        if (tx.isActive())
            tx.rollback();
        pm.close();
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) CollectionFieldTester(org.datanucleus.samples.widget.CollectionFieldTester) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection) Person(org.jpox.samples.models.company.Person) HashSet(java.util.HashSet)

Example 39 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class PersistenceManagerTest method testUpdatePersistentFields.

/**
 * Simple test of transactional PC field updates.
 */
public void testUpdatePersistentFields() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            BigDecimal bd = new BigDecimal("12345.12345");
            BigInteger bi = new BigInteger("12345");
            java.util.Date date1 = (new java.util.GregorianCalendar()).getTime();
            java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01");
            java.sql.Time time3 = java.sql.Time.valueOf("10:01:59");
            java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2001-01-01 23:23:23.050500000");
            tx.begin();
            Primitive p = new Primitive();
            setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", bd, bi, date1, date2, time3, timestamp);
            pm.makePersistent(p);
            tx.commit();
            p = null;
            // Retrieve the object and update some fields
            tx.begin();
            Extent clnPrimitive = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
            p = (Primitive) clnPrimitive.iterator().next();
            date1 = new java.util.Date(date1.getTime() + 10000);
            date2 = java.sql.Date.valueOf("2001-01-02");
            time3 = java.sql.Time.valueOf("10:01:59");
            timestamp = java.sql.Timestamp.valueOf("2001-01-02 23:23:23.050500000");
            setPrimitiveValues(p, false, (byte) 22, 'a', 34, (short) 44, 1234567890, 456.456F, 456.456, "fixedlength", "normalsize", "hugexyzabc", bd, bi, date1, date2, time3, timestamp);
            tx.commit();
            // test the results
            tx.begin();
            clnPrimitive = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
            p = (Primitive) clnPrimitive.iterator().next();
            assertPrimitiveValues(p, false, (byte) 22, 'a', 34, (short) 44, 1234567890, 456.456F, 456.456, "fixedlength", "normalsize", "hugexyzabc", bd, bi, date1, date2, time3, timestamp);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Primitive.class);
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) BigDecimal(java.math.BigDecimal) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) Transaction(javax.jdo.Transaction) BigInteger(java.math.BigInteger)

Example 40 with Extent

use of javax.jdo.Extent in project tests by datanucleus.

the class PersistenceManagerTest method testInheritedFieldsPersisted.

/**
 * Test that inherited fields are persisted.
 */
public void testInheritedFieldsPersisted() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Manager mgr = null;
        try {
            tx.begin();
            mgr = new Manager(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0], EMP_SALARY[0], EMP_SERIAL[0]);
            pm.makePersistent(mgr);
            tx.commit();
            tx.begin();
            Extent ext = pm.getExtent(Manager.class, false);
            java.util.Iterator it = ext.iterator();
            assertTrue(it.hasNext());
            mgr = (Manager) it.next();
            assertEquals(FIRSTNAME[0], mgr.getFirstName());
            assertEquals(LASTNAME[0], mgr.getLastName());
            assertEquals(EMP_SALARY[0], mgr.getSalary(), 0.0F);
            pm.currentTransaction().commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
                pm.close();
                fail("Failed to persist object and commit transaction");
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            // get a fresh pm to ensure that data is coming from the store,
            // not the cache
            tx.begin();
            Extent ext = pm.getExtent(Manager.class, false);
            java.util.Iterator it = ext.iterator();
            assertTrue(it.hasNext());
            mgr = (Manager) it.next();
            assertEquals(FIRSTNAME[0], mgr.getFirstName());
            assertEquals(LASTNAME[0], mgr.getLastName());
            assertEquals(EMP_SALARY[0], mgr.getSalary(), 0.0F);
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Manager.class);
    }
}
Also used : 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)

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