use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class SchemaTest method testDefaultedFields.
/**
* Test of the specification and persistence of a class with defaulted fields.
*/
public void testDefaultedFields() {
addClassesToSchema(new Class[] { Employee.class });
try {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id = null;
try {
tx.begin();
Employee e = new Employee(245, "Fred", "Flintstone", "fred.flintstone@warnerbros.com", (float) 178.90, "1245C");
pm.makePersistent(e);
tx.commit();
id = pm.getObjectId(e);
} catch (Exception e) {
LOG.error(e);
fail("Persistence of object with defaulted fields failed when should have just used defaults : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Avoid L2 cache interference if enabled
pmf.getDataStoreCache().evictAll(false, Employee.class);
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Employee e = (Employee) pm.getObjectById(id);
assertEquals("Defaulted SalaryCurrency is incorrect", "GBP", e.getSalaryCurrency());
tx.commit();
} catch (Exception e) {
LOG.error(e);
fail("Checking of object with defaulted fields failed : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Employee.class);
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class SchemaTest method testColumnWidth.
/**
* Test of the column width specification.
* Test the PMF property "datanucleus.rdbms.stringLengthExceededAction".
*/
public void testColumnWidth() {
try {
// 1). Persist an object with a "serialNo" too long for the column, and expect an Exception
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Employee e = new Employee(245, "Fred", "Flintstone", "fred.flintstone@warnerbros.com", (float) 178.90, "123456789012345");
pm.makePersistent(e);
tx.commit();
fail("Persisted an object with a field value that was too long for the column storing it!");
} catch (JDOFatalUserException e) {
// Expected this to be thrown
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// 2). Persist an object with a "serialNo" too long for the column, and use PMF option to truncate
Properties userProps = new Properties();
userProps.setProperty(RDBMSPropertyNames.PROPERTY_RDBMS_STRING_LENGTH_EXCEEDED_ACTION, "TRUNCATE");
PersistenceManagerFactory pmf2 = getPMF(1, userProps);
pm = pmf2.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Employee e = new Employee(245, "Fred", "Flintstone", "fred.flintstone@warnerbros.com", (float) 178.90, "123456789012345");
pm.makePersistent(e);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown when persisting object with too-long String field but truncate selected");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf2.close();
} finally {
clean(Employee.class);
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class SchemaTest method testReadOnlyDatastore.
/**
* Test of the Read-Only datastore facility.
* Should prevent all attempts to write to the datastore.
*/
public void testReadOnlyDatastore() {
try {
// Create the necessary table and create a few objects
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Make sure our read-write PMF has schema for this class
pm.getExtent(Developer.class);
// Make sure our read-write PMF has schema for this class
pm.getExtent(Manager.class);
Employee e = new Employee(123, "Barney", "Rubble", "barney.rubble@warnerbros.com", (float) 123.45, "1245C");
pm.makePersistent(e);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Create a PMF for our read-only schema
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_DATASTORE_READONLY, "true");
PersistenceManagerFactory pmf2 = getPMF(1, userProps);
assertTrue("The PMF should have had the ReadOnlyDatastore property, yet hasn't", getConfigurationForPMF(pmf2).getBooleanProperty(PropertyNames.PROPERTY_DATASTORE_READONLY));
PersistenceManager pm2 = pmf2.getPersistenceManager();
// a). Try makePersistent
Transaction tx2 = pm2.currentTransaction();
try {
tx2.begin();
Employee e = new Employee(123, "Barney", "Rubble", "barney.rubble@warnerbros.com", (float) 123.45, "1245C");
pm2.makePersistent(e);
tx2.commit();
assertTrue("Should have thrown an exception when trying makePersistent on ReadOnly datastore", false);
} catch (Exception e) {
LOG.error(e);
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
}
// b). Try deletePersistent
tx2 = pm2.currentTransaction();
try {
tx2.begin();
Extent ex = pm2.getExtent(Employee.class, true);
Iterator iter = ex.iterator();
while (iter.hasNext()) {
Employee e = (Employee) iter.next();
pm2.deletePersistent(e);
}
tx2.commit();
assertTrue("Should have thrown an exception when trying deletePersistent on ReadOnly datastore", false);
} catch (Exception e) {
LOG.error(e);
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
}
// c). Try update
tx2 = pm2.currentTransaction();
try {
tx2.begin();
Extent ex = pm2.getExtent(Employee.class, true);
Iterator iter = ex.iterator();
while (iter.hasNext()) {
Employee e = (Employee) iter.next();
e.setAge(23);
}
tx2.commit();
assertTrue("Should have thrown an exception when modifying an object on ReadOnly datastore", false);
} catch (Exception e) {
LOG.error(e);
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
}
// d). Try query
tx2 = pm2.currentTransaction();
try {
tx2.begin();
Query q = pm2.newQuery(Employee.class);
Collection results = (Collection) q.execute();
Iterator resultsIter = results.iterator();
while (resultsIter.hasNext()) {
resultsIter.next();
}
tx2.commit();
} catch (Exception e) {
assertTrue("Should have been able to access objects on a ReadOnly datastore", false);
LOG.error(e);
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
}
pm2.close();
pmf2.close();
} finally {
clean(Employee.class);
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class JDOQLQueryTest method testCast.
/**
* Test use of a cast operator.
*/
public void testCast() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Persist 2 Employees
Employee emp1 = new Employee(101, "F1", "S1", "f1.s1@company.com", 100f, "10001");
Employee emp2 = new Employee(102, "F2", "S2", "f2.s2@company.com", 200f, "10002");
pm.makePersistent(emp1);
pm.makePersistent(emp2);
pm.flush();
// Find the Employee with the specified serial number
LOG.info(">> Querying for cast Employee serial number");
Query q = pm.newQuery(Person.class, "((Employee)this).serialNo == \"10001\"");
List results = (List) q.execute();
assertNotNull("No results from query!", results);
assertEquals("Number of Employees with serial number was incorrect", 1, results.size());
tx.commit();
} catch (JDOUserException e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Employee.class);
}
}
use of org.jpox.samples.models.company.Employee in project tests by datanucleus.
the class JDOQLQueryTest method testSingleStringSubquery.
/**
* Test a subquery using the JDOQL Query as single-string.
*/
public void testSingleStringSubquery() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Persist 2 Employees
Employee emp1 = new Employee(101, "F1", "S1", "f1.s1@company.com", 100f, "10001");
Employee emp2 = new Employee(102, "F2", "S2", "f2.s2@company.com", 200f, "10002");
pm.makePersistent(emp1);
pm.makePersistent(emp2);
pm.flush();
// Find the Employees earning more than the average salary
Query q = pm.newQuery("JDOQL", "SELECT FROM " + Employee.class.getName() + " WHERE salary > (SELECT avg(salary) FROM " + Employee.class.getName() + ")");
q.addExtension("datanucleus.query.evaluateInMemory", "true");
q.compile();
List results = (List) q.execute();
assertNotNull("No results from query!", results);
assertEquals("Number of Employees with more than average salary was wrong", 1, results.size());
tx.commit();
} catch (JDOUserException e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
clean(Employee.class);
}
}
Aggregations