Search in sources :

Example 1 with DateTestEntity

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

the class DateTimeTypesIT method testTimestamp.

@Test
public void testTimestamp() 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();
    SelectQuery q = new SelectQuery(DateTestEntity.class);
    DateTestEntity testRead = (DateTestEntity) context.performQuery(q).get(0);
    assertNotNull(testRead.getTimestampColumn());
    assertEquals(now, testRead.getTimestampColumn());
}
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 2 with DateTestEntity

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

the class DateTimeTypesIT method testSQLTemplateTime.

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

Example 3 with DateTestEntity

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

the class DateTimeTypesIT method testTime.

@Test
public void testTime() throws Exception {
    DateTestEntity test = context.newObject(DateTestEntity.class);
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(1970, 0, 1, 1, 20, 30);
    Date nowTime = cal.getTime();
    test.setTimeColumn(nowTime);
    context.commitChanges();
    SelectQuery q = new SelectQuery(DateTestEntity.class);
    DateTestEntity testRead = (DateTestEntity) context.performQuery(q).get(0);
    assertNotNull(testRead.getTimeColumn());
    assertEquals(Date.class, testRead.getTimeColumn().getClass());
    // OpenBase fails to store seconds for the time
    // FrontBase returns time with 1 hour offset (I guess "TIME WITH TIMEZONE" may
    // need to be used as a default FB type?)
    // so this test is approximate...
    long delta = nowTime.getTime() - testRead.getTimeColumn().getTime();
    assertTrue("" + delta, Math.abs(delta) <= 1000 * 60 * 60);
}
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 4 with DateTestEntity

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

the class DateTimeTypesIT method testSQLTemplateDate.

@Test
public void testSQLTemplateDate() throws Exception {
    DateTestEntity test = (DateTestEntity) context.newObject("DateTestEntity");
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(2003, 1, 1, 1, 20, 30);
    // most databases fail millisecond accuracy
    // cal.set(Calendar.MILLISECOND, 55);
    java.sql.Date now = new java.sql.Date(cal.getTime().getTime());
    test.setDateColumn(now);
    context.commitChanges();
    DataRow testRead = (DataRow) context.performQuery(MappedSelect.query("SelectDateTest")).get(0);
    Date columnValue = (Date) testRead.get("DATE_COLUMN");
    assertNotNull(columnValue);
    assertEquals(now.toString(), new java.sql.Date(columnValue.getTime()).toString());
}
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 5 with DateTestEntity

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

the class ASTFunctionCallDateIT method testASTCurrentTimeParse.

@Test
public void testASTCurrentTimeParse() {
    Expression exp = ExpressionFactory.exp("timeColumn > currentTime()");
    DateTestEntity res = ObjectSelect.query(DateTestEntity.class, exp).selectOne(context);
    if (!unitDbAdapter.supportsTimeSqlType()) {
        return;
    }
    assertNotNull(res);
}
Also used : Expression(org.apache.cayenne.exp.Expression) DateTestEntity(org.apache.cayenne.testdo.date_time.DateTestEntity) Test(org.junit.Test)

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