Search in sources :

Example 31 with JDOUserException

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

the class AttachDetachTest method testPersistWithDetachedRelative.

/**
 * Test that persists an object, and then detaches it. Then creates an object and adds a relation
 * to the detached object and persists the new object. The related object should be connected to
 * the new object (shouldn't create a new version of the object).
 * @throws Exception Thrown if an error occurs.
 */
public void testPersistWithDetachedRelative() throws Exception {
    try {
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        Account detachedAcct = null;
        try {
            tx.begin();
            Account acct = new Account();
            acct.setEnabled(true);
            acct.setUsername("john");
            pm.makePersistent(acct);
            detachedAcct = (Account) pm.detachCopy(acct);
            tx.commit();
        } catch (JDOUserException ue) {
            LOG.error("Exception in test", ue);
            fail("Exception thrown while performing test : " + ue.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Persist the new object with the related detached
        // and detachCopy the new object
        pm = newPM();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
            woody.setAccount(detachedAcct);
            pm.makePersistent(woody);
            tx.commit();
        } catch (JDOUserException ue) {
            LOG.error("Exception during test", ue);
            fail("Exception thrown while performing test : " + ue.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Account(org.jpox.samples.models.company.Account) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException)

Example 32 with JDOUserException

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

the class AttachDetachTest method testCopyOnAttachFalseMultipleDetach.

/**
 * Test for use of CopyOnAttach=false with a 1-1 unidir relation and where we detach more than 1 of an object
 * and try to attach them in the same txn.
 */
public void testCopyOnAttachFalseMultipleDetach() {
    try {
        LoginAccount detachedAcct1 = null;
        LoginAccount detachedAcct2 = null;
        // Persist the owner object
        PersistenceManager pm = newPM();
        pm.setCopyOnAttach(false);
        pm.getFetchPlan().setGroup(FetchPlan.ALL).setMaxFetchDepth(2);
        Transaction tx = pm.currentTransaction();
        try {
            // Persist
            tx.begin();
            LoginAccount acct = new LoginAccount("George", "Bush", "bush", "iraq");
            pm.makePersistent(acct);
            // Detach 2 copies
            detachedAcct1 = (LoginAccount) pm.detachCopy(acct);
            detachedAcct2 = (LoginAccount) pm.detachCopy(acct);
            tx.commit();
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Update the detached objects
        assertEquals("Account1 object is in incorrect state", ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(detachedAcct1));
        assertEquals("Account2 object is in incorrect state", ObjectState.DETACHED_CLEAN, JDOHelper.getObjectState(detachedAcct2));
        detachedAcct1.setFirstName("George W");
        detachedAcct2.setFirstName("George B");
        // Attach the detached objects
        pm = newPM();
        pm.setCopyOnAttach(false);
        tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(detachedAcct1);
            pm.flush();
            try {
                pm.makePersistent(detachedAcct2);
                LOG.error("Attach of second version of object succeeded!");
                fail("Attach of second version of an object succeeded but should have thrown exception since CopyOnAttach=false");
            } catch (JDOUserException ue) {
                // Expected so rethrow it
                throw ue;
            }
            tx.commit();
        } catch (Exception e) {
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(LoginAccount.class);
        clean(Login.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) LoginAccount(org.jpox.samples.one_one.unidir.LoginAccount) JDOUserException(javax.jdo.JDOUserException) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 33 with JDOUserException

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

the class AttachDetachTest method testRelationManaging.

/**
 * Test checks if we can connect detached objects outside pm context
 */
public void testRelationManaging() {
    PersistenceManager pm = newPM();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        Master m = new Master();
        m.setId("Master");
        pm.makePersistent(m);
        Detail d = new Detail();
        d.setId("Detail");
        d.setMaster(m);
        Master m1 = new Master();
        m1.setId("Master1");
        pm.makePersistent(d);
        pm.makePersistent(m1);
        pm.detachCopy(m);
        Detail detachedD = (Detail) pm.detachCopy(d);
        detachedD.setMaster(m1);
        Master chM = detachedD.getMaster();
        assertEquals(chM.getId(), "Master1");
        Detail attachedD = (Detail) pm.makePersistent(detachedD);
        assertEquals(attachedD.getMaster().getId(), "Master1");
        tx.rollback();
    } catch (JDOUserException ue) {
        LOG.error("Exception in test", ue);
        fail("Exception thrown while performing test : " + ue.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Master(org.datanucleus.samples.models.hashsetcollection.Master) Transaction(javax.jdo.Transaction) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) Detail(org.datanucleus.samples.models.hashsetcollection.Detail)

Example 34 with JDOUserException

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

the class SQLQueryTest method testWithCandidateClassWithResultClass.

/**
 * Test of SQL queries with a candidate class AND a result class.
 */
public void testWithCandidateClassWithResultClass() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Create some basic data to query, and query using a ResultClass that is constructor based
        tx = pm.currentTransaction();
        tx.begin();
        Person pers1 = new Person(234568, "Homer", "Simpson", "homer.simpson@fox.com");
        pers1.setAge(45);
        Person pers2 = new Person(234578, "Marge", "Simpson", "marge.simpson@fox.com");
        pers1.setAge(42);
        pm.makePersistent(pers1);
        pm.makePersistent(pers2);
        tx.commit();
        tx = pm.currentTransaction();
        tx.begin();
        try {
            Query amountQuery = pm.newNamedQuery(Person.class, "PersonDetails");
            List results = (List) amountQuery.execute();
            Iterator resultsIter = results.iterator();
            while (resultsIter.hasNext()) {
                Object obj = resultsIter.next();
                assertEquals("ResultClass of query is incorrect.", PersonalDetails.class.getName(), obj.getClass().getName());
            }
            amountQuery.closeAll();
        } catch (JDOUserException e) {
            fail(e.getMessage());
        }
        tx.commit();
        // Create some inherited data to query and query using a ResultClass without constructor
        tx = pm.currentTransaction();
        tx.begin();
        Developer p3 = new Developer(13, "James", "Java", "james@java.com", (float) 15.00, "1234569", new Integer(3), "jdo");
        pm.makePersistent(p3);
        tx.commit();
        tx = pm.currentTransaction();
        tx.begin();
        Query inhQuery = pm.newNamedQuery(Developer.class, "DeveloperWithSkillForResult");
        List results = (List) inhQuery.execute("jdo");
        Iterator iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been DeveloperRC", obj.getClass().getName().equals(DeveloperRC.class.getName()));
            DeveloperRC p = (DeveloperRC) obj;
            assertTrue("Query with candidate class has returned the wrong Developer object.", p.getSKILL().equals("jdo"));
        }
        tx.commit();
        // Create some inherited data to query and query using INNER JOIN and users class
        tx = pm.currentTransaction();
        tx.begin();
        Developer p4 = new Developer(100, "Mike", "Microsoft", "mike@microsoft.com", 10, "1234570", new Integer(3), ".net");
        p4.setGlobalNum("GUID-p4");
        pm.makePersistent(p4);
        tx.commit();
        tx = pm.currentTransaction();
        tx.begin();
        Query inhQuery2 = pm.newNamedQuery(Developer.class, "DeveloperWithSkillUsingJoinForResult");
        results = (List) inhQuery2.execute(".net");
        iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been DeveloperRC", obj.getClass().getName().equals(DeveloperRC.class.getName()));
            DeveloperRC p = (DeveloperRC) obj;
            assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", p.getSKILL().equals(".net"));
            assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", p.getGlobalNum().equals("GUID-p4"));
            assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, p.personNum);
            assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 10, (int) p.salary);
        }
        tx.commit();
        // Create some inherited data to query and query using HashMap.
        tx = pm.currentTransaction();
        tx.begin();
        Developer p5 = new Developer();
        p5.setFirstName("John");
        p5.setSKILL("uml");
        p5.setGlobalNum("GUID-p5");
        p5.setSalary(10);
        p5.setPersonNum(100);
        p5.setSerialNo("" + new Random(System.currentTimeMillis()).nextInt());
        pm.makePersistent(p5);
        tx.commit();
        // ResultClass = java.util.HashMap
        tx = pm.currentTransaction();
        tx.begin();
        Query inhQuery3 = pm.newNamedQuery(Developer.class, "DeveloperWithSkillUsingJoinForResultHashMap");
        results = (List) inhQuery3.execute("uml");
        iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been HashMap", obj.getClass().getName().equals(HashMap.class.getName()));
            HashMap p = (HashMap) obj;
            assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", getValueForKeyInMapCaseInsensitive(p, "SKILL").equals("uml"));
            assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", getValueForKeyInMapCaseInsensitive(p, "GLOBALNUM").equals("GUID-p5"));
            Number salary = (Number) getValueForKeyInMapCaseInsensitive(p, "salary");
            assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 10, salary.intValue());
            Object personNumObj = getValueForKeyInMapCaseInsensitive(p, "PERSONNUM");
            if (personNumObj instanceof Long) {
                assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, ((Long) personNumObj).intValue());
            } else if (personNumObj instanceof BigDecimal) {
                assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, ((BigDecimal) personNumObj).intValue());
            } else {
                fail("Test doest support keys in map of type " + personNumObj.getClass());
            }
        }
        tx.commit();
        // ResultClass = java.util.Map
        tx = pm.currentTransaction();
        tx.begin();
        Query inhQuery4 = pm.newNamedQuery(Developer.class, "DeveloperWithSkillUsingJoinForResultMap");
        results = (List) inhQuery4.execute("uml");
        iter = results.iterator();
        while (iter.hasNext()) {
            Object obj = iter.next();
            assertTrue("Query with candidate class has returned the wrong type of object : was " + obj.getClass().getName() + " but should have been HashMap", obj.getClass().getName().equals(HashMap.class.getName()));
            HashMap p = (HashMap) obj;
            assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", getValueForKeyInMapCaseInsensitive(p, "SKILL").equals("uml"));
            assertTrue("Query with candidate class has returned the wrong DeveloperRC object.", getValueForKeyInMapCaseInsensitive(p, "GLOBALNUM").equals("GUID-p5"));
            Number salary = (Number) getValueForKeyInMapCaseInsensitive(p, "salary");
            assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 10, salary.intValue());
            Object personNumObj = getValueForKeyInMapCaseInsensitive(p, "PERSONNUM");
            if (personNumObj instanceof Long) {
                assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, ((Long) personNumObj).intValue());
            } else if (personNumObj instanceof BigDecimal) {
                assertEquals("Query with candidate class has returned the wrong DeveloperRC object.", 100, ((BigDecimal) personNumObj).intValue());
            } else {
                fail("Test doest support keys in map of type " + personNumObj.getClass());
            }
        }
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown while querying with candidate class and result class : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Developer.class);
        clean(Person.class);
    }
}
Also used : Query(javax.jdo.Query) DeveloperRC(org.jpox.samples.models.company.DeveloperRC) PersistenceManager(javax.jdo.PersistenceManager) HashMap(java.util.HashMap) Developer(org.jpox.samples.models.company.Developer) PersonalDetails(org.jpox.samples.models.company.PersonalDetails) JDOUserException(javax.jdo.JDOUserException) BigDecimal(java.math.BigDecimal) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException) BigInteger(java.math.BigInteger) Transaction(javax.jdo.Transaction) Random(java.util.Random) Iterator(java.util.Iterator) List(java.util.List) Person(org.jpox.samples.models.company.Person)

Example 35 with JDOUserException

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

the class SQLQueryTest method testQueryWithTimeout.

/**
 * Test of a query with a timeout.
 */
public void testQueryWithTimeout() throws Exception {
    // Try a query
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        // TODO Change this to a query that will take a LONG time and check for it
        String sqlText = "SELECT count(*) FROM PERSON";
        Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        query.addExtension("org.jpox.query.timeout", "1");
        query.execute();
        tx.commit();
    } catch (JDODataStoreException dse) {
        fail("JDODataStoreException thrown when using query timeout : " + dse.getCause().getMessage());
    } catch (JDOUserException ue) {
    // Do nothing, since this is 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) JDODataStoreException(javax.jdo.JDODataStoreException)

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