use of javax.jdo.Transaction in project tests by datanucleus.
the class ApplicationIdPersistenceTest method testOneToOne.
public void testOneToOne() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object p1Id = null;
Object p2Id = null;
try {
tx.begin();
Person p1 = new Person();
p1.setPersonNum(1);
p1.setGlobalNum("1");
p1.setFirstName("Bugs");
p1.setLastName("Bunny");
Person p2 = new Person();
p2.setPersonNum(2);
p2.setGlobalNum("2");
p2.setFirstName("Daffy");
p2.setLastName("Duck");
p2.setBestFriend(p1);
pm.makePersistent(p2);
tx.commit();
p1Id = pm.getObjectId(p1);
p2Id = pm.getObjectId(p2);
} catch (Exception e) {
LOG.error("Exception during 1-1 persist", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// No L2 cache interference
pmf.getDataStoreCache().evictAll();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
// Check number of objects present
Query q = pm.newQuery(Person.class);
List<Person> results = (List<Person>) q.execute();
assertEquals(2, results.size());
tx.commit();
} catch (Exception e) {
LOG.error("Exception during 1-1 retrieve and check", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Person p1 = null;
try {
p1 = (Person) pm.getObjectById(p1Id);
assertEquals("Bugs", p1.getFirstName());
assertEquals("Bunny", p1.getLastName());
assertEquals(1, p1.getPersonNum());
} catch (JDOObjectNotFoundException onfe) {
fail("Person p1 not found");
}
assertNull("p1 best friend should be null", p1.getBestFriend());
Person p2 = null;
try {
p2 = (Person) pm.getObjectById(p2Id);
assertEquals("Daffy", p2.getFirstName());
assertEquals("Duck", p2.getLastName());
assertEquals(2, p2.getPersonNum());
} catch (JDOObjectNotFoundException onfe) {
fail("Person p2 not found");
}
assertNotNull("p2 best friend should not be null", p2.getBestFriend());
assertEquals("p2 best friend should be same as p1 object", p1, p2.getBestFriend());
tx.commit();
} catch (Exception e) {
LOG.error("Exception during 1-1 retrieve and check", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Person.class);
}
}
use of javax.jdo.Transaction in project tests by datanucleus.
the class ApplicationIdPersistenceTest method testEnum.
/**
* Test persistence of an enum as a String and as ordinal.
*/
public void testEnum() {
Palette p;
Object id = null;
try {
// ---------------------
// RED
// ---------------------
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
p = new Palette();
p.setAmount(100);
p.setColour(Colour.RED);
p.setColourOrdinal(Colour.RED);
pm.makePersistent(p);
id = JDOHelper.getObjectId(p);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.getDataStoreCache().evictAll();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
p = (Palette) pm.getObjectById(id, true);
assertEquals(100, p.getAmount());
assertEquals(Colour.RED, p.getColour());
assertEquals(Colour.RED, p.getColourOrdinal());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// ---------------------
// null
// ---------------------
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
p = new Palette();
p.setAmount(101);
p.setColour(null);
p.setColourOrdinal(null);
pm.makePersistent(p);
id = JDOHelper.getObjectId(p);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.getDataStoreCache().evictAll();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
p = (Palette) pm.getObjectById(id, true);
assertEquals(101, p.getAmount());
assertNull(p.getColour());
assertNull(p.getColourOrdinal());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// ---------------------
// GREEN
// ---------------------
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
p = new Palette();
p.setAmount(102);
p.setColour(Colour.GREEN);
p.setColourOrdinal(Colour.GREEN);
pm.makePersistent(p);
id = JDOHelper.getObjectId(p);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.getDataStoreCache().evictAll();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
p = (Palette) pm.getObjectById(id, true);
assertEquals(102, p.getAmount());
assertEquals(Colour.GREEN, p.getColour());
assertEquals(Colour.GREEN, p.getColourOrdinal());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Palette.class);
}
}
use of javax.jdo.Transaction in project tests by datanucleus.
the class ApplicationIdPersistenceTest method testSurrogateVersion.
/**
* Test of persistence/retrieve of objects with surrogate version.
*/
public void testSurrogateVersion() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
try {
tx.begin();
Organisation org1 = new Organisation("First");
org1.setDescription("Original Description");
pm.makePersistent(org1);
tx.commit();
id = JDOHelper.getObjectId(org1);
assertEquals("Incorrect version after persist", new Long(1), JDOHelper.getVersion(org1));
} catch (Exception e) {
LOG.error("Exception persisting data", e);
fail("Exception thrown persisting data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.getDataStoreCache().evictAll();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Organisation org1 = (Organisation) pm.getObjectById(id);
assertEquals("Incorrect version after getObjectById", new Long(1), JDOHelper.getVersion(org1));
tx.commit();
} catch (Exception e) {
LOG.error("Exception retrieving data", e);
fail("Exception thrown persisting data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.getDataStoreCache().evictAll();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(Organisation.class);
List<Organisation> results = (List<Organisation>) q.execute();
assertNotNull("No results from query!", results);
assertEquals("Incorrect number of Organisation objects!", 1, results.size());
Organisation org1 = results.iterator().next();
assertEquals("Incorrect version after query", new Long(1), JDOHelper.getVersion(org1));
// Provoke an update
org1.setDescription("New Description");
tx.commit();
assertEquals("Incorrect version after update", new Long(2), JDOHelper.getVersion(org1));
} catch (Exception e) {
LOG.error("Exception retrieving data", e);
fail("Exception thrown persisting data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.getDataStoreCache().evictAll();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Organisation org1 = (Organisation) pm.getObjectById(id);
assertEquals("Incorrect version after getObjectById", new Long(2), JDOHelper.getVersion(org1));
tx.commit();
} catch (Exception e) {
LOG.error("Exception retrieving data", e);
fail("Exception thrown persisting data " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Do clean up
clean(Organisation.class);
}
}
use of javax.jdo.Transaction in project tests by datanucleus.
the class ApplicationIdPersistenceTest method testInheritanceAndFloat.
/**
* Test persistence of inheritance, and persistence of float.
*/
public void testInheritanceAndFloat() {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Employee e = new Employee();
e.setPersonNum(1);
e.setGlobalNum("1");
e.setFirstName("Bugs");
e.setLastName("Bunny");
e.setSalary(123);
pm.makePersistent(e);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.getDataStoreCache().evictAll();
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(Employee.class);
List<Employee> employees = (List<Employee>) q.execute();
assertNotNull(employees);
assertEquals("Number of employees is wrong", 1, employees.size());
Employee e = employees.get(0);
assertEquals("Employee name is wrong", "Bugs", e.getFirstName());
assertEquals("Employee name is wrong", "Bunny", e.getLastName());
assertEquals("Employee salary is wrong", 123, e.getSalary(), 0.1);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Employee.class);
}
}
use of javax.jdo.Transaction in project tests by datanucleus.
the class ApplicationIdPersistenceTest method testAccessJdoConnection.
public void testAccessJdoConnection() throws Exception {
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Person p = new Person();
p.setPersonNum(1);
p.setGlobalNum("1");
p.setFirstName("Bugs");
p.setLastName("Bunny");
Person p2 = new Person();
p2.setPersonNum(2);
p2.setGlobalNum("2");
p2.setFirstName("My");
p2.setLastName("Friend");
pm.makePersistent(p);
pm.makePersistent(p2);
JDOConnection conn = pm.getDataStoreConnection();
assertTrue("Native connection should be instanceof com.mongodb.DB!", conn.getNativeConnection() instanceof DB);
conn.close();
tx.commit();
} catch (Exception e) {
LOG.error("Exception during persist with connection access", e);
fail("Exception thrown when running test " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Person.class);
}
}
Aggregations