Search in sources :

Example 81 with JDOUserException

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

the class PersistenceManagerFactoryTest method testJDOHelperInstantiation.

/**
 * Test instantiating a <code>PersistenceManagerFactory</code> via
 * <code>JDOHelper.getPersistenceManagerFactory(Properties)</code>.
 */
public void testJDOHelperInstantiation() {
    /*
         * Things to consider:
         *  - Unknown properties should do nothing.
         *  - Setting Optimistic or RetainValues to true sets NontransactionalRead
         *     to true.
         *  - TransactionIsolation has valid values of Connection.TRANSACTION_*;
         *     anything else will throw an Exception.
         *  - A PersistenceManagerFactory obtained via JDOHelper should be
         *     nonconfigurable.
         */
    /*
         * 1) Test setting all propers to valid values.
         */
    boolean optimistic = true;
    boolean retainValues = true;
    boolean restoreValues = true;
    boolean ignoreCache = true;
    boolean nontransactionalRead = true;
    boolean nontransactionalWrite = false;
    boolean multithreaded = true;
    Properties dbProps = TestHelper.getPropertiesForDatastore(1);
    String driverName = dbProps.getProperty("datanucleus.ConnectionDriverName");
    String url = dbProps.getProperty("datanucleus.ConnectionURL");
    String userName = dbProps.getProperty("datanucleus.ConnectionUserName");
    String password = dbProps.getProperty("datanucleus.ConnectionPassword");
    boolean validateTables = true;
    boolean validateConstraints = true;
    boolean autoCreateTables = true;
    boolean autoCreateConstraints = true;
    int transactionIsolation = Connection.TRANSACTION_READ_COMMITTED;
    PMFProperties props = new PMFProperties();
    props.setOptimistic(optimistic);
    props.setRetainValues(retainValues);
    // This throws an exception.  Test later.
    props.setRestoreValues(restoreValues);
    props.setIgnoreCache(ignoreCache);
    props.setNontransactionalRead(nontransactionalRead);
    props.setNontransactionalWrite(nontransactionalWrite);
    props.setMultithreaded(multithreaded);
    if (driverName != null) {
        props.setDriverName(driverName);
    }
    if (userName != null) {
        props.setUserName(userName);
    }
    if (password != null) {
        props.setPassword(password);
    }
    props.setURL(url);
    // props.setFactoryName(factory1);  // Setting this uses jndi which blows up w/out
    // props.setFactory2Name(factory2); // a managed environment (ie - no JNDI).
    props.setValidateTables(validateTables);
    props.setValidateConstraints(validateConstraints);
    props.setAutoCreateTables(autoCreateTables);
    props.setAutoCreateConstraints(autoCreateConstraints);
    props.setTransactionIsolation(transactionIsolation);
    PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("PMF should be org.datanucleus.api.jdo.JDOPersistenceManagerFactory.", (pmf instanceof JDOPersistenceManagerFactory));
    assertTrue("PMF from JDOHelper doesn't match expected properties (" + props.toString() + ").", props.matchesPMF(pmf));
    pmf.close();
    // Test whether NonTransactionalWrite was correctly handled
    props.setNontransactionalWrite(true);
    try {
        pmf = JDOHelper.getPersistenceManagerFactory(props);
    } catch (Exception e) {
        fail("Setting of nontransactionalWrite shouldnt have caused an Exception but did : " + e.getMessage());
    }
    props.setNontransactionalWrite(false);
    /*
         * Test whether NonTransactionalRead was correctly set 
         */
    props.setNontransactionalRead(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
    pmf.close();
    props.setNontransactionalRead(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("NontransactionalRead should be true", pmf.getNontransactionalRead());
    pmf.close();
    props.setNontransactionalRead(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
    pmf.close();
    props.setNontransactionalRead(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("NontransactionalRead should be true", pmf.getNontransactionalRead());
    pmf.close();
    /*
         * Test whether RetainValues was correctly set 
         */
    props.setRetainValues(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("RetainValues should be false", pmf.getRetainValues());
    pmf.close();
    props.setRetainValues(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("RetainValues should be true", pmf.getRetainValues());
    pmf.close();
    props.setRetainValues(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("RetainValues should be false", pmf.getRetainValues());
    pmf.close();
    props.setRetainValues(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("RetainValues should be true", pmf.getRetainValues());
    pmf.close();
    /*
         * Test whether RestoreValues was correctly set 
         */
    props.setRestoreValues(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("RestoreValues should be false", pmf.getRestoreValues());
    pmf.close();
    props.setRestoreValues(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("RestoreValues should be true", pmf.getRestoreValues());
    pmf.close();
    props.setRestoreValues(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("RestoreValues should be false", pmf.getRestoreValues());
    pmf.close();
    props.setRestoreValues(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("RestoreValues should be true", pmf.getRestoreValues());
    pmf.close();
    /*
         * Test whether Optimistic was correctly set 
         */
    props.setOptimistic(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("Optimistic should be false", pmf.getOptimistic());
    pmf.close();
    props.setOptimistic(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("Optimistic should be true", pmf.getOptimistic());
    pmf.close();
    props.setOptimistic(false);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("Optimistic should be false", pmf.getOptimistic());
    pmf.close();
    props.setOptimistic(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("Optimistic should be true", pmf.getOptimistic());
    pmf.close();
    /*
         * Test changing one property does not change other property JDO 2 ED
         * spec 5.6 NontransactionalRead, NontransactionalWrite, Optimistic, and
         * RetainValues are independent options. A JDO implementation must not
         * automatically change the values of these properties as a side effect
         * of the user changing other properties.
         */
    props.setNontransactionalRead(false);
    props.setRetainValues(false);
    props.setOptimistic(false);
    props.setRestoreValues(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
    assertFalse("RetainValues should be false", pmf.getRetainValues());
    assertFalse("Optimistic should be false", pmf.getOptimistic());
    assertTrue("RestoreValues should be true", pmf.getRestoreValues());
    pmf.close();
    props.setRestoreValues(false);
    props.setNontransactionalRead(false);
    props.setRetainValues(false);
    props.setOptimistic(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("RestoreValues should be false", pmf.getRestoreValues());
    assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
    assertFalse("RetainValues should be false", pmf.getRetainValues());
    assertTrue("Optimistic should be true", pmf.getOptimistic());
    pmf.close();
    props.setOptimistic(false);
    props.setRestoreValues(false);
    props.setNontransactionalRead(false);
    props.setRetainValues(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("Optimistic should be false", pmf.getOptimistic());
    assertFalse("RestoreValues should be false", pmf.getRestoreValues());
    assertFalse("NontransactionalRead should be false", pmf.getNontransactionalRead());
    assertTrue("RetainValues should be true", pmf.getRetainValues());
    pmf.close();
    props.setRetainValues(false);
    props.setOptimistic(false);
    props.setRestoreValues(false);
    props.setNontransactionalRead(true);
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertFalse("RetainValues should be false", pmf.getRetainValues());
    assertFalse("Optimistic should be false", pmf.getOptimistic());
    assertFalse("RestoreValues should be false", pmf.getRestoreValues());
    assertTrue("NontransactionalRead should be true", pmf.getNontransactionalRead());
    pmf.close();
    /*
         * 2) Test that an invalid driver name throws an exception
         */
    if (vendorID != null) {
        props.setDriverName("my.test.driver.should.not.exist");
        try {
            pmf = JDOHelper.getPersistenceManagerFactory(props);
            assertTrue("Invalid driver name should have thrown exception.", false);
            pmf.close();
        } catch (Exception e) {
        /* Ignore */
        }
    }
    /*
         * 3) Test that setting an invalid transaction isolation throws an exception.
         */
    if (vendorID != null) {
        props.setDriverName(driverName);
        props.setTransactionIsolation(8738);
        try {
            pmf = JDOHelper.getPersistenceManagerFactory(props);
            assertTrue("Setting invalid transaction isolation should have thrown exception.", false);
            pmf.close();
        } catch (Exception e) {
        /* Ignore */
        }
    }
    /*
         * 4) Test that PMF is not configurable after getting an instance.
         */
    try {
        if (vendorID != null) {
            props.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        }
        pmf = JDOHelper.getPersistenceManagerFactory(props);
        assertTrue("PMF from JDOHelper doesn't match expected properties (" + props.toString() + ").", props.matchesPMF(pmf));
        pmf.setConnectionUserName("bobjones");
        assertTrue("Setting properties after frozen should throw exception.", false);
        pmf.close();
    } catch (JDOUserException e) {
    /* Ignore */
    }
    /*
         * 5) Test return data from getProperties
         */
    Properties pmf_props = pmf.getProperties();
    String vendor = pmf_props.getProperty("VendorName");
    if (vendor == null) {
        assertTrue("PMF doesnt return VendorName property", false);
    }
    String version = pmf_props.getProperty("VersionNumber");
    if (version == null) {
        assertTrue("PMF doesnt return VersionNumber property", false);
    }
    /*
         * 6) Test that setting an unknown property does nothing weird.
         */
    props.setProperty("datanucleus.MyTestUnknownProperty", "unknown");
    pmf = JDOHelper.getPersistenceManagerFactory(props);
    assertTrue("PMF from JDOHelper doesn't match expected properties (" + props.toString() + ").", props.matchesPMF(pmf));
    pmf.close();
}
Also used : PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) JDOUserException(javax.jdo.JDOUserException) JDOException(javax.jdo.JDOException) JDOUserException(javax.jdo.JDOUserException) JDOFatalUserException(javax.jdo.JDOFatalUserException)

Example 82 with JDOUserException

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

the class PersistenceManagerTest method testDeletePersistentExceptions.

/**
 * Test attempts to delete a transient object.
 */
public void testDeletePersistentExceptions() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            Person p = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]);
            // test attempt to redelete transient object
            try {
                tx.begin();
                pm.deletePersistent(p);
                fail("calling deletePersistent on transient object should fail");
            } catch (JDOUserException e) {
            } finally {
                tx.rollback();
            }
            // test attempt to delete a P-new-del
            tx.begin();
            pm.makePersistent(p);
            pm.deletePersistent(p);
            try {
                pm.deletePersistent(p);
            } catch (JDOUserException e) {
                fail("calling deletePersistent on a P-del object should work");
            }
            tx.rollback();
            // test attempt to delete a P-del
            p = createNewPerson(pm, 0);
            tx.begin();
            pm.deletePersistent(p);
            try {
                pm.deletePersistent(p);
            } catch (JDOUserException e) {
                fail("calling deletePersistent on a P-del object should work");
            }
            tx.rollback();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person) JDOUserException(javax.jdo.JDOUserException)

Example 83 with JDOUserException

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

the class PersistenceManagerTest method testGetObjectById.

/**
 * Test for getObjectById()
 * @throws Exception
 */
public void testGetObjectById() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // Test insert of native and native wrapper data types
            BigDecimal bd = new BigDecimal("12345.12345");
            BigInteger bi = new BigInteger("12345");
            java.util.Date date1 = (new java.util.GregorianCalendar()).getTime();
            java.sql.Date date2 = java.sql.Date.valueOf("2001-01-01");
            java.sql.Time time3 = java.sql.Time.valueOf("10:01:59");
            java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf("2001-01-01 23:23:23.050500000");
            tx.begin();
            Primitive p = new Primitive();
            setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", bd, bi, date1, date2, time3, timestamp);
            pm.makePersistent(p);
            tx.commit();
            Object id = pm.getObjectId(p);
            tx.begin();
            // Check the identity before and after retrieval
            Primitive p1 = (Primitive) pm.getObjectById(id);
            Object id1 = pm.getObjectId(p1);
            assertEquals(id, id1);
            boolean success = false;
            // Check the throwing of an error with an invalid id
            try {
                pm.getObjectById(new Integer(1));
            } catch (JDOObjectNotFoundException ex) {
                success = true;
            }
            assertTrue("Expected JDOObjectNotFoundException", success);
            // Check the throwing of an error with a null id
            try {
                success = false;
                pm.getObjectById(null);
            } catch (JDOUserException ex) {
                success = true;
            }
            assertTrue("Expected JDOUserException", success);
            // Check the throwing of an error with invalid id
            try {
                pm.getObjectById(new LongIdentity(Primitive.class, "111"));
                fail("Expected JDOUserException");
            } catch (JDOUserException ex) {
            // 
            }
            try {
                success = false;
                pm.getObjectById(p1);
            } catch (JDOObjectNotFoundException ex) {
                success = true;
            }
            assertTrue("Expected JDOObjectNotFoundException", success);
            tx.rollback();
            p = new Primitive();
            tx.begin();
            setPrimitiveValues(p, true, (byte) 23, 'z', 33, (short) 43, 123456789L, 123.456F, 123.456, "fixed", "normal", "huge", bd, bi, date1, date2, time3, timestamp);
            pm.makePersistent(p);
            id = pm.getObjectId(p);
            assertTrue(p == pm.getObjectById(id));
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(Primitive.class);
    }
}
Also used : JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) InversePrimitive(org.datanucleus.samples.widget.InversePrimitive) Primitive(org.datanucleus.samples.widget.Primitive) Transaction(javax.jdo.Transaction) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) BigInteger(java.math.BigInteger) LongIdentity(javax.jdo.identity.LongIdentity)

Example 84 with JDOUserException

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

the class PersistenceManagerTest method testDeletePersistent.

/**
 * Simple test of deletePersistent method.
 */
public void testDeletePersistent() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            // test deletion of transient object
            tx.begin();
            try {
                pm.deletePersistent(new Person());
                fail("Expected exception in delete persistent of non pc instances");
            } catch (JDOUserException ex) {
            // expected
            }
            // test deletion of transactional object
            Person y = new Person();
            pm.makeTransactional(y);
            try {
                pm.deletePersistent(y);
                fail("Expected exception in delete persistent of transactional instances");
            } catch (JDOUserException ex) {
            // expected
            }
            tx.commit();
            // test deletion of persistent-clean object
            Person x = createNewPerson(pm, 0);
            tx.begin();
            pm.deletePersistent(x);
            tx.commit();
            Person queryResult = queryPerson(0, pm);
            // Should be deleted
            assertNull(queryResult);
            // test deletion of persistent-new objects
            tx.begin();
            x = new Person(0, FIRSTNAME[0], LASTNAME[0], EMAIL[0]);
            pm.makePersistent(x);
            pm.deletePersistent(x);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Person(org.jpox.samples.models.company.Person) JDOUserException(javax.jdo.JDOUserException)

Example 85 with JDOUserException

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

the class JDOQLInMemoryTest method testParameterInvalid.

/**
 * Test of illegal parameter specifications.
 */
public void testParameterInvalid() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Query q = pm.newQuery(getQueryLanguage(), "SELECT FROM " + Person.class.getName());
        q.addExtension("datanucleus.query.evaluateInMemory", "true");
        q.setFilter("firstName == param1");
        q.declareParameters("java.lang.String param1, java.lang.String param1");
        q.execute("Ana");
        fail("Should have thrown exception due to invalid specification of parameters but didnt!");
        tx.commit();
    } catch (JDOUserException ue) {
    // Expected
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Aggregations

JDOUserException (javax.jdo.JDOUserException)191 PersistenceManager (javax.jdo.PersistenceManager)163 Transaction (javax.jdo.Transaction)161 Query (javax.jdo.Query)94 Iterator (java.util.Iterator)54 Collection (java.util.Collection)45 Employee (org.jpox.samples.models.company.Employee)35 List (java.util.List)33 HashSet (java.util.HashSet)30 ArrayList (java.util.ArrayList)22 Extent (javax.jdo.Extent)22 JDOException (javax.jdo.JDOException)15 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)15 CollectionHolder (org.jpox.samples.types.container.CollectionHolder)15 Map (java.util.Map)13 MapHolder (org.jpox.samples.types.container.MapHolder)13 JDOFatalUserException (javax.jdo.JDOFatalUserException)12 NucleusException (org.datanucleus.exceptions.NucleusException)10 TransactionActiveOnCloseException (org.datanucleus.exceptions.TransactionActiveOnCloseException)10 Person (org.jpox.samples.models.company.Person)10