• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007-present Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp;
33 
34 import static org.testng.Assert.assertEquals;
35 import static org.testng.Assert.assertSame;
36 
37 import java.io.IOException;
38 
39 import org.testng.annotations.Test;
40 
41 /**
42  * Test tick clock.
43  */
44 @Test
45 public class TestClock_Tick extends AbstractTest {
46 
47     private static final ZoneId MOSCOW = ZoneId.of("Europe/Moscow");
48     private static final ZoneId PARIS = ZoneId.of("Europe/Paris");
49     private static final Duration AMOUNT = Duration.ofSeconds(2);
50     private static final ZonedDateTime ZDT = LocalDateTime.of(2008, 6, 30, 11, 30, 10, 500).atZone(ZoneOffset.ofHours(2));
51     private static final Instant INSTANT = ZDT.toInstant();
52 
53     //-----------------------------------------------------------------------
test_isSerializable()54     public void test_isSerializable() throws IOException, ClassNotFoundException {
55         assertSerializable(Clock.tickSeconds(PARIS));
56         assertSerializable(Clock.tickMinutes(MOSCOW));
57         assertSerializable(Clock.tick(Clock.fixed(INSTANT, PARIS), AMOUNT));
58     }
59 
60     //-----------------------------------------------------------------------
test_tick_ClockDuration_250millis()61     public void test_tick_ClockDuration_250millis() {
62         for (int i = 0; i < 1000; i++) {
63             Clock test = Clock.tick(Clock.fixed(ZDT.withNano(i * 1000000).toInstant(), PARIS), Duration.ofMillis(250));
64             assertEquals(test.instant(), ZDT.withNano((i / 250) * 250000000).toInstant());
65             assertEquals(test.getZone(), PARIS);
66         }
67     }
68 
test_tick_ClockDuration_250micros()69     public void test_tick_ClockDuration_250micros() {
70         for (int i = 0; i < 1000; i++) {
71             Clock test = Clock.tick(Clock.fixed(ZDT.withNano(i * 1000).toInstant(), PARIS), Duration.ofNanos(250000));
72             assertEquals(test.instant(), ZDT.withNano((i / 250) * 250000).toInstant());
73             assertEquals(test.getZone(), PARIS);
74         }
75     }
76 
test_tick_ClockDuration_20nanos()77     public void test_tick_ClockDuration_20nanos() {
78         for (int i = 0; i < 1000; i++) {
79             Clock test = Clock.tick(Clock.fixed(ZDT.withNano(i).toInstant(), PARIS), Duration.ofNanos(20));
80             assertEquals(test.instant(), ZDT.withNano((i / 20) * 20).toInstant());
81             assertEquals(test.getZone(), PARIS);
82         }
83     }
84 
test_tick_ClockDuration_zeroDuration()85     public void test_tick_ClockDuration_zeroDuration() {
86         Clock underlying = Clock.system(PARIS);
87         Clock test = Clock.tick(underlying, Duration.ZERO);
88         assertSame(test, underlying);  // spec says same
89     }
90 
test_tick_ClockDuration_1nsDuration()91     public void test_tick_ClockDuration_1nsDuration() {
92         Clock underlying = Clock.system(PARIS);
93         Clock test = Clock.tick(underlying, Duration.ofNanos(1));
94         assertSame(test, underlying);  // spec says same
95     }
96 
97     @Test(expectedExceptions = ArithmeticException.class)
test_tick_ClockDuration_maxDuration()98     public void test_tick_ClockDuration_maxDuration() {
99         Clock.tick(Clock.systemUTC(), Duration.ofSeconds(Long.MAX_VALUE));
100     }
101 
102     @Test(expectedExceptions = IllegalArgumentException.class)
test_tick_ClockDuration_subMilliNotDivisible_123ns()103     public void test_tick_ClockDuration_subMilliNotDivisible_123ns() {
104         Clock.tick(Clock.systemUTC(), Duration.ofSeconds(0, 123));
105     }
106 
107     @Test(expectedExceptions = IllegalArgumentException.class)
test_tick_ClockDuration_subMilliNotDivisible_999ns()108     public void test_tick_ClockDuration_subMilliNotDivisible_999ns() {
109         Clock.tick(Clock.systemUTC(), Duration.ofSeconds(0, 999));
110     }
111 
112     @Test(expectedExceptions = IllegalArgumentException.class)
test_tick_ClockDuration_subMilliNotDivisible_999999999ns()113     public void test_tick_ClockDuration_subMilliNotDivisible_999999999ns() {
114         Clock.tick(Clock.systemUTC(), Duration.ofSeconds(0, 999999999));
115     }
116 
117     @Test(expectedExceptions = IllegalArgumentException.class)
test_tick_ClockDuration_negative1ns()118     public void test_tick_ClockDuration_negative1ns() {
119         Clock.tick(Clock.systemUTC(), Duration.ofSeconds(0, -1));
120     }
121 
122     @Test(expectedExceptions = IllegalArgumentException.class)
test_tick_ClockDuration_negative1s()123     public void test_tick_ClockDuration_negative1s() {
124         Clock.tick(Clock.systemUTC(), Duration.ofSeconds(-1));
125     }
126 
127     @Test(expectedExceptions = NullPointerException.class)
test_tick_ClockDuration_nullClock()128     public void test_tick_ClockDuration_nullClock() {
129         Clock.tick(null, Duration.ZERO);
130     }
131 
132     @Test(expectedExceptions = NullPointerException.class)
test_tick_ClockDuration_nullDuration()133     public void test_tick_ClockDuration_nullDuration() {
134         Clock.tick(Clock.systemUTC(), null);
135     }
136 
137     //-----------------------------------------------------------------------
test_tickSeconds_ZoneId()138     public void test_tickSeconds_ZoneId() throws Exception {
139         Clock test = Clock.tickSeconds(PARIS);
140         assertEquals(test.getZone(), PARIS);
141         assertEquals(test.instant().getNano(), 0);
142         Thread.sleep(100);
143         assertEquals(test.instant().getNano(), 0);
144     }
145 
146     @Test(expectedExceptions = NullPointerException.class)
test_tickSeconds_ZoneId_nullZoneId()147     public void test_tickSeconds_ZoneId_nullZoneId() {
148         Clock.tickSeconds(null);
149     }
150 
151     //-----------------------------------------------------------------------
test_tickMinutes_ZoneId()152     public void test_tickMinutes_ZoneId() {
153         Clock test = Clock.tickMinutes(PARIS);
154         assertEquals(test.getZone(), PARIS);
155         Instant instant = test.instant();
156         assertEquals(instant.getEpochSecond() % 60, 0);
157         assertEquals(instant.getNano(), 0);
158     }
159 
160     @Test(expectedExceptions = NullPointerException.class)
test_tickMinutes_ZoneId_nullZoneId()161     public void test_tickMinutes_ZoneId_nullZoneId() {
162         Clock.tickMinutes(null);
163     }
164 
165     //-------------------------------------------------------------------------
test_withZone()166     public void test_withZone() {
167         Clock test = Clock.tick(Clock.system(PARIS), Duration.ofMillis(500));
168         Clock changed = test.withZone(MOSCOW);
169         assertEquals(test.getZone(), PARIS);
170         assertEquals(changed.getZone(), MOSCOW);
171     }
172 
test_withZone_same()173     public void test_withZone_same() {
174         Clock test = Clock.tick(Clock.system(PARIS), Duration.ofMillis(500));
175         Clock changed = test.withZone(PARIS);
176         assertSame(test, changed);
177     }
178 
179     @Test(expectedExceptions = NullPointerException.class)
test_withZone_null()180     public void test_withZone_null() {
181         Clock.tick(Clock.system(PARIS), Duration.ofMillis(500)).withZone(null);
182     }
183 
184     //-----------------------------------------------------------------------
test__equals()185     public void test__equals() {
186         Clock a = Clock.tick(Clock.system(PARIS), Duration.ofMillis(500));
187         Clock b = Clock.tick(Clock.system(PARIS), Duration.ofMillis(500));
188         assertEquals(a.equals(a), true);
189         assertEquals(a.equals(b), true);
190         assertEquals(b.equals(a), true);
191         assertEquals(b.equals(b), true);
192 
193         Clock c = Clock.tick(Clock.system(MOSCOW), Duration.ofMillis(500));
194         assertEquals(a.equals(c), false);
195 
196         Clock d = Clock.tick(Clock.system(PARIS), Duration.ofMillis(499));
197         assertEquals(a.equals(d), false);
198 
199         assertEquals(a.equals(null), false);
200         assertEquals(a.equals("other type"), false);
201         assertEquals(a.equals(Clock.systemUTC()), false);
202     }
203 
test_hashCode()204     public void test_hashCode() {
205         Clock a = Clock.tick(Clock.system(PARIS), Duration.ofMillis(500));
206         Clock b = Clock.tick(Clock.system(PARIS), Duration.ofMillis(500));
207         assertEquals(a.hashCode(), a.hashCode());
208         assertEquals(a.hashCode(), b.hashCode());
209 
210         Clock c = Clock.tick(Clock.system(MOSCOW), Duration.ofMillis(500));
211         assertEquals(a.hashCode() == c.hashCode(), false);
212 
213         Clock d = Clock.tick(Clock.system(PARIS), Duration.ofMillis(499));
214         assertEquals(a.hashCode() == d.hashCode(), false);
215     }
216 
217     //-----------------------------------------------------------------------
test_toString()218     public void test_toString() {
219         Clock test = Clock.tick(Clock.systemUTC(), Duration.ofMillis(500));
220         assertEquals(test.toString(), "TickClock[SystemClock[Z],PT0.5S]");
221     }
222 
223 }
224