use of org.datanucleus.samples.types.javatime.JavaTimeSample1 in project tests by datanucleus.
the class JavaTimeTest method testDateTimeMethods.
/**
* Test the use of the Date.getDay(), Date.getMonth(), Date.getYear(), Time.getHour(), Time.getMinute(), Time.getSecond() methods.
*/
public void testDateTimeMethods() {
try {
LocalDateTime dateTime1 = LocalDateTime.of(2008, 3, 17, 15, 9, 0, 0);
LocalDateTime dateTime2 = LocalDateTime.of(2009, 5, 13, 7, 9, 26, 0);
JavaTimeSample1 s1a = new JavaTimeSample1(1, dateTime1, dateTime2);
LocalDateTime dateTime3 = LocalDateTime.of(2011, 10, 14, 1, 0, 15, 0);
LocalDateTime dateTime4 = LocalDateTime.of(2012, 11, 1, 7, 9, 0, 0);
JavaTimeSample1 s1b = new JavaTimeSample1(2, dateTime3, dateTime4);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
Object id1 = null;
Object id2 = null;
try {
tx.begin();
pm.makePersistent(s1a);
pm.makePersistent(s1b);
tx.commit();
id1 = JDOHelper.getObjectId(s1a);
id2 = JDOHelper.getObjectId(s1b);
tx.begin();
try {
Query q = pm.newQuery(JavaTimeSample1.class);
q.setFilter("dateTime1.getDayOfMonth() == 17");
List results = (List) q.execute();
assertEquals("Received incorrect number of results for LocalDateTime.getDayOfMonth()", 1, results.size());
JavaTimeSample1 first = (JavaTimeSample1) results.iterator().next();
assertEquals("Retrieved object for getDayOfMonth() is incorrect", id1, JDOHelper.getObjectId(first));
q.closeAll();
q = pm.newQuery(JavaTimeSample1.class);
q.setFilter("dateTime1.getMonthValue() == 10");
results = (List) q.execute();
assertEquals("Received incorrect number of results for LocalDateTime.getMonthValue", 1, results.size());
first = (JavaTimeSample1) results.iterator().next();
assertEquals("Retrieved object for getMonth() is incorrect", id2, JDOHelper.getObjectId(first));
q.closeAll();
q = pm.newQuery(JavaTimeSample1.class);
q.setFilter("dateTime1.getYear() == 2008");
results = (List) q.execute();
assertEquals("Received incorrect number of results for LocalDateTime.getYear", 1, results.size());
first = (JavaTimeSample1) results.iterator().next();
assertEquals("Retrieved object for getYear() is incorrect", id1, JDOHelper.getObjectId(first));
q.closeAll();
// TODO sample can be persisted with timezone info resulting in H2 returning incorrect value for the test
/*q = pm.newQuery(JavaTimeSample1.class);
q.setFilter("dateTime1.getHour() == 1");
results = (List)q.execute();
assertEquals("Received incorrect number of results for LocalDateTime.getHour", 1, results.size());
first = (JavaTimeSample1)results.iterator().next();
assertEquals("Retrieved object for getHour() is incorrect", id2, JDOHelper.getObjectId(first));
q.closeAll();*/
q = pm.newQuery(JavaTimeSample1.class);
q.setFilter("dateTime1.getMinute() == 9");
results = (List) q.execute();
assertEquals("Received incorrect number of results for LocalDateTime.getMinute", 1, results.size());
first = (JavaTimeSample1) results.iterator().next();
assertEquals("Retrieved object for getMinute() is incorrect", id1, JDOHelper.getObjectId(first));
q.closeAll();
q = pm.newQuery(JavaTimeSample1.class);
q.setFilter("dateTime1.getSecond() == 0");
results = (List) q.execute();
assertEquals("Received incorrect number of results for LocalDateTime.getSecond", 1, results.size());
first = (JavaTimeSample1) results.iterator().next();
assertEquals("Retrieved object for getSecond() is incorrect", id1, JDOHelper.getObjectId(first));
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(JavaTimeSample1.class);
}
}
use of org.datanucleus.samples.types.javatime.JavaTimeSample1 in project tests by datanucleus.
the class JavaTimeTest method testLocalDateTime.
/**
* Test for DateTime persistence and retrieval.
*/
public void testLocalDateTime() {
try {
// Create some data we can use for access
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
LocalDateTime dateTime1 = LocalDateTime.of(2008, 3, 14, 15, 9, 26, 0);
LocalDateTime dateTime2 = LocalDateTime.of(2009, 5, 13, 7, 9, 26, 0);
Object id = null;
try {
tx.begin();
JavaTimeSample1 s = new JavaTimeSample1(1, dateTime1, dateTime2);
pm.makePersistent(s);
tx.commit();
id = pm.getObjectId(s);
} catch (Exception e) {
LOG.error("Error persisting DateTime sample", e);
fail("Error persisting DateTime sample");
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Retrieve the data
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
JavaTimeSample1 s = (JavaTimeSample1) pm.getObjectById(id);
LocalDateTime dt1 = s.getDateTime1();
assertNotNull("Retrieved DateTime was null!", dt1);
assertEquals("Timestamp : Year was wrong", 2008, dt1.getYear());
assertEquals("Timestamp : Month was wrong", Month.MARCH, dt1.getMonth());
assertEquals("Timestamp : Day was wrong", 14, dt1.getDayOfMonth());
assertEquals("Timestamp : Hour was wrong", 15, dt1.getHour());
assertEquals("Timestamp : Minute was wrong", 9, dt1.getMinute());
assertEquals("Timestamp : Second was wrong", 26, dt1.getSecond());
LocalDateTime dt2 = s.getDateTime2();
assertNotNull("Retrieved DateTime was null!", dt2);
assertEquals("String : Year was wrong", 2009, dt2.getYear());
assertEquals("String : Month was wrong", Month.MAY, dt2.getMonth());
assertEquals("String : Day was wrong", 13, dt2.getDayOfMonth());
assertEquals("String : Hour was wrong", 7, dt2.getHour());
assertEquals("String : Minute was wrong", 9, dt2.getMinute());
assertEquals("String : Second was wrong", 26, dt2.getSecond());
tx.commit();
} catch (Exception e) {
LOG.error("Error retrieving DateTime data", e);
fail("Error retrieving DateTime data : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Query the data
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
{
Query<JavaTimeSample1> q = pm.newQuery(JavaTimeSample1.class, "dateTime1.getYear() == 2008");
List<JavaTimeSample1> results = q.executeList();
assertNotNull(results);
assertEquals(1, results.size());
JavaTimeSample1 s = results.iterator().next();
LocalDateTime dt = s.getDateTime1();
assertNotNull(dt);
assertEquals("Timestamp : Year was wrong", 2008, dt.getYear());
assertEquals("Timestamp : Month was wrong", Month.MARCH, dt.getMonth());
assertEquals("Timestamp : Day was wrong", 14, dt.getDayOfMonth());
assertEquals("Timestamp : Hour was wrong", 15, dt.getHour());
assertEquals("Timestamp : Minute was wrong", 9, dt.getMinute());
assertEquals("Timestamp : Second was wrong", 26, dt.getSecond());
}
{
Query<JavaTimeSample1> q = pm.newQuery(JavaTimeSample1.class, "dateTime1.getMonthValue() == 3");
List<JavaTimeSample1> results = q.executeList();
assertNotNull(results);
assertEquals(1, results.size());
JavaTimeSample1 s = results.iterator().next();
LocalDateTime dt = s.getDateTime1();
assertNotNull(dt);
assertEquals("Timestamp : Year was wrong", 2008, dt.getYear());
assertEquals("Timestamp : Month was wrong", Month.MARCH, dt.getMonth());
assertEquals("Timestamp : Day was wrong", 14, dt.getDayOfMonth());
assertEquals("Timestamp : Hour was wrong", 15, dt.getHour());
assertEquals("Timestamp : Minute was wrong", 9, dt.getMinute());
assertEquals("Timestamp : Second was wrong", 26, dt.getSecond());
}
{
Query<JavaTimeSample1> q = pm.newQuery(JavaTimeSample1.class, "dateTime1.getDayOfMonth() == 14");
List<JavaTimeSample1> results = q.executeList();
assertNotNull(results);
assertEquals(1, results.size());
JavaTimeSample1 s = results.iterator().next();
LocalDateTime dt = s.getDateTime1();
assertNotNull(dt);
assertEquals("Timestamp : Year was wrong", 2008, dt.getYear());
assertEquals("Timestamp : Month was wrong", Month.MARCH, dt.getMonth());
assertEquals("Timestamp : Day was wrong", 14, dt.getDayOfMonth());
assertEquals("Timestamp : Hour was wrong", 15, dt.getHour());
assertEquals("Timestamp : Minute was wrong", 9, dt.getMinute());
assertEquals("Timestamp : Second was wrong", 26, dt.getSecond());
}
{
Query<JavaTimeSample1> q = pm.newQuery(JavaTimeSample1.class, "dateTime1.getHour() == 15");
List<JavaTimeSample1> results = q.executeList();
assertNotNull(results);
assertEquals(1, results.size());
JavaTimeSample1 s = results.iterator().next();
LocalDateTime dt = s.getDateTime1();
assertNotNull(dt);
assertEquals("Timestamp : Year was wrong", 2008, dt.getYear());
assertEquals("Timestamp : Month was wrong", Month.MARCH, dt.getMonth());
assertEquals("Timestamp : Day was wrong", 14, dt.getDayOfMonth());
assertEquals("Timestamp : Hour was wrong", 15, dt.getHour());
assertEquals("Timestamp : Minute was wrong", 9, dt.getMinute());
assertEquals("Timestamp : Second was wrong", 26, dt.getSecond());
}
{
Query<JavaTimeSample1> q = pm.newQuery(JavaTimeSample1.class, "dateTime1.getMinute() == 9");
List<JavaTimeSample1> results = q.executeList();
assertNotNull(results);
assertEquals(1, results.size());
JavaTimeSample1 s = results.iterator().next();
LocalDateTime dt = s.getDateTime1();
assertNotNull(dt);
assertEquals("Timestamp : Year was wrong", 2008, dt.getYear());
assertEquals("Timestamp : Month was wrong", Month.MARCH, dt.getMonth());
assertEquals("Timestamp : Day was wrong", 14, dt.getDayOfMonth());
assertEquals("Timestamp : Hour was wrong", 15, dt.getHour());
assertEquals("Timestamp : Minute was wrong", 9, dt.getMinute());
assertEquals("Timestamp : Second was wrong", 26, dt.getSecond());
}
{
Query<JavaTimeSample1> q = pm.newQuery(JavaTimeSample1.class, "dateTime1.getSecond() == 26");
List<JavaTimeSample1> results = q.executeList();
assertNotNull(results);
assertEquals(1, results.size());
JavaTimeSample1 s = results.iterator().next();
LocalDateTime dt = s.getDateTime1();
assertNotNull(dt);
assertEquals("Timestamp : Year was wrong", 2008, dt.getYear());
assertEquals("Timestamp : Month was wrong", Month.MARCH, dt.getMonth());
assertEquals("Timestamp : Day was wrong", 14, dt.getDayOfMonth());
assertEquals("Timestamp : Hour was wrong", 15, dt.getHour());
assertEquals("Timestamp : Minute was wrong", 9, dt.getMinute());
assertEquals("Timestamp : Second was wrong", 26, dt.getSecond());
}
tx.commit();
} catch (Exception e) {
LOG.error("Error querying LocalDateTime", e);
fail("Error querying LocalDateTime : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(JavaTimeSample1.class);
}
}
use of org.datanucleus.samples.types.javatime.JavaTimeSample1 in project tests by datanucleus.
the class JavaTimeTest method testDateTime.
/**
* Test for DateTime persistence/retrieval.
*/
public void testDateTime() {
try {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
LocalDateTime dateTime1 = LocalDateTime.of(2008, 3, 14, 15, 9, 26, 0);
LocalDateTime dateTime2 = LocalDateTime.of(2009, 5, 13, 7, 9, 26, 0);
try {
tx.begin();
JavaTimeSample1 s = new JavaTimeSample1(1, dateTime1, dateTime2);
JavaTimeSample1 s2 = new JavaTimeSample1(2, null, null);
em.persist(s);
em.persist(s2);
tx.commit();
} catch (Exception e) {
LOG.error("Error persisting DateTime sample", e);
fail("Error persisting DateTime sample");
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Retrieve the data
em = emf.createEntityManager();
tx = em.getTransaction();
try {
tx.begin();
JavaTimeSample1 s = em.find(JavaTimeSample1.class, 1);
LocalDateTime dt1 = s.getDateTime1();
assertNotNull("Retrieved DateTime was null!", dt1);
assertEquals("Timestamp : Year was wrong", 2008, dt1.getYear());
assertEquals("Timestamp : Month was wrong", Month.MARCH, dt1.getMonth());
assertEquals("Timestamp : Day was wrong", 14, dt1.getDayOfMonth());
assertEquals("Timestamp : Hour was wrong", 15, dt1.getHour());
assertEquals("Timestamp : Minute was wrong", 9, dt1.getMinute());
assertEquals("Timestamp : Second was wrong", 26, dt1.getSecond());
LocalDateTime dt2 = s.getDateTime2();
assertNotNull("Retrieved DateTime was null!", dt2);
assertEquals("String : Year was wrong", 2009, dt2.getYear());
assertEquals("String : Month was wrong", Month.MAY, dt2.getMonth());
assertEquals("String : Day was wrong", 13, dt2.getDayOfMonth());
assertEquals("String : Hour was wrong", 7, dt2.getHour());
assertEquals("String : Minute was wrong", 9, dt2.getMinute());
assertEquals("String : Second was wrong", 26, dt2.getSecond());
JavaTimeSample1 s2 = em.find(JavaTimeSample1.class, 2);
assertNull(s2.getDateTime1());
assertNull(s2.getDateTime2());
tx.commit();
} catch (Exception e) {
LOG.error("Error retrieving DateTime data", e);
fail("Error retrieving DateTime data : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
// Query the data
em = emf.createEntityManager();
tx = em.getTransaction();
try {
tx.begin();
// Try query with a LocalDateTime parameter
Query q = em.createQuery("SELECT s FROM " + JavaTimeSample1.class.getName() + " s WHERE s.dateTime1 IS NOT NULL AND s.dateTime1 < :param");
q.setParameter("param", dateTime2);
List<JavaTimeSample1> results = q.getResultList();
assertEquals(1, results.size());
tx.commit();
} catch (Exception e) {
LOG.error("Error retrieving DateTime data", e);
fail("Error retrieving DateTime data : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} finally {
clean(JavaTimeSample1.class);
}
}
Aggregations