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