use of javax.jdo.JDODataStoreException in project tests by datanucleus.
the class RecursiveTest method testLoop.
public void testLoop() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Department dep1 = new Department("Dep-1", null);
Department dep2 = new Department("Dep-2", null);
dep1.setSuperDepartment(dep2);
dep2.setSuperDepartment(dep1);
try {
pm.makePersistent(dep1);
pm.makePersistent(dep2);
tx.commit();
fail("Should fail, recursive loop in hierarchical mapping.");
} catch (JDODataStoreException e) {
// expected
}
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.JDODataStoreException in project tests by datanucleus.
the class SQLQueryTest method testInvalidQuery.
/**
* Test of an invalid query. All queries should start "SELECT".
*/
public void testInvalidQuery() throws Exception {
// Try an invalid query
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
String sqlText = "INSERT INTO PERSON VALUES(1,2)";
Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
query.execute();
fail("Should have thrown an exception on an invalid (non-SELECT) query, but allowed execution");
} catch (JDOUserException ue) {
// Do nothing, since this is expected
} catch (JDODataStoreException dse) {
// Do nothing, since this is expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.JDODataStoreException 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 javax.jdo.JDODataStoreException in project tests by datanucleus.
the class SQLQueryTest method testInvalidQuery.
/**
* Test of an invalid query. All queries should start "SELECT".
*/
public void testInvalidQuery() throws Exception {
// Try an invalid query
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
String sqlText = "INSERT INTO PERSON VALUES(1,2)";
Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
query.execute();
fail("Should have thrown an exception on an invalid (non-SELECT) query, but allowed execution");
} catch (JDOUserException ue) {
// Do nothing, since this is expected
} catch (JDODataStoreException dse) {
// Do nothing, since this is expected
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
use of javax.jdo.JDODataStoreException in project hive by apache.
the class ObjectStore method getMSchemaVersion.
private MVersionTable getMSchemaVersion() throws NoSuchObjectException, MetaException {
boolean committed = false;
Query query = null;
List<MVersionTable> mVerTables = new ArrayList<>();
try {
openTransaction();
query = pm.newQuery(MVersionTable.class);
try {
mVerTables = (List<MVersionTable>) query.execute();
pm.retrieveAll(mVerTables);
} catch (JDODataStoreException e) {
if (e.getCause() instanceof MissingTableException) {
throw new MetaException("Version table not found. " + "The metastore is not upgraded to " + MetaStoreSchemaInfoFactory.get(getConf()).getHiveSchemaVersion());
} else {
throw e;
}
}
committed = commitTransaction();
if (mVerTables.isEmpty()) {
throw new NoSuchObjectException("No matching version found");
}
if (mVerTables.size() > 1) {
String msg = "Metastore contains multiple versions (" + mVerTables.size() + ") ";
for (MVersionTable version : mVerTables) {
msg += "[ version = " + version.getSchemaVersion() + ", comment = " + version.getVersionComment() + " ] ";
}
throw new MetaException(msg.trim());
}
return mVerTables.get(0);
} finally {
rollbackAndCleanup(committed, query);
}
}
Aggregations