Author: tfischer
Date: Mon Jan 30 19:53:14 2012
New Revision: 1237948
URL: http://svn.apache.org/viewvc?rev=1237948&view=rev
Log:
TORQUE-94 add tests for timestamp accuracy
Modified:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DateTest.java
Modified:
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DateTest.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DateTest.java?rev=1237948&r1=1237947&r2=1237948&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DateTest.java
(original)
+++
db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DateTest.java
Mon Jan 30 19:53:14 2012
@@ -30,6 +30,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.torque.BaseDatabaseTestCase;
import org.apache.torque.TorqueException;
+import org.apache.torque.adapter.DBMM;
import org.apache.torque.adapter.DBMSSQL;
import org.apache.torque.adapter.DBOracle;
import org.apache.torque.criteria.Criteria;
@@ -37,7 +38,7 @@ import org.apache.torque.test.DateTimeTi
import org.apache.torque.test.DateTimeTimestampPeer;
/**
- * Runtime tests.
+ * Tests behavior of date, time and timestamp fields.
*
* @author <a href="mailto:[email protected]">Scott Eade</a>
* @author <a href="mailto:[email protected]">Martin Poeschl</a>
@@ -359,6 +360,110 @@ public class DateTest extends BaseDataba
}
/**
+ * Checks that a select is possible using a java.util.date object
+ * in a timestamp field and does not match for a second difference.
+ *
+ * @throws TorqueException if a problem occurs.
+ */
+ public void testSelectWithUtilDateOnTimestampMismatch()
+ throws TorqueException
+ {
+ cleanDateTimeTimestampTable();
+
+ // insert new DateTest object to db
+ DateTimeTimestamp dateTimeTimestamp = new DateTimeTimestamp();
+ dateTimeTimestamp.setDateValue(new Date());
+ dateTimeTimestamp.setTimeValue(new Date());
+ dateTimeTimestamp.setTimestampValue(
+ new GregorianCalendar(2010, 1, 23).getTime());
+ dateTimeTimestamp.save();
+
+ // execute select
+ Criteria criteria = new Criteria();
+ Date toSelect = new GregorianCalendar(2010, 1, 23).getTime();
+ toSelect = new Date(toSelect.getTime() - 1000L);
+ criteria.where(DateTimeTimestampPeer.TIMESTAMP_VALUE, toSelect);
+ List<DateTimeTimestamp> result
+ = DateTimeTimestampPeer.doSelect(criteria);
+
+ // verify
+ assertEquals(0, result.size());
+ }
+
+ /**
+ * Checks that a select is possible when milliseconds are used.
+ * in databases where this is supported.
+ *
+ * @throws TorqueException if a problem occurs.
+ */
+ public void testSelectWithMillisecondsOnTimestampExactMatch()
+ throws TorqueException
+ {
+ cleanDateTimeTimestampTable();
+
+ // insert new DateTest object to db
+ DateTimeTimestamp dateTimeTimestamp = new DateTimeTimestamp();
+ dateTimeTimestamp.setDateValue(new Date());
+ dateTimeTimestamp.setTimeValue(new Date());
+ GregorianCalendar calendar = new GregorianCalendar(2010, 1, 23);
+ calendar.set(GregorianCalendar.MILLISECOND, 123);
+ dateTimeTimestamp.setTimestampValue(calendar.getTime());
+ dateTimeTimestamp.save();
+
+ // execute matching select
+ Criteria criteria = new Criteria();
+ calendar = new GregorianCalendar(2010, 1, 23);
+ calendar.set(GregorianCalendar.MILLISECOND, 123);
+ criteria.where(
+ DateTimeTimestampPeer.TIMESTAMP_VALUE,
+ calendar.getTime());
+ List<DateTimeTimestamp> result
+ = DateTimeTimestampPeer.doSelect(criteria);
+
+ // verify
+ assertEquals(1, result.size());
+ assertEquals(dateTimeTimestamp, result.get(0));
+ }
+
+ /**
+ * Checks that a select does not match when a timestamp to select
+ * is a millisecond away from the timestamp saved in the database.
+ *
+ * @throws TorqueException if a problem occurs.
+ */
+ public void testSelectWithMillisecondsOnTimestampMillisecondMismatch()
+ throws TorqueException
+ {
+ if (!timestampHasMillisecondAccuracy())
+ {
+ return;
+ }
+ cleanDateTimeTimestampTable();
+
+ // insert new DateTest object to db
+ DateTimeTimestamp dateTimeTimestamp = new DateTimeTimestamp();
+ dateTimeTimestamp.setDateValue(new Date());
+ dateTimeTimestamp.setTimeValue(new Date());
+ GregorianCalendar calendar = new GregorianCalendar(2010, 1, 23);
+ calendar.set(GregorianCalendar.MILLISECOND, 123);
+ dateTimeTimestamp.setTimestampValue(calendar.getTime());
+ dateTimeTimestamp.save();
+
+ // execute matching select
+ Criteria criteria = new Criteria();
+ calendar = new GregorianCalendar(2010, 1, 23);
+ calendar.set(GregorianCalendar.MILLISECOND, 124);
+ criteria.where(
+ DateTimeTimestampPeer.TIMESTAMP_VALUE,
+ calendar.getTime());
+ List<DateTimeTimestamp> result
+ = DateTimeTimestampPeer.doSelect(criteria);
+
+ // verify
+ assertEquals(0, result.size());
+ }
+
+ /**
* Checks that a select is possible using a java.sql.timestamp object
* in a timestamp field.
*
@@ -404,4 +509,19 @@ public class DateTest extends BaseDataba
Criteria.NOT_EQUAL);
DateTimeTimestampPeer.doDelete(criteria);
}
+
+ private boolean timestampHasMillisecondAccuracy()
+ {
+ if (defaultAdapter instanceof DBMM)
+ {
+ return false;
+ }
+ if (defaultAdapter instanceof DBMSSQL)
+ {
+ // although datetime2 has 100 nanoseconds accurary
+ // it seems to get lost in the jtds driver.
+ return false;
+ }
+ return true;
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]