Search in sources :

Example 21 with Transaction

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

the class ViewTest method testNameViewWithId.

/**
 * Use of a simple view for an object, using application identity for the view objects.
 */
public void testNameViewWithId() {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            pm.makePersistent(new NameObject(1, "FIRST"));
            pm.makePersistent(new NameObject(2, "SECOND"));
            pm.makePersistent(new NameObject(3, "THIRD"));
            pm.makePersistent(new NameObject(4, "FOURTH"));
            tx.commit();
            tx.begin();
            Query q = pm.newQuery("SELECT FROM " + NameObject.class.getName() + " ORDER BY id");
            List<NameObject> persons = (List<NameObject>) q.execute();
            assertEquals(4, persons.size());
            assertEquals("FIRST", persons.get(0).getName());
            Query q1 = pm.newQuery("SELECT FROM " + FNameView2.class.getName() + " ORDER BY id");
            List<FNameView2> fNames = ((List<FNameView2>) q1.execute());
            assertEquals(2, fNames.size());
            assertEquals("FIRST", fNames.get(0).getName());
            assertEquals("FOURTH", fNames.get(1).getName());
            tx.commit();
        } catch (Throwable thr) {
            LOG.error(">> Exception thrown persist/view data with FNameView2", thr);
            fail("Failed to persist data : " + thr.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(NameObject.class);
    }
}
Also used : FNameView2(org.jpox.samples.rdbms.views.FNameView2) NameObject(org.jpox.samples.rdbms.views.NameObject) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List)

Example 22 with Transaction

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

the class ViewTest method testViewOfSetWidgets.

public void testViewOfSetWidgets() throws Exception {
    /*
         * We can't run this test on Cloudscape because the view used by
         * SetWidgetCounts doesn't execute properly; some counts that should
         * be 0 come up 1.  This is presumably due to a bug in Cloudscape
         * (last tried on both 3.6 and 4.0).
         */
    if ("cloudscape".equals(vendorID)) {
        return;
    }
    try {
        LOG.info("Testing view derived from of " + StorageTester.TEST_OBJECT_COUNT + " " + SetWidget.class.getName() + " objects");
        tester.insertObjects(SetWidget.class);
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Extent ext = pm.getExtent(SetWidgetCounts.class, true);
            Iterator exti = ext.iterator();
            int count = 0;
            while (exti.hasNext()) {
                SetWidgetCounts actual = (SetWidgetCounts) exti.next();
                SetWidgetCounts expected = new SetWidgetCounts(actual.getSetWidget());
                StorageTester.assertFieldsEqual(expected, actual);
                ++count;
            }
            tx.commit();
            assertEquals("Iteration over view extent returned wrong number of rows", StorageTester.TEST_OBJECT_COUNT, count);
            tx.begin();
            Query query = pm.newQuery(pm.getExtent(SetWidgetCounts.class, true));
            query.setFilter("normalSetSize != 0");
            query.setOrdering("sw.numElementWidgets descending");
            Collection results = (Collection) query.execute();
            TestObject[] objs = tester.getObjects();
            try {
                HashSet expected = new HashSet();
                for (int i = 0; i < objs.length; ++i) {
                    SetWidget sw = (SetWidget) objs[i];
                    if (sw.getNormalSet().size() != 0) {
                        expected.add(new SetWidgetCounts(sw));
                    }
                }
                assertTrue("Query has no expected results (test is broken)", !expected.isEmpty());
                assertTrue("Query returned no rows", !results.isEmpty());
                HashSet actual = new HashSet(results);
                assertEquals("Query returned duplicate rows", results.size(), actual.size());
                assertTrue("Query did not return expected results: expected " + expected + ", but was " + actual, TestObject.compareSet(expected, actual));
            } finally {
                query.closeAll();
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } catch (JDOException e) {
        LOG.error(">> Exception during test", e);
        fail("Exception occurred during test : " + e.getMessage());
    } finally {
        tester.removeObjects();
        clean(Widget.class);
    }
}
Also used : SetWidget(org.jpox.samples.widget.SetWidget) HashSetWidget(org.jpox.samples.widget.HashSetWidget) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) SetWidgetCounts(org.jpox.samples.rdbms.views.SetWidgetCounts) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection) HashSet(java.util.HashSet) JDOException(javax.jdo.JDOException)

Example 23 with Transaction

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

the class SQLQueryTest method testTimestampQueryOnOracle.

/**
 * Verify CORE-2976
 */
public void testTimestampQueryOnOracle() {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // run this test only on Oracle
        if (vendorID != null && vendorID.equals("oracle")) {
            tx.begin();
            Query timestampQuery = pm.newQuery(Query.SQL, "SELECT LOCALTIMESTAMP FROM DUAL");
            timestampQuery.setResultClass(java.sql.Timestamp.class);
            timestampQuery.setUnique(true);
            Timestamp result = (Timestamp) timestampQuery.execute();
            assertNotNull(result);
        }
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Timestamp(java.sql.Timestamp)

Example 24 with Transaction

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

the class SQLQueryTest method testWithoutCandidatesClassWithParameters.

/**
 * Basic test of SQL without a candidate class but with parameters.
 */
public void testWithoutCandidatesClassWithParameters() throws Exception {
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        // insert a new element for table person
        tx.begin();
        Person p = new Person(1, "Nobody", "Nobody", "nobody@jpox.org");
        pm.makePersistent(p);
        tx.commit();
        tx.begin();
        String sqlText = "SELECT count(*) FROM PERSON WHERE EMAIL_ADDRESS = ?";
        Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        List results = (List) query.execute("nobody@jpox.org");
        Iterator iter = results.iterator();
        assertEquals(1, results.size());
        while (iter.hasNext()) {
            Object obj = iter.next();
            if (obj.getClass().isArray()) {
                fail("SQL Query selecting count(*) has returned an Object[] yet should have been Object");
            }
            assertTrue("SQL Query selecting count(*) has returned an object of the wrong type : was " + obj.getClass().getName() + " but should have been Number or subclass", obj instanceof Number);
            Number value = (Number) obj;
            assertEquals("SQL Query selecting count(*) returned the wrong value : was " + value.longValue() + " but should have been 1", value.longValue(), 1);
        }
        // test more than one parameter
        sqlText = "SELECT count(*) FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
        query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        results = (List) query.execute("nobody@jpox.org", "Nobody");
        iter = results.iterator();
        assertEquals(1, results.size());
        while (iter.hasNext()) {
            Object obj = iter.next();
            if (obj.getClass().isArray()) {
                fail("SQL Query selecting count(*) has returned an Object[] yet should have been Object");
            }
            assertTrue("SQL Query selecting count(*) has returned an object of the wrong type : was " + obj.getClass().getName() + " but should have been Number or subclass", obj instanceof Number);
            Number value = (Number) obj;
            assertEquals("SQL Query selecting count(*) returned the wrong value : was " + value.longValue() + " but should have been 1", value.longValue(), 1);
        }
        // test more than one parameter
        sqlText = "SELECT count(*) FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
        query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        results = (List) query.execute("nobody@jpox.org", "Noboda");
        assertEquals(1, results.size());
        // test more than one parameter
        sqlText = "SELECT * FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
        query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        results = (List) query.execute("nobody@jpox.org", "Nobody");
        assertEquals(1, results.size());
        // test more than one parameter
        sqlText = "SELECT * FROM PERSON WHERE EMAIL_ADDRESS = ? AND FIRSTNAME = ?";
        query = pm.newQuery("javax.jdo.query.SQL", sqlText);
        results = (List) query.execute("nobody@jpox.org", "Noboda");
        assertEquals(0, results.size());
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error(e);
        fail("Exception thrown while running SQL query with parameters : " + e.getMessage());
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
        clean(Person.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Iterator(java.util.Iterator) List(java.util.List) Person(org.jpox.samples.models.company.Person) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Example 25 with Transaction

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

the class SQLQueryTest method testSelectStarQuery.

/**
 * Test of the use of SELECT * in a query.
 */
public void testSelectStarQuery() throws Exception {
    try {
        // Persist something to select
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Manager m1 = new Manager(1, "Barney", "Rubble", "barney.rubble@flintstones.com", 100, "123456");
            pm.makePersistent(m1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Do an SQL query to find the Manager
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            String sqlText = "SELECT * FROM MANAGER";
            Query query = pm.newQuery("javax.jdo.query.SQL", sqlText);
            query.setClass(Manager.class);
            List results = (List) query.execute();
            assertTrue("\"SELECT *\" query returned null, yet should have returned some results", results != null);
            assertEquals("Number of Manager objects retrieved from \"SELECT *\" query was incorrect", 1, results.size());
            Manager mgr = (Manager) results.iterator().next();
            // These will cause further SQL statements to retrieve the fields in the Person/Employee part of the object
            assertEquals("\"SELECT *\" query returned Manager with incorrect first name", "Barney", mgr.getFirstName());
            assertEquals("\"SELECT *\" query returned Manager with incorrect last name", "Rubble", mgr.getLastName());
            assertEquals("\"SELECT *\" query returned Manager with incorrect email", "barney.rubble@flintstones.com", mgr.getEmailAddress());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown from \"SELECT *\" query : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        CompanyHelper.clearCompanyData(pmf);
    }
}
Also used : Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) List(java.util.List) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) JDOUserException(javax.jdo.JDOUserException) JDODataStoreException(javax.jdo.JDODataStoreException)

Aggregations

Transaction (javax.jdo.Transaction)1552 PersistenceManager (javax.jdo.PersistenceManager)1542 Query (javax.jdo.Query)635 List (java.util.List)395 JDOUserException (javax.jdo.JDOUserException)347 Collection (java.util.Collection)287 Iterator (java.util.Iterator)216 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)183 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)156 Person (org.jpox.samples.models.company.Person)144 Employee (org.jpox.samples.models.company.Employee)122 Manager (org.jpox.samples.models.company.Manager)98 HashSet (java.util.HashSet)97 SQLException (java.sql.SQLException)80 ArrayList (java.util.ArrayList)77 JDOException (javax.jdo.JDOException)73 Extent (javax.jdo.Extent)70 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)68 JDODataStoreException (javax.jdo.JDODataStoreException)65 JDOFatalUserException (javax.jdo.JDOFatalUserException)65