mardi 7 avril 2015

Java Unit testing with time

I save records with the a timestamp in an oracle database. This timestamp indicates the moment of a several event.


I need the age of the oldest item. In a logical way this means



current time - time event



I use folowing SQL code to get the age of the oldest item from my database



select dm1.name_event,
TO_CHAR(MAX(TO_DATE('20000101','yyyymmdd')+(SYSDATE - dm1.time_event)),'hh24:mi:ss') as "age_of_oldest_item"
from dm_procmon dm1


As good practise I want to test this code in my java application with JUnit.



@Test
public void oneItem() throws SQLException {
stm = connection.prepareStatement("INSERT INTO DM_PROCMON (DM_ID, WORKFLOW, NAME_EVENT, TIME_EVENT) VALUES(DM_ID.nextval,'1-EF.txt', 'up', ?)");
stm.setTime(1, new Time(getTime(1, 0, 0))); // 1 hour
stm.execute();

ResultSet resultSet = connection.createStatement().executeQuery(Utilities.resourceToString("logi.sql"));
Time currentAge = new Time(0);
if (resultSet != null) {
while (resultSet.next()) {
currentAge = resultSet.getTime(4);
}
}

Assert.assertEquals(new Date().getTime(), currentAge.getTime());
}


I get an assertion error. What I think the problem is is that there is a timestamp difference between the statement which gets executed and the time with the assertion statement happens.


The assertion output is



java.lang.AssertionError: Expected :1428398404004 Actual :33604000



So I think there is maybe also a rounding mistake in my approach. What is the solution for this?


Aucun commentaire:

Enregistrer un commentaire