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