Search in sources :

Example 1 with ClassWithDefaultCols

use of org.datanucleus.samples.schema.ClassWithDefaultCols in project tests by datanucleus.

the class SchemaColumnTest method testColumnDefaultsStoringNullWhenNull.

/**
 * Test of default values for columns, storing null when a field is null at persist.
 */
public void testColumnDefaultsStoringNullWhenNull() {
    Properties props = new Properties();
    props.setProperty(RDBMSPropertyNames.PROPERTY_RDBMS_COLUMN_DEFAULT_WHEN_NULL, "false");
    PersistenceManagerFactory myPMF = getConfigurablePMF(1, props);
    try {
        PersistenceManager pm = myPMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            ClassWithDefaultCols c1 = new ClassWithDefaultCols(1);
            pm.makePersistent(c1);
            tx.commit();
        } catch (Exception ex) {
            LOG.error("Exception during test : " + ex.getMessage());
            fail("Exception thrown during test : " + ex.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        myPMF.getDataStoreCache().evictAll();
        // Retrieve and check data
        pm = myPMF.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ClassWithDefaultCols c1 = pm.getObjectById(ClassWithDefaultCols.class, 1);
            assertNull(c1.getDefaultedNameNull());
            assertNull(c1.getDefaultedName());
            assertNull(c1.getDefaultedLong());
            tx.commit();
        } catch (Exception ex) {
            LOG.error("Exception during test : " + ex.getMessage());
            fail("Exception thrown during test : " + ex.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(myPMF, ClassWithDefaultCols.class);
        myPMF.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ClassWithDefaultCols(org.datanucleus.samples.schema.ClassWithDefaultCols) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties)

Example 2 with ClassWithDefaultCols

use of org.datanucleus.samples.schema.ClassWithDefaultCols in project tests by datanucleus.

the class SchemaColumnTest method testColumnDefaultsStoringDefaultWhenNull.

/**
 * Test of default values for columns, storing the default when a field is null at persist.
 */
public void testColumnDefaultsStoringDefaultWhenNull() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            ClassWithDefaultCols c1 = new ClassWithDefaultCols(1);
            pm.makePersistent(c1);
            tx.commit();
        } catch (Exception ex) {
            LOG.error("Exception during test : " + ex.getMessage());
            fail("Exception thrown during test : " + ex.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf.getDataStoreCache().evictAll();
        // Retrieve and check data
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            ClassWithDefaultCols c1 = pm.getObjectById(ClassWithDefaultCols.class, 1);
            assertNull(c1.getDefaultedNameNull());
            assertEquals("Name 1", c1.getDefaultedName());
            assertEquals(new Long(3), c1.getDefaultedLong());
            tx.commit();
        } catch (Exception ex) {
            LOG.error("Exception during test : " + ex.getMessage());
            fail("Exception thrown during test : " + ex.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(ClassWithDefaultCols.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ClassWithDefaultCols(org.datanucleus.samples.schema.ClassWithDefaultCols)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)2 Transaction (javax.jdo.Transaction)2 ClassWithDefaultCols (org.datanucleus.samples.schema.ClassWithDefaultCols)2 Properties (java.util.Properties)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1