use of org.datanucleus.store.rdbms.RDBMSStoreManager in project tests by datanucleus.
the class SchemaTest method testMapOfSimpleSimpleViaXml.
/**
* Test for JPA Map<NonPC, NonPC> using xml.
*/
public void testMapOfSimpleSimpleViaXml() throws Exception {
addClassesToSchema(new Class[] { MapHolder1Xml.class });
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
RDBMSStoreManager databaseMgr = (RDBMSStoreManager) storeMgr;
Connection conn = null;
ManagedConnection mconn = null;
try {
tx.begin();
mconn = databaseMgr.getConnectionManager().getConnection(0);
conn = (Connection) mconn.getConnection();
DatabaseMetaData dmd = conn.getMetaData();
HashSet<String> columnNames = new HashSet<String>();
columnNames.add("JPA_XML_MAPHOLDER1_ID");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_XML_MAPHOLDER1", columnNames);
HashSet<String> columnNames2 = new HashSet<String>();
columnNames2.add("MAPHOLDER1_ID");
columnNames2.add("PROP_NAME");
columnNames2.add("PROP_VALUE");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "JPA_XML_MAPHOLDER1_PROPS", columnNames2);
HashSet<String> columnNames3 = new HashSet<String>();
columnNames3.add("MAPHOLDER1XML_JPA_XML_MAPHOLDER1_ID");
columnNames3.add("PROPERTIES2_KEY");
columnNames3.add("PROPERTIES2_VALUE");
RDBMSTestHelper.checkColumnsForTable(storeMgr, dmd, "MAPHOLDER1XML_PROPERTIES2", columnNames3);
tx.commit();
} catch (Exception e) {
LOG.error("Exception thrown", e);
fail("Exception thrown : " + e.getMessage());
} finally {
if (conn != null) {
mconn.close();
}
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
use of org.datanucleus.store.rdbms.RDBMSStoreManager in project tests by datanucleus.
the class StoredProcedureTest method testExecuteOutputParam.
public void testExecuteOutputParam() {
if (vendorID == null) {
return;
}
if (storeMgr instanceof RDBMSStoreManager) {
DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
if (!dba.supportsOption(DatastoreAdapter.STORED_PROCEDURES)) {
LOG.warn("Database doesnt support stored procedures so ignoring the test");
return;
}
}
String procName = "DN_PROC_OUTPUTPARAM";
RDBMSStoreManager rdbmsMgr = (RDBMSStoreManager) storeMgr;
ManagedConnection mc = rdbmsMgr.getConnectionManager().getConnection(-1);
try {
Connection conn = (Connection) mc.getConnection();
Statement stmt = conn.createStatement();
// Drop it first
String dropStmt = "DROP PROCEDURE IF EXISTS " + procName;
stmt.execute(dropStmt);
// Create it
String createStmt = "CREATE PROCEDURE " + procName + "(OUT PARAM1 INT) BEGIN " + "SELECT COUNT(*) INTO PARAM1 FROM JPA_AN_PERSON; END";
stmt.execute(createStmt);
} catch (SQLException sqle) {
fail("Exception in drop-create of stored procedure : " + sqle.getMessage());
} finally {
mc.close();
}
try {
JPAEntityManager em = (JPAEntityManager) getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
em.persist(p);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
em = (JPAEntityManager) getEM();
tx = em.getTransaction();
try {
tx.begin();
// Get value to compare against
Query q = em.createQuery("SELECT COUNT(p) FROM " + Person.class.getName() + " p");
Long count = (Long) q.getSingleResult();
// Execute stored proc and compare
StoredProcedureQuery spq = em.createStoredProcedureQuery(procName);
spq.registerStoredProcedureParameter("PARAM1", Integer.class, ParameterMode.OUT);
boolean val = spq.execute();
assertFalse("Flag for result set returned true but should have been false", val);
Object paramVal = spq.getOutputParameterValue("PARAM1");
assertEquals("Output parameter is incorrect", new Integer(count.intValue()), paramVal);
tx.commit();
} catch (Exception e) {
LOG.error("Exception in test", e);
fail("Exception in test : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
// Cleanup data
clean(Person.class);
}
}
use of org.datanucleus.store.rdbms.RDBMSStoreManager in project tests by datanucleus.
the class SchemaHandlerTest method testForeignKeyRetrieval.
/**
* Test of the retrieval of FKs.
*/
public void testForeignKeyRetrieval() {
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);
// Check for the FK using the schema handler
StoreSchemaHandler handler = databaseMgr.getSchemaHandler();
Connection con = (Connection) databaseMgr.getConnectionManager().getConnection(((JDOPersistenceManager) pm).getExecutionContext()).getConnection();
RDBMSTableFKInfo fkInfo = (RDBMSTableFKInfo) handler.getSchemaData(con, "foreign-keys", new Object[] { table1 });
// Expecting single FK between SchemaClass1.other and SchemaClass2
assertEquals("Number of FKs for table " + table1 + " is wrong", 1, fkInfo.getNumberOfChildren());
// Check the FK details
ForeignKeyInfo fk = (ForeignKeyInfo) fkInfo.getChild(0);
assertEquals("FK Name is wrong", "TABLE1_FK1", ((String) fk.getProperty("fk_name")).toUpperCase());
assertEquals("PK Table Name is wrong", "SCHEMA_TABLE_2", ((String) fk.getProperty("pk_table_name")).toUpperCase());
assertEquals("FK Table Name is wrong", "SCHEMA_TABLE_1", ((String) fk.getProperty("fk_table_name")).toUpperCase());
assertEquals("PK Column Name is wrong", "TABLE2_ID", ((String) fk.getProperty("pk_column_name")).toUpperCase());
assertEquals("FK Column Name is wrong", "OTHER_ID", ((String) fk.getProperty("fk_column_name")).toUpperCase());
}
use of org.datanucleus.store.rdbms.RDBMSStoreManager 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 org.datanucleus.store.rdbms.RDBMSStoreManager 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"));
}
Aggregations