use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class SchemaHandlerTest method testColumnRetrieval.
/**
* Test of the retrieval of columns.
*/
public void testColumnRetrieval() {
addClassesToSchema(new Class[] { SchemaClass1.class, SchemaClass2.class });
PersistenceManager pm = pmf.getPersistenceManager();
RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
StoreSchemaHandler handler = databaseMgr.getSchemaHandler();
ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
Connection con = (Connection) databaseMgr.getConnectionManager().getConnection(((JDOPersistenceManager) pm).getExecutionContext()).getConnection();
// Retrieve and check the table for SchemaClass1
DatastoreClass table1 = databaseMgr.getDatastoreClass(SchemaClass1.class.getName(), clr);
RDBMSTableInfo tableInfo1 = (RDBMSTableInfo) handler.getSchemaData(con, "columns", new Object[] { table1 });
assertNotNull("TableInfo from getColumns is NULL!", tableInfo1);
assertEquals("Number of columns for table " + table1 + " is wrong", 4, tableInfo1.getNumberOfChildren());
Iterator colsIter = tableInfo1.getChildren().iterator();
Collection colNamesPresent = new HashSet();
colNamesPresent.add("TABLE1_ID1");
colNamesPresent.add("TABLE1_ID2");
colNamesPresent.add("NAME");
colNamesPresent.add("OTHER_ID");
while (colsIter.hasNext()) {
RDBMSColumnInfo colInfo = (RDBMSColumnInfo) colsIter.next();
String colInfoName = colInfo.getColumnName().toUpperCase();
if (colInfoName.equals("TABLE1_ID1") || colInfoName.equals("TABLE1_ID2") || colInfoName.equals("NAME") || colInfoName.equals("OTHER_ID")) {
colNamesPresent.remove(colInfoName);
}
}
assertTrue("Some columns expected were not present in the datastore table : " + StringUtils.collectionToString(colNamesPresent), colNamesPresent.size() == 0);
// Retrieve and check the table for SchemaClass2
DatastoreClass table2 = databaseMgr.getDatastoreClass(SchemaClass2.class.getName(), clr);
RDBMSTableInfo tableInfo2 = (RDBMSTableInfo) handler.getSchemaData(con, "columns", new Object[] { table2 });
assertEquals("Number of columns for table " + table2 + " is wrong", 3, tableInfo2.getNumberOfChildren());
colsIter = tableInfo2.getChildren().iterator();
colNamesPresent.clear();
colNamesPresent.add("TABLE2_ID");
colNamesPresent.add("NAME");
colNamesPresent.add("VALUE");
while (colsIter.hasNext()) {
RDBMSColumnInfo colInfo = (RDBMSColumnInfo) colsIter.next();
String colInfoName = colInfo.getColumnName().toUpperCase();
if (colInfoName.equals("TABLE2_ID")) {
colNamesPresent.remove(colInfoName);
}
if (colInfoName.equals("NAME")) {
colNamesPresent.remove(colInfoName);
assertEquals("Length of column " + colInfo.getColumnName() + " has incorrect length", 20, colInfo.getColumnSize());
}
if (colInfoName.equals("VALUE")) {
colNamesPresent.remove(colInfoName);
}
}
assertTrue("Some columns expected were not present in the datastore table : " + StringUtils.collectionToString(colNamesPresent), colNamesPresent.size() == 0);
// Now check retrieval of a column for a table
RDBMSColumnInfo colInfo = (RDBMSColumnInfo) handler.getSchemaData(con, "column", new Object[] { table2, "VALUE" });
if (colInfo == null) {
colInfo = (RDBMSColumnInfo) handler.getSchemaData(con, "column", new Object[] { table2, "value" });
}
assertNotNull("Column VALUE for table " + table2 + " was not found", colInfo);
assertEquals("Column name is wrong", "VALUE", colInfo.getColumnName().toUpperCase());
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class SchemaHandlerTest method testPrimaryKeyRetrieval.
/**
* Test of the retrieval of PKs.
*/
public void testPrimaryKeyRetrieval() {
addClassesToSchema(new Class[] { SchemaClass1.class, SchemaClass2.class });
PersistenceManager pm = pmf.getPersistenceManager();
RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
// Retrieve the table for SchemaClass1
ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
DatastoreClass table1 = databaseMgr.getDatastoreClass(SchemaClass1.class.getName(), clr);
DatastoreClass table2 = databaseMgr.getDatastoreClass(SchemaClass2.class.getName(), clr);
// Check for the FK using the schema handler
StoreSchemaHandler handler = databaseMgr.getSchemaHandler();
Connection con = (Connection) databaseMgr.getConnectionManager().getConnection(((JDOPersistenceManager) pm).getExecutionContext()).getConnection();
RDBMSTablePKInfo pkInfo1 = (RDBMSTablePKInfo) handler.getSchemaData(con, "primary-keys", new Object[] { table1 });
RDBMSTablePKInfo pkInfo2 = (RDBMSTablePKInfo) handler.getSchemaData(con, "primary-keys", new Object[] { table2 });
// Expecting 2 PK columns for SchemaClass1
// TODO Enable checks on the PK name (when JDBC drivers return it correctly)
assertEquals("Number of PKs for table " + table1 + " is wrong", 2, pkInfo1.getNumberOfChildren());
PrimaryKeyInfo pk = (PrimaryKeyInfo) pkInfo1.getChild(0);
assertEquals("Column Name is wrong", "TABLE1_ID1", ((String) pk.getProperty("column_name")).toUpperCase());
// assertEquals("PK Name is wrong", "TABLE1_PK", pk.getProperty("pk_name"));
pk = (PrimaryKeyInfo) pkInfo1.getChild(1);
assertEquals("Column Name is wrong", "TABLE1_ID2", ((String) pk.getProperty("column_name")).toUpperCase());
// assertEquals("PK Name is wrong", "TABLE1_PK", pk.getProperty("pk_name"));
// Expecting 1 PK column for SchemaClass
assertEquals("Number of PKs for table " + table1 + " is wrong", 1, pkInfo2.getNumberOfChildren());
pk = (PrimaryKeyInfo) pkInfo2.getChild(0);
assertEquals("Column Name is wrong", "TABLE2_ID", ((String) pk.getProperty("column_name")).toUpperCase());
// assertEquals("PK Name is wrong", "TABLE2_PK", pk.getProperty("pk_name"));
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class TypeQueryTest method testQueryWithDateJdbcTypeChange.
/**
* Test for persistence of a java.util.Date with a jdbc-type of "DATE" and queried against
* an input date parameter.
*/
public void testQueryWithDateJdbcTypeChange() {
try {
// Persist some objects
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Calendar cal = Calendar.getInstance();
cal.set(2005, 5, 20);
Date cutOffDate = cal.getTime();
try {
tx.begin();
DateHolder holder1 = new DateHolder();
Calendar cal1 = Calendar.getInstance();
cal1.set(2003, 6, 1);
holder1.setDateField(cal1.getTime());
DateHolder holder2 = new DateHolder();
Calendar cal2 = Calendar.getInstance();
cal2.set(2006, 6, 1);
holder2.setDateField(cal2.getTime());
pm.makePersistent(holder1);
pm.makePersistent(holder2);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error("Exception thrown persisting object with date field", e);
fail("Exception thrown persisting objects : " + e.getMessage());
return;
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Query using an input parameter
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(DateHolder.class, "dateField < :myDate");
List results = (List) q.execute(cutOffDate);
assertEquals("Number of objects returned from Query of date is wrong", 1, results.size());
tx.commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error("Exception thrown querying object using date parameter", e);
fail("Exception thrown querying object using date parameter with changed jdbc-type : " + e.getMessage());
return;
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
// Clean out our data
clean(DateHolder.class);
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class TypesMappingTest method testMSSQLUniqueIdentifierType.
/**
* Test for MSSQL datastore "UNIQUEIDENTIFIER" type.
*/
public void testMSSQLUniqueIdentifierType() {
if (!vendorID.equals("sqlserver")) {
return;
}
MSSQLTypes types;
Object id = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
types = new MSSQLTypes();
types.setSimpleString("some string");
pm.makePersistent(types);
id = JDOHelper.getObjectId(types);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
types = (MSSQLTypes) pm.getObjectById(id, true);
assertEquals("UUIDString retrieved is not 36 chars", 36, types.getUuid().length());
assertEquals("Simple String retrieved is wrong", "some string", types.getSimpleString());
assertEquals("UUIDString retrieved is not 36 chars", 36, types.getAnotherString().length());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
tx = pm.currentTransaction();
try {
tx.begin();
types = new MSSQLTypes();
types.setUuid("6F9619FF-8B86-D011-B42D-00C04FC964FF");
types.setSimpleString("some string");
pm.makePersistent(types);
id = JDOHelper.getObjectId(types);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
types = (MSSQLTypes) pm.getObjectById(id, true);
assertEquals("UUIDString retrieved is not 36 chars", 36, types.getUuid().length());
assertEquals("UUIDString retrieved is not 6F9619FF-8B86-D011-B42D-00C04FC964FF", "6F9619FF-8B86-D011-B42D-00C04FC964FF", types.getUuid());
assertEquals("Simple String retrieved is wrong", "some string", types.getSimpleString());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.PersistenceManager in project tests by datanucleus.
the class TypesMappingTest method testXMLType.
/**
* Test for Oracles XMLType.
* TODO Need to have Oracle XMLType in CLASSPATH for this
*/
public void testXMLType() {
if (!vendorID.equals("oracle")) {
return;
}
OracleTypes types;
Object id = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
String xml = "<jdo><package/></jdo>";
String xml2 = "<jdo><package name=\"org.datanucleus\"/></jdo>";
try {
tx.begin();
types = new OracleTypes();
types.setXml(xml);
pm.makePersistent(types);
id = JDOHelper.getObjectId(types);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
types = (OracleTypes) pm.getObjectById(id, true);
assertEquals(xml, types.getXml());
tx.commit();
// test update
tx.begin();
types = (OracleTypes) pm.getObjectById(id, true);
types.setXml(xml2);
tx.commit();
tx.begin();
types = (OracleTypes) pm.getObjectById(id, true);
pm.refresh(types);
assertEquals(xml2, types.getXml());
tx.commit();
// test update to null
tx.begin();
types = (OracleTypes) pm.getObjectById(id, true);
types.setXml(null);
tx.commit();
tx.begin();
types = (OracleTypes) pm.getObjectById(id, true);
pm.refresh(types);
assertNull(types.getXml());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
// test null
tx = pm.currentTransaction();
try {
tx.begin();
types = new OracleTypes();
types.setXml(null);
pm.makePersistent(types);
id = JDOHelper.getObjectId(types);
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
types = (OracleTypes) pm.getObjectById(id, true);
assertNull(types.getXml());
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
Aggregations