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);
}
}
Aggregations