Search in sources :

Example 1 with Primitive

use of org.datanucleus.samples.widget.Primitive in project tests by datanucleus.

the class PersistenceManagerTest method testGetObjectById.

/**
 * Test for getObjectById()
 * @throws Exception
 */
public void testGetObjectById() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Test insert of native and native wrapper data types
            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();
            Object id = pm.getObjectId(p);
            tx.begin();
            // Check the identity before and after retrieval
            Primitive p1 = (Primitive) pm.getObjectById(id);
            Object id1 = pm.getObjectId(p1);
            assertEquals(id, id1);
            boolean success = false;
            // Check the throwing of an error with an invalid id
            try {
                pm.getObjectById(new Integer(1));
            } catch (JDOObjectNotFoundException ex) {
                success = true;
            }
            assertTrue("Expected JDOObjectNotFoundException", success);
            // Check the throwing of an error with a null id
            try {
                success = false;
                pm.getObjectById(null);
            } catch (JDOUserException ex) {
                success = true;
            }
            assertTrue("Expected JDOUserException", success);
            // Check the throwing of an error with invalid id
            try {
                pm.getObjectById(new LongIdentity(Primitive.class, "111"));
                fail("Expected JDOUserException");
            } catch (JDOUserException ex) {
            // 
            }
            try {
                success = false;
                pm.getObjectById(p1);
            } catch (JDOObjectNotFoundException ex) {
                success = true;
            }
            assertTrue("Expected JDOObjectNotFoundException", success);
            tx.rollback();
            p = new Primitive();
            tx.begin();
            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);
            id = pm.getObjectId(p);
            assertTrue(p == pm.getObjectById(id));
            tx.commit();
        } 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) JDOUserException(javax.jdo.JDOUserException) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) Transaction(javax.jdo.Transaction) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) BigInteger(java.math.BigInteger) LongIdentity(javax.jdo.identity.LongIdentity)

Example 2 with Primitive

use of org.datanucleus.samples.widget.Primitive in project tests by datanucleus.

the class PersistenceManagerTest method testJoinTableCollectionFieldPersistence1.

/**
 * This test was written as a result of a bug that was found. Test
 * persisting an object with a collection field, add objects to the
 * collection, make the owning object transient, and then persist it again.
 */
public void testJoinTableCollectionFieldPersistence1() {
    if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_ORM_SERIALISED_COLLECTION_ELEMENT)) {
        return;
    }
    PersistenceManager pm = pmf.getPersistenceManager();
    CollectionFieldTester tester = new CollectionFieldTester();
    for (int i = 0; i < 3; i++) {
        Primitive p = new Primitive();
        setPrimitiveValues(p);
        tester.getPrimitiveCollection().add(p);
    }
    try {
        pm.currentTransaction().begin();
        pm.makePersistent(tester);
        pm.currentTransaction().commit();
        pm.currentTransaction().begin();
        pm.makeTransient(tester);
        pm.makePersistent(tester);
        pm.currentTransaction().commit();
    } finally {
        if (pm.currentTransaction().isActive())
            pm.currentTransaction().rollback();
        pm.close();
    }
}
Also used : CollectionFieldTester(org.datanucleus.samples.widget.CollectionFieldTester) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager)

Example 3 with Primitive

use of org.datanucleus.samples.widget.Primitive in project tests by datanucleus.

the class PersistenceManagerTest method testGetObjectId.

/**
 * Test for getObjectId() method returning the identity of an object.
 */
public void testGetObjectId() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Test insert of native and native wrapper data types
            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();
            tx.begin();
            Primitive p1 = (Primitive) pm.detachCopy(p);
            tx.commit();
            Object id = pm.getObjectId(p);
            Object id1 = pm.getObjectId(null);
            Object id2 = pm.getObjectId(new Integer(1));
            Object id3 = pm.getObjectId(p1);
            Object id4 = pm.getObjectId(new Primitive());
            assertNotNull(id);
            assertNull(id1);
            assertNull(id2);
            assertNotNull(id3);
            assertNull(id4);
            tx.begin();
            p.setBoolean(false);
            p.setBoolean(true);
            Object id5 = pm.getObjectId(p);
            assertNotNull(id5);
            tx.commit();
        } 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) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) Transaction(javax.jdo.Transaction) BigInteger(java.math.BigInteger)

Example 4 with Primitive

use of org.datanucleus.samples.widget.Primitive 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 5 with Primitive

use of org.datanucleus.samples.widget.Primitive in project tests by datanucleus.

the class PersistenceManagerTest method testGetObjectByIdNonTransactional.

/**
 * Test for getObjectById() when the object is retrieved from the L2 cache.
 * @throws Exception
 */
public void testGetObjectByIdNonTransactional() throws Exception {
    try {
        Object id = null;
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Insert a record for use later
            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();
            id = pm.getObjectId(p);
            tx.begin();
            Primitive p1 = (Primitive) pm.getObjectById(id);
            Object id1 = pm.getObjectId(p1);
            assertEquals(id, id1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Retrieve the object in a nontransactionalRead PMF from L2 cache
        PersistenceManager pm2 = null;
        try {
            pm = pmf.getPersistenceManager();
            try {
                // first getObjectById() will result in hollow instance that
                // is put in L2 cache
                Object pc = pm.getObjectById(id);
                // pin pc in the L2 cache to make sure it is not garbage collected
                pmf.getDataStoreCache().pin(pc);
                // take a new PM to have an empty L1 cache
                pm2 = pmf.getPersistenceManager();
                // take PC from L2 cache
                pm2.getObjectById(id);
            } catch (TransactionNotActiveException e) {
                LOG.error("Exception thrown in test", e);
                fail("TransactionNotActiveException thrown even though nontransactionRead is true");
            }
        } finally {
            if (!pm.isClosed()) {
                pm.close();
            }
            if (pm2 != null) {
                pm2.close();
            }
        }
    } finally {
        // Clean out our data
        clean(Primitive.class);
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) BigDecimal(java.math.BigDecimal) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) Transaction(javax.jdo.Transaction) TransactionNotActiveException(org.datanucleus.api.jdo.exceptions.TransactionNotActiveException) BigInteger(java.math.BigInteger)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)12 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)12 InversePrimitive (org.datanucleus.samples.widget.InversePrimitive)12 Primitive (org.datanucleus.samples.widget.Primitive)12 Transaction (javax.jdo.Transaction)10 BigDecimal (java.math.BigDecimal)8 BigInteger (java.math.BigInteger)8 Extent (javax.jdo.Extent)6 CollectionFieldTester (org.datanucleus.samples.widget.CollectionFieldTester)3 Iterator (java.util.Iterator)2 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)2 JDOUserException (javax.jdo.JDOUserException)2 TransactionNotActiveException (org.datanucleus.api.jdo.exceptions.TransactionNotActiveException)2 StoreManager (org.datanucleus.store.StoreManager)2 Manager (org.jpox.samples.models.company.Manager)2 SQLException (java.sql.SQLException)1 JDOException (javax.jdo.JDOException)1 JDOUnsupportedOptionException (javax.jdo.JDOUnsupportedOptionException)1 JDOUserCallbackException (javax.jdo.JDOUserCallbackException)1 LongIdentity (javax.jdo.identity.LongIdentity)1