Search in sources :

Example 1 with JDOFatalUserException

use of javax.jdo.JDOFatalUserException in project tests by datanucleus.

the class SchemaTest method testColumnWidth.

/**
 * Test of the column width specification.
 * Test the PMF property "datanucleus.rdbms.stringLengthExceededAction".
 */
public void testColumnWidth() {
    try {
        // 1). Persist an object with a "serialNo" too long for the column, and expect an Exception
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee(245, "Fred", "Flintstone", "fred.flintstone@warnerbros.com", (float) 178.90, "123456789012345");
            pm.makePersistent(e);
            tx.commit();
            fail("Persisted an object with a field value that was too long for the column storing it!");
        } catch (JDOFatalUserException e) {
        // Expected this to be thrown
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // 2). Persist an object with a "serialNo" too long for the column, and use PMF option to truncate
        Properties userProps = new Properties();
        userProps.setProperty(RDBMSPropertyNames.PROPERTY_RDBMS_STRING_LENGTH_EXCEEDED_ACTION, "TRUNCATE");
        PersistenceManagerFactory pmf2 = getPMF(1, userProps);
        pm = pmf2.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee(245, "Fred", "Flintstone", "fred.flintstone@warnerbros.com", (float) 178.90, "123456789012345");
            pm.makePersistent(e);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown when persisting object with too-long String field but truncate selected");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf2.close();
    } finally {
        clean(Employee.class);
    }
}
Also used : JDOFatalUserException(javax.jdo.JDOFatalUserException) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException) SQLException(java.sql.SQLException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 2 with JDOFatalUserException

use of javax.jdo.JDOFatalUserException in project tests by datanucleus.

the class PersistenceManagerFactoryTest method testObjectForStringProperty.

/**
 * Test setting an object for a property value but string was expected
 */
public void testObjectForStringProperty() {
    Properties props = TestHelper.getPropertiesForDatastore(1);
    Map<Object, Object> map = new HashMap<>();
    map.putAll(props);
    map.put("javax.jdo.option.Mapping", new Object());
    try {
        PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(map);
        try {
            fail("Expected JDOFatalInternalException");
        } finally {
            pmf.close();
        }
    } catch (JDOFatalUserException ex) {
        assertEquals(IllegalArgumentException.class, ex.getNestedExceptions()[0].getCause().getClass());
    } catch (Exception e) {
        fail("Incorrect exception thrown : expected JDOFatalUserException but got " + e.getClass().getName());
    }
}
Also used : JDOFatalUserException(javax.jdo.JDOFatalUserException) HashMap(java.util.HashMap) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException)

Example 3 with JDOFatalUserException

use of javax.jdo.JDOFatalUserException in project tests by datanucleus.

the class PersistenceManagerFactoryTest method testServerTimeZoneID.

/**
 * Test for serverTimeZoneID setting.
 */
public void testServerTimeZoneID() {
    PersistenceManagerFactory pmf = null;
    // Try setting the serverTimeZoneID to an invalid value
    try {
        Properties userProps = new Properties();
        userProps.setProperty("javax.jdo.option.ServerTimeZoneID", "JPOX_CENTRAL_TIMEZONE");
        pmf = getPMF(1, userProps);
        fail("Expected a JDOUserException when setting the ServerTimeZoneID to an invalid value but worked!");
    } catch (JDOFatalUserException jdoe) {
    // Expected
    } finally {
        if (pmf != null && !pmf.isClosed()) {
            pmf.close();
        }
    }
    // Try setting it to a valid value and make sure it is retained
    try {
        Properties userProps = new Properties();
        userProps.setProperty("javax.jdo.option.ServerTimeZoneID", "UTC");
        pmf = getPMF(1, userProps);
        assertEquals("ServerTimeZoneID was not set correctly", ((JDOPersistenceManagerFactory) pmf).getServerTimeZoneID(), "UTC");
    } catch (Exception e) {
        fail("Exception thrown when setting the ServerTimeZoneID to UTC");
    } finally {
        if (pmf != null && !pmf.isClosed()) {
            pmf.close();
        }
    }
    // Try leaving it unset and check the value
    try {
        pmf = getPMF(1, null);
        assertEquals("ServerTimeZoneID was not set correctly", ((JDOPersistenceManagerFactory) pmf).getServerTimeZoneID(), null);
    } catch (Exception e) {
        fail("Exception thrown when setting the ServerTimeZoneID to UTC");
    } finally {
        if (pmf != null && !pmf.isClosed()) {
            pmf.close();
        }
    }
}
Also used : JDOFatalUserException(javax.jdo.JDOFatalUserException) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException)

Example 4 with JDOFatalUserException

use of javax.jdo.JDOFatalUserException in project tests by datanucleus.

the class SchemaTest method testColumnWidth.

/**
 * Test of the column width specification.
 * Test the PMF property "org.jpox.rdbms.stringLengthExceededAction".
 */
public void testColumnWidth() {
    try {
        // 1). Persist an object with a "serialNo" too long for the column, and expect an Exception
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee(245, "Fred", "Flintstone", "fred.flintstone@warnerbros.com", (float) 178.90, "123456789012345");
            pm.makePersistent(e);
            tx.commit();
            fail("Persisted an object with a field value that was too long for the column storing it!");
        } catch (JDOFatalUserException e) {
        // Expected this to be thrown
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // 2). Persist an object with a "serialNo" too long for the column, and use PMF option to truncate
        Properties userProps = new Properties();
        userProps.setProperty(RDBMSPropertyNames.PROPERTY_RDBMS_STRING_LENGTH_EXCEEDED_ACTION, "TRUNCATE");
        PersistenceManagerFactory pmf2 = getPMF(1, userProps);
        pm = pmf2.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee e = new Employee(245, "Fred", "Flintstone", "fred.flintstone@warnerbros.com", (float) 178.90, "123456789012345");
            pm.makePersistent(e);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown when persisting object with too-long String field but truncate selected");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        pmf2.close();
    } finally {
        clean(Employee.class);
    }
}
Also used : JDOFatalUserException(javax.jdo.JDOFatalUserException) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) Properties(java.util.Properties) JDOFatalUserException(javax.jdo.JDOFatalUserException) JDOFatalInternalException(javax.jdo.JDOFatalInternalException) SQLException(java.sql.SQLException)

Aggregations

Properties (java.util.Properties)4 JDOFatalUserException (javax.jdo.JDOFatalUserException)4 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)4 JDOUserException (javax.jdo.JDOUserException)3 SQLException (java.sql.SQLException)2 JDOException (javax.jdo.JDOException)2 PersistenceManager (javax.jdo.PersistenceManager)2 Transaction (javax.jdo.Transaction)2 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)2 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)2 Employee (org.jpox.samples.models.company.Employee)2 HashMap (java.util.HashMap)1 JDODataStoreException (javax.jdo.JDODataStoreException)1 JDOFatalInternalException (javax.jdo.JDOFatalInternalException)1