Search in sources :

Example 11 with DateTestEntity

use of org.apache.cayenne.testdo.date_time.DateTestEntity in project cayenne by apache.

the class DataContextEJBQLDateTimeFunctionalExpressionsIT method testCURRENT_TIME.

@Test
public void testCURRENT_TIME() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    DateTestEntity o1 = context.newObject(DateTestEntity.class);
    cal.set(year, month, day, 0, 0, 0);
    o1.setTimeColumn(cal.getTime());
    DateTestEntity o2 = context.newObject(DateTestEntity.class);
    cal.set(year, month, day, 23, 59, 59);
    o2.setTimeColumn(cal.getTime());
    context.commitChanges();
    EJBQLQuery query = new EJBQLQuery("SELECT d FROM DateTestEntity d WHERE d.timeColumn < CURRENT_TIME");
    List<?> objects = context.performQuery(query);
    if (!unitDbAdapter.supportsTimeSqlType()) {
        // result will be invalid most likely as DB doesn't support TIME data type
        return;
    }
    assertEquals(1, objects.size());
    assertTrue(objects.contains(o1));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Calendar(java.util.Calendar) DateTestEntity(org.apache.cayenne.testdo.date_time.DateTestEntity) Test(org.junit.Test)

Example 12 with DateTestEntity

use of org.apache.cayenne.testdo.date_time.DateTestEntity in project cayenne by apache.

the class DataContextEJBQLDateTimeFunctionalExpressionsIT method testCURRENT_TIMESTAMP.

@Test
public void testCURRENT_TIMESTAMP() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int date = cal.get(Calendar.DATE);
    DateTestEntity o1 = context.newObject(DateTestEntity.class);
    cal.set(year, month, date, 0, 0, 0);
    o1.setTimestampColumn(cal.getTime());
    DateTestEntity o2 = context.newObject(DateTestEntity.class);
    cal.set(year, month, date, 23, 59, 59);
    o2.setTimestampColumn(cal.getTime());
    context.commitChanges();
    EJBQLQuery query = new EJBQLQuery("SELECT d FROM DateTestEntity d WHERE d.timestampColumn < CURRENT_TIMESTAMP");
    List<?> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    assertTrue(objects.contains(o1));
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) Calendar(java.util.Calendar) DateTestEntity(org.apache.cayenne.testdo.date_time.DateTestEntity) Test(org.junit.Test)

Example 13 with DateTestEntity

use of org.apache.cayenne.testdo.date_time.DateTestEntity in project cayenne by apache.

the class DateTimeTypesIT method testSQLTemplateTimestamp.

@Test
public void testSQLTemplateTimestamp() throws Exception {
    DateTestEntity test = context.newObject(DateTestEntity.class);
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(2003, 1, 1, 1, 20, 30);
    // most databases fail millisecond accuracy
    // cal.set(Calendar.MILLISECOND, 55);
    Date now = cal.getTime();
    test.setTimestampColumn(now);
    context.commitChanges();
    DataRow testRead = (DataRow) context.performQuery(MappedSelect.query("SelectDateTest")).get(0);
    Date columnValue = (Date) testRead.get("TIMESTAMP_COLUMN");
    assertNotNull(columnValue);
    assertEquals(now, columnValue);
}
Also used : Calendar(java.util.Calendar) DateTestEntity(org.apache.cayenne.testdo.date_time.DateTestEntity) DataRow(org.apache.cayenne.DataRow) Date(java.util.Date) Test(org.junit.Test)

Example 14 with DateTestEntity

use of org.apache.cayenne.testdo.date_time.DateTestEntity in project cayenne by apache.

the class DateTimeTypesIT method testDate.

@Test
public void testDate() throws Exception {
    DateTestEntity test = context.newObject(DateTestEntity.class);
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(2002, 1, 1);
    Date nowDate = cal.getTime();
    test.setDateColumn(nowDate);
    context.commitChanges();
    SelectQuery q = new SelectQuery(DateTestEntity.class);
    DateTestEntity testRead = (DateTestEntity) context.performQuery(q).get(0);
    assertNotNull(testRead.getDateColumn());
    assertEquals(nowDate, testRead.getDateColumn());
    assertEquals(Date.class, testRead.getDateColumn().getClass());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Calendar(java.util.Calendar) DateTestEntity(org.apache.cayenne.testdo.date_time.DateTestEntity) Date(java.util.Date) Test(org.junit.Test)

Example 15 with DateTestEntity

use of org.apache.cayenne.testdo.date_time.DateTestEntity in project cayenne by apache.

the class ASTExtractIT method createDataSet.

@Before
public void createDataSet() throws Exception {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MILLISECOND, 0);
    DateTestEntity o1 = context.newObject(DateTestEntity.class);
    cal.set(2015, Calendar.FEBRUARY, 28, 0, 0, 0);
    o1.setDateColumn(cal.getTime());
    cal.set(2017, Calendar.MARCH, 30, 0, 0, 0);
    o1.setTimeColumn(cal.getTime());
    cal.set(Calendar.DAY_OF_MONTH, 29);
    o1.setTimestampColumn(cal.getTime());
    DateTestEntity o2 = context.newObject(DateTestEntity.class);
    cal.set(2016, Calendar.MARCH, 29, 0, 0, 0);
    o2.setDateColumn(cal.getTime());
    cal.set(2017, Calendar.APRIL, 1, 23, 59, 39);
    o2.setTimeColumn(cal.getTime());
    cal.set(Calendar.DAY_OF_MONTH, 2);
    o2.setTimestampColumn(cal.getTime());
    context.commitChanges();
}
Also used : Calendar(java.util.Calendar) DateTestEntity(org.apache.cayenne.testdo.date_time.DateTestEntity) Before(org.junit.Before)

Aggregations

DateTestEntity (org.apache.cayenne.testdo.date_time.DateTestEntity)17 Test (org.junit.Test)15 Calendar (java.util.Calendar)11 Date (java.util.Date)6 Expression (org.apache.cayenne.exp.Expression)6 DataRow (org.apache.cayenne.DataRow)3 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)3 SelectQuery (org.apache.cayenne.query.SelectQuery)3 Before (org.junit.Before)2 Time (java.sql.Time)1