Search in sources :

Example 41 with PersistenceManager

use of javax.jdo.PersistenceManager 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)

Example 42 with PersistenceManager

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

the class SQLQueryTest method testQueryFromJdoqueryFile.

/**
 * Test of a query specified in a jdo query file.
 */
public void testQueryFromJdoqueryFile() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // 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();
        // Run the query (specified in a jdoquery file)
        tx.begin();
        Query query = pm.newNamedQuery(null, "DeveloperSkills");
        List results = (List) query.execute("jdo");
        Iterator resultsIter = results.iterator();
        while (resultsIter.hasNext()) {
            Object skill = resultsIter.next();
            LOG.debug(">> Skill : " + skill);
        }
    } catch (JDOUserException ue) {
        ue.printStackTrace();
        LOG.error(ue);
        fail("Exception thrown while persisting object and performing query : " + ue.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Developer.class);
    }
}
Also used : BigInteger(java.math.BigInteger) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Developer(org.jpox.samples.models.company.Developer) List(java.util.List) JDOUserException(javax.jdo.JDOUserException)

Example 43 with PersistenceManager

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

the class SQLQueryTest method testWithCandidateClassWithoutResultClass.

/**
 * Test of a query with a candidate class, without a result class.
 * @throws Exception Thrown if an error occurs
 */
public void testWithCandidateClassWithoutResultClass() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // Create some basic data to query
        tx.begin();
        Person p1 = new Person(1, "First", "Person", "first.person@jpox.org");
        pm.makePersistent(p1);
        Person p2 = new Person(2, "Second", "Person", "second.person@jpox.org");
        pm.makePersistent(p2);
        tx.commit();
        // Query for a basic object, including the PK field(s)
        tx = pm.currentTransaction();
        tx.begin();
        Query query = pm.newNamedQuery(Person.class, "PeopleWithEmail");
        List results = (List) query.execute("second.person@jpox.org");
        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 Person", obj.getClass().getName().equals(Person.class.getName()));
            Person p = (Person) obj;
            assertTrue("Query with candidate class has returned the wrong Person object.", p.getPersonNum() == 2);
            assertTrue("Query with candidate class has returned the wrong Person object.", p.getFirstName().equals("Second"));
        }
        tx.commit();
        // Create some inherited data to query
        tx = pm.currentTransaction();
        tx.begin();
        Developer p3 = new Developer(10, "James", "Java", "james@java.com", (float) 12.00, "1234567", new Integer(1), "jdo");
        pm.makePersistent(p3);
        tx.commit();
        // Query for an inherited object, including the PK field(s)
        tx = pm.currentTransaction();
        tx.begin();
        Query inhQuery = pm.newNamedQuery(Developer.class, "DeveloperWithSkill");
        results = (List) inhQuery.execute("jdo");
        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 Developer", obj.getClass().getName().equals(Developer.class.getName()));
            Developer p = (Developer) obj;
            assertTrue("Query with candidate class has returned the wrong Developer object.", p.getSKILL().equals("jdo"));
        }
        tx.commit();
        // Create some inherited data to query
        tx = pm.currentTransaction();
        tx.begin();
        Developer p4 = new Developer(11, "Paul", "Perl", "paul@perl.com", (float) 6.00, "1234568", new Integer(2), "perl");
        p4.setGlobalNum("GUID-p4");
        pm.makePersistent(p4);
        tx.commit();
        // Query for an inherited object, including the PK field(s)
        tx = pm.currentTransaction();
        tx.begin();
        Query inhQuery2 = pm.newNamedQuery(Developer.class, "DeveloperWithSkillUsingJoin");
        results = (List) inhQuery2.execute("perl");
        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 Developer", obj.getClass().getName().equals(Developer.class.getName()));
            Developer p = (Developer) obj;
            assertTrue("Query with candidate class has returned the wrong Developer object.", p.getSKILL().equals("perl"));
            assertTrue("Query with candidate class has returned the wrong Developer object.", p.getGlobalNum().equals("GUID-p4"));
        }
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown while performing SQL query using candidate class : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Developer.class);
        clean(Person.class);
    }
}
Also used : BigInteger(java.math.BigInteger) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) Developer(org.jpox.samples.models.company.Developer) List(java.util.List) Person(org.jpox.samples.models.company.Person) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 44 with PersistenceManager

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

the class NestedGroupsTest method testPersistReferences.

public void testPersistReferences() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        User u1 = new User("U1", "U1", "U1");
        pm.makePersistent(u1);
        User u2 = new User("U2", "U2", "U2");
        pm.makePersistent(u2);
        User u3 = new User("U3", "U3", "U3");
        pm.makePersistent(u3);
        Group g1 = new Group("G1");
        g1.getMembers().add(u1);
        pm.makePersistent(g1);
        Group g2 = new Group("G2");
        g2.getMembers().add(u2);
        pm.makePersistent(g2);
        Group g3 = new Group("G3");
        g3.getMembers().add(u3);
        pm.makePersistent(g3);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 45 with PersistenceManager

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

the class NestedGroupsTest method testCRUD.

public void testCRUD() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        User u1 = new User("U1", "U1", "U1");
        pm.makePersistent(u1);
        User u2 = new User("U2", "U2", "U2");
        pm.makePersistent(u2);
        User u3 = new User("U3", "U3", "U3");
        pm.makePersistent(u3);
        Group g1 = new Group("G1");
        pm.makePersistent(g1);
        Group g2 = new Group("G2");
        pm.makePersistent(g2);
        Group g3 = new Group("G3");
        pm.makePersistent(g3);
        tx.commit();
        pm.close();
        // assert no relationships
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        u1 = pm.getObjectById(User.class, "U1");
        u2 = pm.getObjectById(User.class, "U2");
        u3 = pm.getObjectById(User.class, "U3");
        g1 = pm.getObjectById(Group.class, "G1");
        g2 = pm.getObjectById(Group.class, "G2");
        g3 = pm.getObjectById(Group.class, "G3");
        assertTrue(u1.getMemberOf().isEmpty());
        assertTrue(u2.getMemberOf().isEmpty());
        assertTrue(u3.getMemberOf().isEmpty());
        assertTrue(g1.getMemberOf().isEmpty());
        assertTrue(g1.getMembers().isEmpty());
        assertTrue(g2.getMemberOf().isEmpty());
        assertTrue(g2.getMembers().isEmpty());
        assertTrue(g3.getMemberOf().isEmpty());
        assertTrue(g3.getMembers().isEmpty());
        // add some relationships
        g1.getMembers().add(u1);
        g3.getMembers().add(u3);
        g3.getMembers().add(g2);
        g2.getMembers().add(g1);
        tx.commit();
        pm.close();
        // assert new relationships
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        u1 = pm.getObjectById(User.class, "U1");
        u2 = pm.getObjectById(User.class, "U2");
        u3 = pm.getObjectById(User.class, "U3");
        g1 = pm.getObjectById(Group.class, "G1");
        g2 = pm.getObjectById(Group.class, "G2");
        g3 = pm.getObjectById(Group.class, "G3");
        assertEquals(1, u1.getMemberOf().size());
        assertEquals(0, u2.getMemberOf().size());
        assertEquals(1, u3.getMemberOf().size());
        assertEquals(1, g1.getMemberOf().size());
        assertEquals(1, g1.getMembers().size());
        assertEquals(1, g2.getMemberOf().size());
        assertTrue(g2.getMemberOf().contains(g3));
        assertEquals(1, g2.getMembers().size());
        assertTrue(g2.getMembers().contains(g1));
        assertEquals(0, g3.getMemberOf().size());
        assertEquals(2, g3.getMembers().size());
        assertTrue(g3.getMembers().contains(u3));
        assertTrue(g3.getMembers().contains(g2));
        // change relationships
        g3.getMembers().clear();
        u2.getMemberOf().add(g2);
        tx.commit();
        // assert new relationships
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        tx.begin();
        u1 = pm.getObjectById(User.class, "U1");
        u2 = pm.getObjectById(User.class, "U2");
        u3 = pm.getObjectById(User.class, "U3");
        g1 = pm.getObjectById(Group.class, "G1");
        g2 = pm.getObjectById(Group.class, "G2");
        g3 = pm.getObjectById(Group.class, "G3");
        assertEquals(1, u1.getMemberOf().size());
        assertEquals(1, u2.getMemberOf().size());
        assertEquals(0, u3.getMemberOf().size());
        assertEquals(1, g1.getMemberOf().size());
        assertEquals(1, g1.getMembers().size());
        assertEquals(0, g2.getMemberOf().size());
        assertEquals(2, g2.getMembers().size());
        assertTrue(g2.getMembers().contains(u2));
        assertTrue(g2.getMembers().contains(g1));
        assertEquals(0, g3.getMemberOf().size());
        assertEquals(0, g3.getMembers().size());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Aggregations

PersistenceManager (javax.jdo.PersistenceManager)1664 Transaction (javax.jdo.Transaction)1542 Query (javax.jdo.Query)671 List (java.util.List)397 JDOUserException (javax.jdo.JDOUserException)352 Collection (java.util.Collection)309 Iterator (java.util.Iterator)239 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)185 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)170 Person (org.jpox.samples.models.company.Person)146 Employee (org.jpox.samples.models.company.Employee)128 Manager (org.jpox.samples.models.company.Manager)107 HashSet (java.util.HashSet)101 ArrayList (java.util.ArrayList)85 SQLException (java.sql.SQLException)82 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)77 JDOException (javax.jdo.JDOException)74 Extent (javax.jdo.Extent)72 JDOFatalUserException (javax.jdo.JDOFatalUserException)68 JDODataStoreException (javax.jdo.JDODataStoreException)65