Skip to content

Commit b6e02c9

Browse files
Adjust test to respect expected inaccuracies
1 parent 7a233ae commit b6e02c9

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/test/java/org/javawebstack/orm/test/TypesTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import org.javawebstack.orm.exception.ORMConfigurationException;
99
import org.junit.jupiter.api.Test;
1010

11+
import javax.swing.text.TabSet;
1112
import java.sql.Timestamp;
1213
import java.time.Instant;
1314

15+
import static org.javawebstack.orm.test.shared.util.TimestampUtil.diffInNanoseconds;
1416
import static org.junit.jupiter.api.Assertions.*;
1517

1618
public class TypesTest extends ORMTestCase {
@@ -58,10 +60,7 @@ public void testFields() throws ORMConfigurationException {
5860
assertEquals(model.exampleDouble, 123456789.1234567890D);
5961
assertEquals(model.exampleLong, 999999999999999999L);
6062
assertEquals(model.exampleLongPrimitive, 999999999999999999L);
61-
62-
// TODO: Use
63-
assertNotNull(model.timestampTest);
64-
63+
assertTrue(diffInNanoseconds(timestamp, model.timestampTest) < 1);
6564
model.exampleNull = "Text";
6665
model.save();
6766
model = Repo.get(ExampleModel.class).get(id);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.javawebstack.orm.test.shared.util;
2+
3+
import java.sql.Timestamp;
4+
5+
/*
6+
* The original author is Aaron Digulla
7+
* https://stackoverflow.com/a/582487
8+
*/
9+
public class TimestampUtil {
10+
11+
public static Double diffInNanoseconds (java.util.Date t1, java.util.Date t2)
12+
{
13+
// Make sure the result is always > 0
14+
if (t1.compareTo (t2) < 0)
15+
{
16+
java.util.Date tmp = t1;
17+
t1 = t2;
18+
t2 = tmp;
19+
}
20+
21+
// Timestamps mix milli and nanoseconds in the API, so we have to separate the two
22+
long diffSeconds = (t1.getTime () / 1000) - (t2.getTime () / 1000);
23+
// For normals dates, we have millisecond precision
24+
int nano1 = ((int) t1.getTime () % 1000) * 1000000;
25+
// If the parameter is a Timestamp, we have additional precision in nanoseconds
26+
if (t1 instanceof Timestamp)
27+
nano1 = ((Timestamp)t1).getNanos ();
28+
int nano2 = ((int) t2.getTime () % 1000) * 1000000;
29+
if (t2 instanceof Timestamp)
30+
nano2 = ((Timestamp)t2).getNanos ();
31+
32+
int diffNanos = nano1 - nano2;
33+
if (diffNanos < 0)
34+
{
35+
// Borrow one second
36+
diffSeconds --;
37+
diffNanos += 1000000000;
38+
}
39+
40+
return diffSeconds * 1000000000.0 + diffNanos / 1000.0;
41+
42+
}
43+
}

0 commit comments

Comments
 (0)