• 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 import static org.testng.Assert.fail;
37 
38 import java.io.IOException;
39 
40 import org.testng.annotations.Test;
41 
42 /**
43  * Test system clock.
44  */
45 @Test
46 public class TestClock_System extends AbstractTest {
47 
48     private static final ZoneId MOSCOW = ZoneId.of("Europe/Moscow");
49     private static final ZoneId PARIS = ZoneId.of("Europe/Paris");
50 
51     //-----------------------------------------------------------------------
test_isSerializable()52     public void test_isSerializable() throws IOException, ClassNotFoundException {
53         assertSerializable(Clock.systemUTC());
54         assertSerializable(Clock.systemDefaultZone());
55         assertSerializable(Clock.system(PARIS));
56     }
57 
58     //-----------------------------------------------------------------------
test_instant()59     public void test_instant() {
60         Clock system = Clock.systemUTC();
61         assertEquals(system.getZone(), ZoneOffset.UTC);
62         for (int i = 0; i < 10000; i++) {
63             // assume can eventually get these within 10 milliseconds
64             Instant instant = system.instant();
65             long systemMillis = System.currentTimeMillis();
66             if (systemMillis - instant.toEpochMilli() < 10) {
67                 return;  // success
68             }
69         }
70         fail();
71     }
72 
test_millis()73     public void test_millis() {
74         Clock system = Clock.systemUTC();
75         assertEquals(system.getZone(), ZoneOffset.UTC);
76         for (int i = 0; i < 10000; i++) {
77             // assume can eventually get these within 10 milliseconds
78             long instant = system.millis();
79             long systemMillis = System.currentTimeMillis();
80             if (systemMillis - instant < 10) {
81                 return;  // success
82             }
83         }
84         fail();
85     }
86 
87     //-------------------------------------------------------------------------
test_systemUTC()88     public void test_systemUTC() {
89         Clock test = Clock.systemUTC();
90         assertEquals(test.getZone(), ZoneOffset.UTC);
91         assertEquals(test, Clock.system(ZoneOffset.UTC));
92     }
93 
test_systemDefaultZone()94     public void test_systemDefaultZone() {
95         Clock test = Clock.systemDefaultZone();
96         assertEquals(test.getZone(), ZoneId.systemDefault());
97         assertEquals(test, Clock.system(ZoneId.systemDefault()));
98     }
99 
test_system_ZoneId()100     public void test_system_ZoneId() {
101         Clock test = Clock.system(PARIS);
102         assertEquals(test.getZone(), PARIS);
103     }
104 
105     @Test(expectedExceptions = NullPointerException.class)
test_zoneId_nullZoneId()106     public void test_zoneId_nullZoneId() {
107         Clock.system(null);
108     }
109 
110     //-------------------------------------------------------------------------
test_withZone()111     public void test_withZone() {
112         Clock test = Clock.system(PARIS);
113         Clock changed = test.withZone(MOSCOW);
114         assertEquals(test.getZone(), PARIS);
115         assertEquals(changed.getZone(), MOSCOW);
116     }
117 
test_withZone_same()118     public void test_withZone_same() {
119         Clock test = Clock.system(PARIS);
120         Clock changed = test.withZone(PARIS);
121         assertSame(test, changed);
122     }
123 
test_withZone_fromUTC()124     public void test_withZone_fromUTC() {
125         Clock test = Clock.systemUTC();
126         Clock changed = test.withZone(PARIS);
127         assertEquals(changed.getZone(), PARIS);
128     }
129 
130     @Test(expectedExceptions = NullPointerException.class)
test_withZone_null()131     public void test_withZone_null() {
132         Clock.systemUTC().withZone(null);
133     }
134 
135     //-----------------------------------------------------------------------
test_equals()136     public void test_equals() {
137         Clock a = Clock.systemUTC();
138         Clock b = Clock.systemUTC();
139         assertEquals(a.equals(a), true);
140         assertEquals(a.equals(b), true);
141         assertEquals(b.equals(a), true);
142         assertEquals(b.equals(b), true);
143 
144         Clock c = Clock.system(PARIS);
145         Clock d = Clock.system(PARIS);
146         assertEquals(c.equals(c), true);
147         assertEquals(c.equals(d), true);
148         assertEquals(d.equals(c), true);
149         assertEquals(d.equals(d), true);
150 
151         assertEquals(a.equals(c), false);
152         assertEquals(c.equals(a), false);
153 
154         assertEquals(a.equals(null), false);
155         assertEquals(a.equals("other type"), false);
156         assertEquals(a.equals(Clock.fixed(Instant.now(), ZoneOffset.UTC)), false);
157     }
158 
test_hashCode()159     public void test_hashCode() {
160         Clock a = Clock.system(ZoneOffset.UTC);
161         Clock b = Clock.system(ZoneOffset.UTC);
162         assertEquals(a.hashCode(), a.hashCode());
163         assertEquals(a.hashCode(), b.hashCode());
164 
165         Clock c = Clock.system(PARIS);
166         assertEquals(a.hashCode() == c.hashCode(), false);
167     }
168 
169     //-----------------------------------------------------------------------
test_toString()170     public void test_toString() {
171         Clock test = Clock.system(PARIS);
172         assertEquals(test.toString(), "SystemClock[Europe/Paris]");
173     }
174 
175 }
176