Search in sources :

Example 11 with Primitive

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

the class PersistenceManagerTest method testMakePersistent.

/**
 * Tests storage of various datatypes using the makePersistent method.
 */
public void testMakePersistent() 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();
            p = null;
            tx.begin();
            // Find the Primitive and check that it was persisted correctly
            Extent clnPrimitive = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
            p = (Primitive) clnPrimitive.iterator().next();
            assertNotNull(p);
            assertPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", bd, bi, date1, date2, time3, timestamp);
            Object id = pm.getObjectId(p);
            tx.commit();
            tx.begin();
            pm.makePersistent(p);
            tx.commit();
            Object id2 = pm.getObjectId(p);
            assertEquals(id, id2);
        } 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 12 with Primitive

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

the class PersistenceManagerTest method testNonTransactionalUpdateWithRollback.

/**
 * Test for NontransactionalWrite and use of rollback after an update.
 */
public void testNonTransactionalUpdateWithRollback() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Primitive p = null;
        Integer intValue = null;
        Short shortValue = null;
        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");
            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);
            intValue = p.getIntObject();
            shortValue = p.getShortObject();
            tx.setRetainValues(true);
            tx.setNontransactionalRead(true);
            tx.setNontransactionalWrite(true);
            tx.begin();
            pm.makePersistent(p);
            tx.commit();
            // Make a nontransactional update
            p.setIntObject(new Integer(1));
            p.setShortObject(new Short((short) 2));
            // Necessary when using nontx.atomic for updates
            intValue = 1;
            // Necessary when using nontx.atomic for updates
            shortValue = 2;
            // Make a change and roll it back
            tx.setRestoreValues(true);
            tx.begin();
            p.setIntObject(new Integer(3));
            tx.rollback();
            assertEquals(1, p.getIntObject().intValue());
            assertEquals(2, p.getShortObject().shortValue());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.setNontransactionalRead(true);
        tx.setNontransactionalWrite(true);
        try {
            // Check the datastore for the effect of the updates
            Extent clnPrimitive = pm.getExtent(org.datanucleus.samples.widget.Primitive.class, false);
            p = (Primitive) clnPrimitive.iterator().next();
            assertNotNull(p);
            assertEquals(intValue.intValue(), p.getIntObject().intValue());
            assertEquals(shortValue.shortValue(), p.getShortObject().shortValue());
        } 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) BigInteger(java.math.BigInteger) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) Transaction(javax.jdo.Transaction) 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