Search in sources :

Example 31 with PersistenceManager

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

the class TypesMappingTest method testDB2DataLinkType.

/**
 * Test for DB2 datastore "DATALINK" type.
 */
public void testDB2DataLinkType() {
    if (!vendorID.equals("db2")) {
        return;
    }
    DB2Types types;
    Object id = null;
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();
        types = new DB2Types();
        types.setDataLinkString("http://www.jpox.org");
        types.setDataLinkString2("http://www.someurl.org/path");
        types.setSimpleString("some string");
        pm.makePersistent(types);
        id = JDOHelper.getObjectId(types);
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        types = (DB2Types) pm.getObjectById(id, true);
        assertEquals("DataLinkString retrieved is wrong", "http://www.jpox.org".toUpperCase(), types.getDataLinkString().toUpperCase());
        assertEquals("DataLinkString2 retrieved is wrong", "/path".toUpperCase(), types.getDataLinkString2().toUpperCase());
        assertEquals("Simple String retrieved is wrong", "some string", types.getSimpleString());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
    pm = pmf.getPersistenceManager();
    tx = pm.currentTransaction();
    try {
        tx.begin();
        types = (DB2Types) ((Collection) pm.newQuery(DB2Types.class).execute()).iterator().next();
        assertEquals("DataLinkString retrieved from Query is wrong", "http://www.jpox.org".toUpperCase(), types.getDataLinkString().toUpperCase());
        assertEquals("DataLinkString2 retrieved from Query is wrong", "/path".toUpperCase(), types.getDataLinkString2().toUpperCase());
        assertEquals("Simple String retrieved from Query is wrong", "some string", types.getSimpleString());
        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        pm.close();
    }
}
Also used : DB2Types(org.jpox.samples.rdbms.types.DB2Types) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager)

Example 32 with PersistenceManager

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

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

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

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

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