use of org.datanucleus.store.rdbms.adapter.DatastoreAdapter in project tests by datanucleus.
the class StoredProcedureTest method testNamedProcWithParamReturningResultSet.
public void testNamedProcWithParamReturningResultSet() {
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_NAMED_RS";
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 + "(IN PARAM1 VARCHAR(255)) BEGIN " + "SELECT COUNT(*) FROM JPA_AN_PERSON WHERE FIRSTNAME = PARAM1; 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 WHERE p.firstName='Fred'");
Long count = (Long) q.getSingleResult();
// Execute stored proc and compare
StoredProcedureQuery spq = em.createNamedStoredProcedureQuery("myNamedSP");
spq.setParameter("PARAM1", "Fred");
boolean val = spq.execute();
assertTrue("Return from execute should have been true", val);
List results = spq.getResultList();
assertNotNull("ResultSet was null!", results);
assertEquals("Number of results was wrong", 1, results.size());
assertEquals("Result set result was wrong", count, results.get(0));
assertFalse("More results present but should be the end", spq.hasMoreResults());
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.adapter.DatastoreAdapter in project tests by datanucleus.
the class JDOQLSubqueryTest method testSingleStringSubqueryWithParameter.
/**
* Test a simple subquery using single-string form and a parameter in the subquery.
*/
public void testSingleStringSubqueryWithParameter() {
if (storeMgr instanceof RDBMSStoreManager) {
DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
if (!dba.supportsOption(DatastoreAdapter.ACCESS_PARENTQUERY_IN_SUBQUERY_JOINED)) {
// Access of outer query cols not supported by this datastore so dont test it
LOG.warn("Database doesnt support use of parameters with subqueries so omitting the test");
return;
}
}
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Persist Employees
Employee emp1 = new Employee(101, "Fred", "Smith", "fred.smith@company.com", 100f, "10001");
Employee emp2 = new Employee(102, "John", "Smith", "john.smith@company.com", 80f, "10002");
Employee emp3 = new Employee(103, "Jim", "Smith", "jim.smith@company.com", 80f, "10003");
Employee emp4 = new Employee(104, "Geoff", "Jones", "f2.s2@company.com", 200f, "10004");
pm.makePersistent(emp1);
pm.makePersistent(emp2);
pm.makePersistent(emp3);
pm.makePersistent(emp4);
pm.flush();
// Find the Employees earning more than the average salary of people with the same surname
Query q = pm.newQuery("SELECT FROM " + Employee.class.getName() + " WHERE salary > " + " (SELECT avg(e.salary) FROM " + Employee.class.getName() + " e " + " WHERE e.lastName == this.lastName)");
// NOTE : HSQL <= 1.8 doesnt seem to support and conditions back to the outer query
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());
// Don't commit the data
tx.rollback();
} catch (JDOUserException e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of org.datanucleus.store.rdbms.adapter.DatastoreAdapter in project tests by datanucleus.
the class SchemaTest method testIsNullable.
/**
* Test for the various column definition options inc NULL, DEFAULT etc.
*/
public void testIsNullable() {
if (!(storeMgr instanceof RDBMSStoreManager)) {
return;
}
DatastoreAdapter dba = ((RDBMSStoreManager) storeMgr).getDatastoreAdapter();
// Version where DEFAULT is after NULL/NOT NULL
StringBuffer str = new StringBuffer("CREATE TABLE ISNULLABLE\n(\n");
String createStmt1 = "ISNULLABLE_ID INT NOT NULL,\n" + "NULLMETADATANONE INT NULL,\n" + "NONNULLMETADATANONE INT NOT NULL,\n" + "NULLDFLTMETADATANONE INT NULL DEFAULT 3,\n" + "NONNULLDFLTMETADATANONE INT NOT NULL DEFAULT 4,\n" + "NULLMETADATADFLT INT NULL,\n" + "NONNULLMETADATADFLT INT NOT NULL,\n" + "NULLDFLTMETADATADFLT INT NULL DEFAULT 7,\n" + "NONNULLDFLTMETADATADFLT INT NOT NULL DEFAULT 8,\n" + "NULLMETADATAEXC INT NULL,\n" + "NONNULLMETADATAEXC INT NOT NULL,\n" + "NULLDFLTMETADATAEXC INT NULL DEFAULT 11,\n" + "NONNULLDFLTMETADATAEXC INT NOT NULL DEFAULT 12";
String createStmt2 = "ISNULLABLE_ID INT NOT NULL,\n" + "NULLMETADATANONE INT,\n" + "NONNULLMETADATANONE INT NOT NULL,\n" + "NULLDFLTMETADATANONE INT DEFAULT 3,\n" + "NONNULLDFLTMETADATANONE INT DEFAULT 4 NOT NULL,\n" + "NULLMETADATADFLT INT,\n" + "NONNULLMETADATADFLT INT NOT NULL,\n" + "NULLDFLTMETADATADFLT INT DEFAULT 7,\n" + "NONNULLDFLTMETADATA_DFLT INT DEFAULT 8 NOT NULL,\n" + "NULLMETADATAEXC INT,\n" + "NONNULLMETADATAEXC INT NOT NULL,\n" + "NULLDFLTMETADATAEXC INT DEFAULT 11,\n" + "NONNULLDFLTMETADATAEXC INT DEFAULT 12 NOT NULL";
if (dba.supportsOption(DatastoreAdapter.DEFAULT_BEFORE_NULL_IN_COLUMN_OPTIONS)) {
str.append(createStmt2);
} else {
str.append(createStmt1);
}
if (dba.supportsOption(DatastoreAdapter.PRIMARYKEY_IN_CREATE_STATEMENTS)) {
str.append(",\nCONSTRAINT ISNULL_PK PRIMARY KEY (ISNULLABLE_ID)\n");
}
str.append(")");
String createStmt = str.toString();
String alterStmt = "ALTER TABLE ISNULLABLE ADD CONSTRAINT ISNULL_PK PRIMARY KEY (ISNULLABLE_ID)";
String dropStmt = "DROP TABLE ISNULLABLE";
boolean isTableManaged = false;
try {
runStmt(createStmt);
isTableManaged = true;
// Add the primary key where we cant specify it in the CREATE TABLE
if (!dba.supportsOption(DatastoreAdapter.PRIMARYKEY_IN_CREATE_STATEMENTS)) {
runStmt(alterStmt);
}
PersistenceManager pm = pmf.getPersistenceManager();
try {
/*
*----------------------------------------------------------
*first test
*----------------------------------------------------------
* insert an object with:
* - null fields
* expected:
* JDOUserException
**/
boolean success = false;
try {
pm.currentTransaction().begin();
Isnullable isnullable = new Isnullable();
pm.makePersistent(isnullable);
pm.currentTransaction().commit();
} catch (JDOUserException dse) {
// expected
success = true;
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
} catch (Exception e) {
LOG.error(e);
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
LOG.error(stringWriter.toString());
fail("Exception thrown while trying to persist an object that shouldn't be persistable : " + e.toString());
}
if (!success) {
fail("completed an unexpected persistence of an object.");
}
/*
*----------------------------------------------------------
*second test
*----------------------------------------------------------
*
* insert an object with:
* // metadata null-value="none"
* nullableMetaDataNone = null
* nonnullMetaDataNone = null
* nullDfltMetaDataNone = null
* nonnullDfltMetaDataNone = null
*
* // medata null-value="default"
* nullableMetaDataDflt = 25
* nonnullMetaDataDflt = 26
* nullDfltMetaDataDflt = 27
* nonnullDfltMetaDataDflt = 28
*
* // metadata null-value="exception"
* nullableMetaDataExc = 29
* nonnullMetaDataExc = 30
* nullDfltMetaDataExc = 31
* nonnullDfltMetaDataExc = 32
*
* expected:
* JDODataStoreException
*
**/
success = false;
try {
pm.currentTransaction().begin();
Isnullable isnullable = new Isnullable();
isnullable.setNullMetaDataDflt(new Integer("25"));
isnullable.setNonnullMetaDataDflt(new Integer("26"));
isnullable.setNullDfltMetaDataDflt(new Integer("27"));
isnullable.setNonnullDfltMetaDataDflt(new Integer("28"));
isnullable.setNullMetaDataExc(new Integer("29"));
isnullable.setNonnullMetaDataExc(new Integer("30"));
isnullable.setNullDfltMetaDataExc(new Integer("31"));
isnullable.setNonnullDfltMetaDataExc(new Integer("32"));
pm.makePersistent(isnullable);
pm.currentTransaction().commit();
} catch (JDODataStoreException dse) {
// expected
success = true;
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
} catch (Exception e) {
LOG.error(e);
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
LOG.error(stringWriter.toString());
fail("Exception thrown while trying to persist an object that shouldn't be persistable : " + e.toString());
}
if (!success) {
fail("completed an unexpected persistence of an object.");
}
/*
*----------------------------------------------------------
*third test
*----------------------------------------------------------
*
* insert an object with:
*
* // metadata null-value="none"
* nullableMetaDataNone = 21
* nonnullMetaDataNone = 22
* nullDfltMetaDataNone = 23
* nonnullDfltMetaDataNone = 24
*
* // medata null-value="default"
* nullableMetaDataDflt = 25
* nonnullMetaDataDflt = 26
* nullDfltMetaDataDflt = 27
* nonnullDfltMetaDataDflt = 28
*
* // metadata null-value="exception"
* nullableMetaDataExc = null
* nonnullMetaDataExc = null
* nullDfltMetaDataExc = null
* nonnullDfltMetaDataExc = null
*
* expected:
* JDOUserException
*
**/
success = false;
try {
pm.currentTransaction().begin();
Isnullable isnullable = new Isnullable();
isnullable.setNullMetaDataNone(new Integer("21"));
isnullable.setNonnullMetaDataNone(new Integer("22"));
isnullable.setNullDfltMetaDataNone(new Integer("23"));
isnullable.setNonnullDfltMetaDataNone(new Integer("24"));
isnullable.setNullMetaDataDflt(new Integer("25"));
isnullable.setNonnullMetaDataDflt(new Integer("26"));
isnullable.setNullDfltMetaDataDflt(new Integer("27"));
isnullable.setNonnullDfltMetaDataDflt(new Integer("28"));
pm.makePersistent(isnullable);
pm.currentTransaction().commit();
} catch (JDOUserException uex) {
// expected
success = true;
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
} catch (Exception e) {
LOG.error(e);
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
LOG.error(stringWriter.toString());
fail("Exception thrown while trying to persist an object that shouldn't be persistable : " + e.toString());
}
if (!success) {
fail("completed an unexpected persistence of an object.");
}
/*
*----------------------------------------------------------
*fourth test
*----------------------------------------------------------
*
* insert an object with:
*
* // metadata null-value="none"
* nullableMetaDataNone = 21
* nonnullMetaDataNone = 22
* nullDfltMetaDataNone = 23
* nonnullDfltMetaDataNone = 24
*
* // medata null-value="default"
* nullableMetaDataDflt = null
* nonnullMetaDataDflt = null
* nullDfltMetaDataDflt = 27
* nonnullDfltMetaDataDflt = 28
*
* // metadata null-value="exception"
* nullableMetaDataExc = 29
* nonnullMetaDataExc = 30
* nullDfltMetaDataExc = 31
* nonnullDfltMetaDataExc = 32
*
* expected:
* JDODataStoreException
*
**/
success = false;
try {
pm.currentTransaction().begin();
Isnullable isnullable = new Isnullable();
isnullable.setNullMetaDataNone(new Integer("21"));
isnullable.setNonnullMetaDataNone(new Integer("22"));
isnullable.setNullDfltMetaDataNone(new Integer("23"));
isnullable.setNonnullDfltMetaDataNone(new Integer("24"));
isnullable.setNullDfltMetaDataDflt(new Integer("27"));
isnullable.setNonnullDfltMetaDataDflt(new Integer("28"));
isnullable.setNullMetaDataExc(new Integer("29"));
isnullable.setNonnullMetaDataExc(new Integer("30"));
isnullable.setNullDfltMetaDataExc(new Integer("31"));
isnullable.setNonnullDfltMetaDataExc(new Integer("32"));
pm.makePersistent(isnullable);
pm.currentTransaction().commit();
} catch (JDODataStoreException uex) {
// expected
success = true;
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
} catch (Exception e) {
LOG.error("Expected JDODataStoreException yet received " + e.getClass().getName(), e);
fail("Exception thrown while trying to persist an object that shouldn't be persistable " + "(expected a JDODataStoreException): " + e.toString());
}
if (!success) {
// the MySQL default. Hence this check doesnt apply there.
if (!vendorID.equals("mysql")) {
fail("completed an unexpected persistence of an object.");
}
}
/*
*----------------------------------------------------------
*fifth test
*----------------------------------------------------------
*
* insert an object with:
*
* // metadata null-value="none"
* nullableMetaDataNone = 21
* nonnullMetaDataNone = 22
* nullDfltMetaDataNone = 23
* nonnullDfltMetaDataNone = 24
*
* // medata null-value="default"
* nullableMetaDataDflt = 25
* nonnullMetaDataDflt = 26
* nullDfltMetaDataDflt = null
* nonnullDfltMetaDataDflt = null
*
* // metadata null-value="exception"
* nullableMetaDataExc = 29
* nonnullMetaDataExc = 30
* nullDfltMetaDataExc = 31
* nonnullDfltMetaDataExc = 32
*
* expected:
* object inserted
*
**/
success = false;
pm.currentTransaction().begin();
Isnullable isnullable = new Isnullable();
isnullable.setNullMetaDataNone(new Integer("21"));
isnullable.setNonnullMetaDataNone(new Integer("22"));
isnullable.setNullDfltMetaDataNone(new Integer("23"));
isnullable.setNonnullDfltMetaDataNone(new Integer("24"));
isnullable.setNullMetaDataDflt(new Integer("25"));
isnullable.setNonnullMetaDataDflt(new Integer("26"));
isnullable.setNullMetaDataExc(new Integer("29"));
isnullable.setNonnullMetaDataExc(new Integer("30"));
isnullable.setNullDfltMetaDataExc(new Integer("31"));
isnullable.setNonnullDfltMetaDataExc(new Integer("32"));
pm.makePersistent(isnullable);
pm.currentTransaction().commit();
Object id = pm.getObjectId(isnullable);
pm.currentTransaction().begin();
Isnullable toValidate = (Isnullable) pm.getObjectById(id, true);
pm.refresh(toValidate);
Isnullable expected = new Isnullable();
expected.setNullMetaDataNone(new Integer("21"));
expected.setNonnullMetaDataNone(new Integer("22"));
expected.setNullDfltMetaDataNone(new Integer("23"));
expected.setNonnullDfltMetaDataNone(new Integer("24"));
expected.setNullMetaDataDflt(new Integer("25"));
expected.setNonnullMetaDataDflt(new Integer("26"));
expected.setNullDfltMetaDataDflt(new Integer("7"));
expected.setNonnullDfltMetaDataDflt(new Integer("8"));
expected.setNullMetaDataExc(new Integer("29"));
expected.setNonnullMetaDataExc(new Integer("30"));
expected.setNullDfltMetaDataExc(new Integer("31"));
expected.setNonnullDfltMetaDataExc(new Integer("32"));
if (expected.compareTo(toValidate)) {
success = true;
} else {
assertEquals(expected, toValidate);
fail("failed to validate the object.");
}
pm.currentTransaction().commit();
/*
*----------------------------------------------------------
*sixth test
*----------------------------------------------------------
*
* insert an object with:
*
* // metadata null-value="none"
* nullableMetaDataNone = null
* nonnullMetaDataNone = 22
* nullDfltMetaDataNone = null
* nonnullDfltMetaDataNone = 24
*
* // medata null-value="default"
* nullableMetaDataDflt = 25
* nonnullMetaDataDflt = 26
* nullDfltMetaDataDflt = null
* nonnullDfltMetaDataDflt = null
*
* // metadata null-value="exception"
* nullableMetaDataExc = 29
* nonnullMetaDataExc = 30
* nullDfltMetaDataExc = 31
* nonnullDfltMetaDataExc = 32
*
* expected:
* object inserted
*
**/
success = false;
pm.currentTransaction().begin();
isnullable = new Isnullable();
isnullable.setNonnullMetaDataNone(new Integer("22"));
isnullable.setNonnullDfltMetaDataNone(new Integer("24"));
isnullable.setNullMetaDataDflt(new Integer("25"));
isnullable.setNonnullMetaDataDflt(new Integer("26"));
isnullable.setNullMetaDataExc(new Integer("29"));
isnullable.setNonnullMetaDataExc(new Integer("30"));
isnullable.setNullDfltMetaDataExc(new Integer("31"));
isnullable.setNonnullDfltMetaDataExc(new Integer("32"));
pm.makePersistent(isnullable);
pm.currentTransaction().commit();
id = pm.getObjectId(isnullable);
pm.currentTransaction().begin();
toValidate = (Isnullable) pm.getObjectById(id, true);
pm.refresh(toValidate);
expected = new Isnullable();
expected.setNonnullMetaDataNone(new Integer("22"));
expected.setNullDfltMetaDataNone(new Integer("3"));
expected.setNonnullDfltMetaDataNone(new Integer("24"));
expected.setNullMetaDataDflt(new Integer("25"));
expected.setNonnullMetaDataDflt(new Integer("26"));
expected.setNullDfltMetaDataDflt(new Integer("7"));
expected.setNonnullDfltMetaDataDflt(new Integer("8"));
expected.setNullMetaDataExc(new Integer("29"));
expected.setNonnullMetaDataExc(new Integer("30"));
expected.setNullDfltMetaDataExc(new Integer("31"));
expected.setNonnullDfltMetaDataExc(new Integer("32"));
if (expected.compareTo(toValidate)) {
success = true;
} else {
assertEquals(expected, toValidate);
fail("failed to validate the object.");
}
pm.currentTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
LOG.error(e);
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
LOG.error(stringWriter.toString());
fail(e.toString());
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
} finally {
if (isTableManaged) {
runStmt(dropStmt);
}
}
}
use of org.datanucleus.store.rdbms.adapter.DatastoreAdapter in project tests by datanucleus.
the class RDBMSAdapterFactoryTest method testGetNewDatastoreAdapter1.
/**
* datastores are identified by product name
* test unknown product
*/
public void testGetNewDatastoreAdapter1() {
DatabaseMetaData md = new DatabaseMetaData();
md.setProductName("unknown");
md.setProductVersion("1");
DatastoreAdapter adapter = factory.getNewDatastoreAdapter(clr, md, null, pluginMgr);
assertNull(adapter);
}
use of org.datanucleus.store.rdbms.adapter.DatastoreAdapter in project tests by datanucleus.
the class RDBMSAdapterFactoryTest method testGetNewDatastoreAdapter4.
/**
* datastores are identified by product name
* test jdbc driver returns null for product name
*/
public void testGetNewDatastoreAdapter4() {
DatabaseMetaData md = new DatabaseMetaData();
md.setProductName(null);
DatastoreAdapter adapter = factory.getNewDatastoreAdapter(clr, md, null, pluginMgr);
assertNull(adapter);
}
Aggregations