use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.
the class CacheTest method testL2CacheAfterReadApplicationIdentity.
/**
* Test for storage of an object in the L2 cache from a query or from getObjectById.
*/
public void testL2CacheAfterReadApplicationIdentity() {
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
PersistenceManagerFactory cachePMF = getPMF(1, userProps);
try {
// Create some data we can use for access
PersistenceManager pm = cachePMF.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
DataStoreCache l2Cache = cachePMF.getDataStoreCache();
l2Cache.pinAll(true, Vote.class);
tx.begin();
Vote vote1 = new Vote(1, "Vote 1");
pm.makePersistent(vote1);
Vote vote2 = new Vote(2, "Vote 2");
pm.makePersistent(vote2);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error persisting basic data necessary to run optimistic L2 test");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
Level2Cache l2Cache = ((JDODataStoreCache) cachePMF.getDataStoreCache()).getLevel2Cache();
assertEquals("Incorrect number of pinned objects", 2, l2Cache.getNumberOfPinnedObjects());
assertEquals("Incorrect number of unpinned objects", 0, l2Cache.getNumberOfUnpinnedObjects());
l2Cache.evictAll();
assertEquals("Incorrect number of pinned objects after evict", 0, l2Cache.getNumberOfPinnedObjects());
assertEquals("Incorrect number of unpinned objects after evict", 0, l2Cache.getNumberOfUnpinnedObjects());
// Try getObjectById
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Vote vote1 = pm.getObjectById(Vote.class, 1);
Vote vote2 = pm.getObjectById(Vote.class, 2);
assertNotNull(vote1);
assertNotNull(vote2);
assertEquals("Incorrect number of pinned objects after getObjectById", 2, l2Cache.getNumberOfPinnedObjects());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error updating object that was retrieved from the L2 cache : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
assertEquals("Incorrect number of pinned objects", 2, l2Cache.getNumberOfPinnedObjects());
assertEquals("Incorrect number of unpinned objects", 0, l2Cache.getNumberOfUnpinnedObjects());
l2Cache.evictAll();
assertEquals("Incorrect number of pinned objects after evict", 0, l2Cache.getNumberOfPinnedObjects());
assertEquals("Incorrect number of unpinned objects after evict", 0, l2Cache.getNumberOfUnpinnedObjects());
// Try Query
pm = cachePMF.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query query = pm.newQuery(Vote.class);
Collection votes = (Collection) query.execute();
Iterator iter = votes.iterator();
while (iter.hasNext()) {
iter.next();
}
assertEquals("Incorrect number of pinned objects after Query", 2, l2Cache.getNumberOfPinnedObjects());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
fail("Error retrieving object from the L2 cache : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(cachePMF, Vote.class);
cachePMF.close();
}
}
use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.
the class CacheTest method testL1WeakRefL2.
/**
* Basic test for L1 Cache using "WeakRefCache" and L2 cache.
*/
public void testL1WeakRefL2() {
Properties userProps = new Properties();
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
PersistenceManagerFactory cachePMF = getPMF(1, userProps);
runL2CacheTestForPMF(cachePMF);
}
use of javax.jdo.PersistenceManagerFactory in project tests by datanucleus.
the class SchemaTest method testFixedDatastore.
/**
* Test of the fixed datastore facility.
* Should prevent all attempts to change tables in the datastore, yet allow insert/delete of rows.
*/
public void testFixedDatastore() {
try {
// Create the necessary table and create a few objects
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
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_SCHEMA_AUTOCREATE_TABLES, "false");
PersistenceManagerFactory pmf2 = getPMF(1, userProps);
assertFalse("The PMF should have had the AutoCreate property as false, yet hasn't", getConfigurationForPMF(pmf2).getBooleanProperty(PropertyNames.PROPERTY_SCHEMA_AUTOCREATE_TABLES));
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, "1245D");
pm2.makePersistent(e);
tx2.commit();
} catch (Exception e) {
assertTrue("Should not have thrown an exception when trying makePersistent on fixed datastore", false);
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();
} catch (Exception e) {
assertTrue("Should not have thrown an exception when trying deletePersistent on fixed datastore", false);
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(21);
}
tx2.commit();
} catch (Exception e) {
assertTrue("Should not have thrown an exception when modifying an object on fixed datastore", false);
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 fixed datastore", false);
LOG.error(e);
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
}
pm2.close();
pmf2.close();
} finally {
clean(Employee.class);
}
}
use of javax.jdo.PersistenceManagerFactory 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 javax.jdo.PersistenceManagerFactory 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);
}
}
Aggregations