Search in sources :

Example 1 with Isnullable

use of org.datanucleus.samples.rdbms.datastore.Isnullable 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);
        }
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException) SQLException(java.sql.SQLException) JDODataStoreException(javax.jdo.JDODataStoreException) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager) JDODataStoreException(javax.jdo.JDODataStoreException) StringWriter(java.io.StringWriter) DatastoreAdapter(org.datanucleus.store.rdbms.adapter.DatastoreAdapter) AReallyObnoxiouslyLongWindedNamedObject(org.datanucleus.samples.rdbms.datastore.AReallyObnoxiouslyLongWindedNamedObject) Isnullable(org.datanucleus.samples.rdbms.datastore.Isnullable) PrintWriter(java.io.PrintWriter)

Aggregations

PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 SQLException (java.sql.SQLException)1 JDODataStoreException (javax.jdo.JDODataStoreException)1 JDOFatalUserException (javax.jdo.JDOFatalUserException)1 JDOUserException (javax.jdo.JDOUserException)1 PersistenceManager (javax.jdo.PersistenceManager)1 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)1 AReallyObnoxiouslyLongWindedNamedObject (org.datanucleus.samples.rdbms.datastore.AReallyObnoxiouslyLongWindedNamedObject)1 Isnullable (org.datanucleus.samples.rdbms.datastore.Isnullable)1 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)1 DatastoreAdapter (org.datanucleus.store.rdbms.adapter.DatastoreAdapter)1