16
16
17
17
package com .google .cloud .spanner ;
18
18
19
+ import static com .google .cloud .spanner .Interval .MICROS_PER_HOUR ;
20
+ import static com .google .cloud .spanner .Interval .MICROS_PER_MILLI ;
21
+ import static com .google .cloud .spanner .Interval .MICROS_PER_MINUTE ;
22
+ import static com .google .cloud .spanner .Interval .MICROS_PER_SECOND ;
23
+ import static com .google .cloud .spanner .Interval .MILLIS_PER_SECOND ;
24
+ import static com .google .cloud .spanner .Interval .MINUTES_PER_HOUR ;
25
+ import static com .google .cloud .spanner .Interval .NANOS_PER_HOUR ;
26
+ import static com .google .cloud .spanner .Interval .NANOS_PER_MICRO ;
27
+ import static com .google .cloud .spanner .Interval .NANOS_PER_MICROSECOND ;
28
+ import static com .google .cloud .spanner .Interval .NANOS_PER_MILLISECOND ;
29
+ import static com .google .cloud .spanner .Interval .NANOS_PER_MINUTE ;
30
+ import static com .google .cloud .spanner .Interval .NANOS_PER_SECOND ;
31
+ import static com .google .cloud .spanner .Interval .SECONDS_PER_HOUR ;
32
+ import static com .google .cloud .spanner .Interval .SECONDS_PER_MINUTE ;
19
33
import static org .junit .Assert .assertEquals ;
20
34
import static org .junit .Assert .assertNotEquals ;
21
35
import static org .junit .Assert .assertThrows ;
22
36
23
37
import java .math .BigInteger ;
38
+ import java .time .Duration ;
39
+ import java .time .temporal .ChronoUnit ;
40
+ import java .util .concurrent .TimeUnit ;
24
41
import org .junit .Test ;
25
42
import org .junit .runner .RunWith ;
26
43
import org .junit .runners .JUnit4 ;
@@ -50,17 +67,15 @@ public void testOfSeconds() {
50
67
Interval interval = Interval .ofSeconds (10 );
51
68
assertEquals (0 , interval .getMonths ());
52
69
assertEquals (0 , interval .getDays ());
53
- assertEquals (
54
- BigInteger .valueOf (10 ).multiply (Interval .NANOS_PER_SECOND ), interval .getNanoseconds ());
70
+ assertEquals (BigInteger .valueOf (Duration .ofSeconds (10 ).toNanos ()), interval .getNanoseconds ());
55
71
}
56
72
57
73
@ Test
58
74
public void testOfMilliseconds () {
59
75
Interval interval = Interval .ofMilliseconds (10 );
60
76
assertEquals (0 , interval .getMonths ());
61
77
assertEquals (0 , interval .getDays ());
62
- assertEquals (
63
- BigInteger .valueOf (10 ).multiply (Interval .NANOS_PER_MILLISECOND ), interval .getNanoseconds ());
78
+ assertEquals (BigInteger .valueOf (Duration .ofMillis (10 ).toNanos ()), interval .getNanoseconds ());
64
79
}
65
80
66
81
@ Test
@@ -69,7 +84,8 @@ public void testOfMicroseconds() {
69
84
assertEquals (0 , interval .getMonths ());
70
85
assertEquals (0 , interval .getDays ());
71
86
assertEquals (
72
- BigInteger .valueOf (10 ).multiply (Interval .NANOS_PER_MICROSECOND ), interval .getNanoseconds ());
87
+ BigInteger .valueOf (Duration .of (10 , ChronoUnit .MICROS ).toNanos ()),
88
+ interval .getNanoseconds ());
73
89
}
74
90
75
91
@ Test
@@ -347,6 +363,32 @@ public void testHashCode() {
347
363
assertNotEquals (interval10 .hashCode (), interval11 .hashCode ());
348
364
}
349
365
366
+ @ Test
367
+ public void testConstants () {
368
+ assertEquals (Duration .of (1 , ChronoUnit .HOURS ).toMinutes (), MINUTES_PER_HOUR );
369
+ assertEquals (Duration .of (1 , ChronoUnit .MINUTES ).getSeconds (), SECONDS_PER_MINUTE );
370
+ assertEquals (Duration .of (1 , ChronoUnit .HOURS ).getSeconds (), SECONDS_PER_HOUR );
371
+ assertEquals (Duration .of (1 , ChronoUnit .SECONDS ).toMillis (), MILLIS_PER_SECOND );
372
+
373
+ assertEquals (TimeUnit .MICROSECONDS .convert (1 , TimeUnit .MILLISECONDS ), MICROS_PER_MILLI );
374
+ assertEquals (TimeUnit .NANOSECONDS .convert (1 , TimeUnit .MICROSECONDS ), NANOS_PER_MICRO );
375
+ assertEquals (TimeUnit .MICROSECONDS .convert (1 , TimeUnit .SECONDS ), MICROS_PER_SECOND );
376
+ assertEquals (TimeUnit .MICROSECONDS .convert (1 , TimeUnit .MINUTES ), MICROS_PER_MINUTE );
377
+ assertEquals (TimeUnit .MICROSECONDS .convert (1 , TimeUnit .HOURS ), MICROS_PER_HOUR );
378
+
379
+ assertEquals (
380
+ TimeUnit .NANOSECONDS .convert (1 , TimeUnit .MICROSECONDS ),
381
+ NANOS_PER_MICROSECOND .longValueExact ());
382
+ assertEquals (
383
+ TimeUnit .NANOSECONDS .convert (1 , TimeUnit .MILLISECONDS ),
384
+ NANOS_PER_MILLISECOND .longValueExact ());
385
+ assertEquals (
386
+ TimeUnit .NANOSECONDS .convert (1 , TimeUnit .SECONDS ), NANOS_PER_SECOND .longValueExact ());
387
+ assertEquals (
388
+ TimeUnit .NANOSECONDS .convert (1 , TimeUnit .MINUTES ), NANOS_PER_MINUTE .longValueExact ());
389
+ assertEquals (TimeUnit .NANOSECONDS .convert (1 , TimeUnit .HOURS ), NANOS_PER_HOUR .longValueExact ());
390
+ }
391
+
350
392
private static class TestCase {
351
393
private final String intervalString ;
352
394
private final int months ;
0 commit comments