Search in sources :

Example 1 with LongIdentity

use of javax.jdo.identity.LongIdentity 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)

Aggregations

BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 JDOUserException (javax.jdo.JDOUserException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 LongIdentity (javax.jdo.identity.LongIdentity)1 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)1 InversePrimitive (org.datanucleus.samples.widget.InversePrimitive)1 Primitive (org.datanucleus.samples.widget.Primitive)1