Search in sources :

Example 1 with DateHolder

use of org.jpox.samples.types.basic.DateHolder in project tests by datanucleus.

the class TypeQueryTest method testQueryWithDateJdbcTypeChange.

/**
 * Test for persistence of a java.util.Date with a jdbc-type of "DATE" and queried against
 * an input date parameter.
 */
public void testQueryWithDateJdbcTypeChange() {
    try {
        // Persist some objects
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Calendar cal = Calendar.getInstance();
        cal.set(2005, 5, 20);
        Date cutOffDate = cal.getTime();
        try {
            tx.begin();
            DateHolder holder1 = new DateHolder();
            Calendar cal1 = Calendar.getInstance();
            cal1.set(2003, 6, 1);
            holder1.setDateField(cal1.getTime());
            DateHolder holder2 = new DateHolder();
            Calendar cal2 = Calendar.getInstance();
            cal2.set(2006, 6, 1);
            holder2.setDateField(cal2.getTime());
            pm.makePersistent(holder1);
            pm.makePersistent(holder2);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error("Exception thrown persisting object with date field", e);
            fail("Exception thrown persisting objects : " + e.getMessage());
            return;
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Query using an input parameter
        pm = pmf.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(DateHolder.class, "dateField < :myDate");
            List results = (List) q.execute(cutOffDate);
            assertEquals("Number of objects returned from Query of date is wrong", 1, results.size());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error("Exception thrown querying object using date parameter", e);
            fail("Exception thrown querying object using date parameter with changed jdbc-type : " + e.getMessage());
            return;
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(DateHolder.class);
    }
}
Also used : DateHolder(org.jpox.samples.types.basic.DateHolder) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Calendar(java.util.Calendar) List(java.util.List) Date(java.util.Date)

Example 2 with DateHolder

use of org.jpox.samples.types.basic.DateHolder in project tests by datanucleus.

the class JDOQLBasicTest method testAggregateOfDate.

/**
 * Test the use of the Date in aggregates.
 */
public void testAggregateOfDate() {
    try {
        Date maxDate = null;
        DateHolder prim1 = new DateHolder();
        Calendar cal1 = Calendar.getInstance();
        cal1.set(Calendar.DAY_OF_MONTH, 25);
        cal1.set(Calendar.MONTH, 10);
        cal1.set(Calendar.YEAR, 2000);
        cal1.set(Calendar.HOUR_OF_DAY, 10);
        cal1.set(Calendar.MINUTE, 55);
        cal1.set(Calendar.SECOND, 5);
        if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_DATASTORE_TIME_STORES_MILLISECS)) {
            cal1.set(Calendar.MILLISECOND, 0);
        }
        prim1.setDateField(cal1.getTime());
        maxDate = cal1.getTime();
        DateHolder prim2 = new DateHolder();
        Calendar cal2 = Calendar.getInstance();
        cal2.set(Calendar.DAY_OF_MONTH, 5);
        cal2.set(Calendar.MONTH, 3);
        cal2.set(Calendar.YEAR, 1982);
        cal2.set(Calendar.HOUR_OF_DAY, 4);
        cal2.set(Calendar.MINUTE, 13);
        cal2.set(Calendar.SECOND, 45);
        if (!storeMgr.getSupportedOptions().contains(StoreManager.OPTION_DATASTORE_TIME_STORES_MILLISECS)) {
            cal2.set(Calendar.MILLISECOND, 0);
        }
        prim2.setDateField(cal2.getTime());
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object id2 = null;
        try {
            tx.begin();
            pm.makePersistent(prim1);
            pm.makePersistent(prim2);
            tx.commit();
            id2 = JDOHelper.getObjectId(prim2);
            tx.begin();
            try {
                Query q = pm.newQuery("SELECT MAX(dateField) FROM " + DateHolder.class.getName());
                Date maxDateAgg = (Date) q.execute();
                assertEquals("Max date is incorrect", maxDate, maxDateAgg);
                q.closeAll();
                q = pm.newQuery("SELECT FROM " + DateHolder.class.getName() + " WHERE dateField < MAX(dateField)");
                List<DateHolder> results = (List) q.execute();
                assertEquals("Received incorrect number of results", 1, results.size());
                DateHolder prim = (DateHolder) results.iterator().next();
                assertEquals("Retrieved object is incorrect", id2, JDOHelper.getObjectId(prim));
                q.closeAll();
            } catch (JDOUserException e) {
                LOG.error("Exception during test", e);
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(DateHolder.class);
    }
}
Also used : DateHolder(org.jpox.samples.types.basic.DateHolder) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Calendar(java.util.Calendar) List(java.util.List) ArrayList(java.util.ArrayList) JDOUserException(javax.jdo.JDOUserException) Date(java.util.Date)

Example 3 with DateHolder

use of org.jpox.samples.types.basic.DateHolder in project tests by datanucleus.

the class JDOQLBasicTest method testDateTimeMethods.

/**
 * Test the use of the Date.getDay(), Date.getMonth(), Date.getYear(), Time.getHour(), Time.getMinute(), Time.getSecond() methods.
 */
public void testDateTimeMethods() {
    try {
        DateHolder prim1 = new DateHolder();
        Calendar cal1 = Calendar.getInstance();
        cal1.set(Calendar.DAY_OF_MONTH, 25);
        cal1.set(Calendar.MONTH, 10);
        cal1.set(Calendar.YEAR, 2000);
        cal1.set(Calendar.HOUR_OF_DAY, 10);
        cal1.set(Calendar.MINUTE, 55);
        cal1.set(Calendar.SECOND, 5);
        cal1.set(Calendar.MILLISECOND, 0);
        prim1.setSQLDateField(new java.sql.Date(cal1.getTimeInMillis()));
        prim1.setSQLTimeField(new java.sql.Time(cal1.getTimeInMillis()));
        DateHolder prim2 = new DateHolder();
        Calendar cal2 = Calendar.getInstance();
        cal2.set(Calendar.DAY_OF_MONTH, 5);
        cal2.set(Calendar.MONTH, 3);
        cal2.set(Calendar.YEAR, 1982);
        cal2.set(Calendar.HOUR_OF_DAY, 4);
        cal2.set(Calendar.MINUTE, 13);
        cal2.set(Calendar.SECOND, 45);
        cal2.set(Calendar.MILLISECOND, 0);
        prim2.setSQLDateField(new java.sql.Date(cal2.getTimeInMillis()));
        prim2.setSQLTimeField(new java.sql.Time(cal2.getTimeInMillis()));
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object id1 = null;
        Object id2 = null;
        try {
            tx.begin();
            pm.makePersistent(prim1);
            pm.makePersistent(prim2);
            tx.commit();
            id1 = JDOHelper.getObjectId(prim1);
            id2 = JDOHelper.getObjectId(prim2);
            tx.begin();
            try {
                // test for date methods
                Query q = pm.newQuery(pm.getExtent(DateHolder.class, false));
                q.setFilter("sqlDateField.getDay() == 25");
                List results = (List) q.execute();
                assertEquals("Received incorrect number of results to Date.getDay() == 25", 1, results.size());
                DateHolder prim = (DateHolder) results.iterator().next();
                assertEquals("Retrieved object for Date.getDay() is incorrect", id1, JDOHelper.getObjectId(prim));
                q.closeAll();
                q = pm.newQuery(pm.getExtent(DateHolder.class, false));
                q.setFilter("sqlDateField.getMonth() == 3");
                results = (List) q.execute();
                assertEquals("Received incorrect number of results to Date.getMonth() == 3", 1, results.size());
                prim = (DateHolder) results.iterator().next();
                assertEquals("Retrieved object for Date.month() is incorrect", id2, JDOHelper.getObjectId(prim));
                q.closeAll();
                q = pm.newQuery(pm.getExtent(DateHolder.class, false));
                q.setFilter("sqlDateField.getYear() == 2000");
                results = (List) q.execute();
                assertEquals("Received incorrect number of results to Date.getYear() == 2000", 1, results.size());
                prim = (DateHolder) results.iterator().next();
                assertEquals("Retrieved object for Date.year() is incorrect", id1, JDOHelper.getObjectId(prim));
                q.closeAll();
                // test for time methods
                q = pm.newQuery(pm.getExtent(DateHolder.class, false));
                q.setFilter("sqlTimeField.getHour() == 10");
                results = (List) q.execute();
                assertEquals("Received incorrect number of results to Time.getHour() == 10", 1, results.size());
                prim = (DateHolder) results.iterator().next();
                assertEquals("Retrieved object for Time.hour() is incorrect", id1, JDOHelper.getObjectId(prim));
                q.closeAll();
                q = pm.newQuery(pm.getExtent(DateHolder.class, false));
                q.setFilter("sqlTimeField.getMinute() == 13");
                results = (List) q.execute();
                assertEquals("Received incorrect number of results to Time.getMinute() == 13", 1, results.size());
                prim = (DateHolder) results.iterator().next();
                assertEquals("Retrieved object for Time.minute() is incorrect", id2, JDOHelper.getObjectId(prim));
                q.closeAll();
                q = pm.newQuery(pm.getExtent(DateHolder.class, false));
                q.setFilter("sqlTimeField.getSecond() == 5");
                results = (List) q.execute();
                assertEquals("Received incorrect number of results to Time.getSecond() == 5", 1, results.size());
                prim = (DateHolder) results.iterator().next();
                assertEquals("Retrieved object for Time.second() is incorrect", id1, JDOHelper.getObjectId(prim));
                q.closeAll();
            } catch (JDOUserException e) {
                LOG.error("Exception during test", e);
                fail(e.getMessage());
            }
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        // Clean out our data
        clean(DateHolder.class);
    }
}
Also used : DateHolder(org.jpox.samples.types.basic.DateHolder) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Calendar(java.util.Calendar) List(java.util.List) ArrayList(java.util.ArrayList) JDOUserException(javax.jdo.JDOUserException)

Aggregations

Calendar (java.util.Calendar)3 List (java.util.List)3 PersistenceManager (javax.jdo.PersistenceManager)3 Query (javax.jdo.Query)3 Transaction (javax.jdo.Transaction)3 DateHolder (org.jpox.samples.types.basic.DateHolder)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 JDOUserException (javax.jdo.JDOUserException)2