1 /*
2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * This file is available under and governed by the GNU General Public
26  * License version 2 only, as published by the Free Software Foundation.
27  * However, the following notice accompanied the original version of this
28  * file:
29  *
30  * Copyright (c) 2007-2012, Stephen Colebourne & Michael Nascimento Santos
31  *
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions are met:
36  *
37  *  * Redistributions of source code must retain the above copyright notice,
38  *    this list of conditions and the following disclaimer.
39  *
40  *  * Redistributions in binary form must reproduce the above copyright notice,
41  *    this list of conditions and the following disclaimer in the documentation
42  *    and/or other materials provided with the distribution.
43  *
44  *  * Neither the name of JSR-310 nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
51  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
52  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
53  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
55  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
56  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
57  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  */
60 package test.java.time;
61 
62 import static org.testng.Assert.assertEquals;
63 import static org.testng.Assert.assertSame;
64 import static org.testng.Assert.assertTrue;
65 
66 import java.time.Clock;
67 import java.time.LocalTime;
68 
69 import org.testng.annotations.Test;
70 
71 /**
72  * Test LocalTime.
73  */
74 @Test
75 public class TestLocalTime extends AbstractTest {
76     static final long NANOS_PER_SECOND = 1_000_000_000L;
77     static final long NANOS_PER_MINUTE = 60 * NANOS_PER_SECOND;
78     static final long NANOS_PER_DAY = 24 * 60 * NANOS_PER_MINUTE;
79 
80     //-----------------------------------------------------------------------
81     @Test
test_immutable()82     public void test_immutable() {
83         assertImmutable(LocalTime.class);
84     }
85 
86     //-----------------------------------------------------------------------
check(LocalTime time, int h, int m, int s, int n)87     private void check(LocalTime time, int h, int m, int s, int n) {
88         assertEquals(time.getHour(), h);
89         assertEquals(time.getMinute(), m);
90         assertEquals(time.getSecond(), s);
91         assertEquals(time.getNano(), n);
92     }
93 
94     //-----------------------------------------------------------------------
95     @Test
constant_MIDNIGHT()96     public void constant_MIDNIGHT() {
97         check(LocalTime.MIDNIGHT, 0, 0, 0, 0);
98     }
99 
100     @Test
constant_MIDNIGHT_same()101     public void constant_MIDNIGHT_same() {
102         assertSame(LocalTime.MIDNIGHT, LocalTime.MIDNIGHT);
103         assertSame(LocalTime.MIDNIGHT, LocalTime.of(0, 0));
104     }
105 
106     @Test
constant_MIDDAY()107     public void constant_MIDDAY() {
108         check(LocalTime.NOON, 12, 0, 0, 0);
109     }
110 
111     @Test
constant_MIDDAY_same()112     public void constant_MIDDAY_same() {
113         assertSame(LocalTime.NOON, LocalTime.NOON);
114         assertSame(LocalTime.NOON, LocalTime.of(12, 0));
115     }
116 
117     //-----------------------------------------------------------------------
118     @Test
constant_MIN_TIME()119     public void constant_MIN_TIME() {
120         check(LocalTime.MIN, 0, 0, 0, 0);
121     }
122 
123     @Test
constant_MIN_TIME_same()124     public void constant_MIN_TIME_same() {
125         assertSame(LocalTime.MIN, LocalTime.of(0, 0));
126     }
127 
128     @Test
constant_MAX_TIME()129     public void constant_MAX_TIME() {
130         check(LocalTime.MAX, 23, 59, 59, 999999999);
131     }
132 
133     @Test
constant_MAX_TIME_same()134     public void constant_MAX_TIME_same() {
135         assertSame(LocalTime.NOON, LocalTime.NOON);
136         assertSame(LocalTime.NOON, LocalTime.of(12, 0));
137     }
138 
139     @Test
factory_time_2ints_singletons()140     public void factory_time_2ints_singletons() {
141         for (int i = 0; i < 24; i++) {
142             LocalTime test1 = LocalTime.of(i, 0);
143             LocalTime test2 = LocalTime.of(i, 0);
144             assertSame(test1, test2);
145         }
146     }
147 
148     @Test
factory_time_3ints_singletons()149     public void factory_time_3ints_singletons() {
150         for (int i = 0; i < 24; i++) {
151             LocalTime test1 = LocalTime.of(i, 0, 0);
152             LocalTime test2 = LocalTime.of(i, 0, 0);
153             assertSame(test1, test2);
154         }
155     }
156 
157     @Test
factory_time_4ints_singletons()158     public void factory_time_4ints_singletons() {
159         for (int i = 0; i < 24; i++) {
160             LocalTime test1 = LocalTime.of(i, 0, 0, 0);
161             LocalTime test2 = LocalTime.of(i, 0, 0, 0);
162             assertSame(test1, test2);
163         }
164     }
165 
166     @Test
factory_ofSecondOfDay_singletons()167     public void factory_ofSecondOfDay_singletons() {
168         for (int i = 0; i < 24; i++) {
169             LocalTime test1 = LocalTime.ofSecondOfDay(i * 60L * 60L);
170             LocalTime test2 = LocalTime.of(i, 0);
171             assertSame(test1, test2);
172         }
173     }
174 
175     @Test
factory_ofNanoOfDay_singletons()176     public void factory_ofNanoOfDay_singletons() {
177         for (int i = 0; i < 24; i++) {
178             LocalTime test1 = LocalTime.ofNanoOfDay(i * 1000000000L * 60L * 60L);
179             LocalTime test2 = LocalTime.of(i, 0);
180             assertSame(test1, test2);
181         }
182     }
183 
184     //-----------------------------------------------------------------------
185     // now()
186     //-----------------------------------------------------------------------
187     @Test
188     @SuppressWarnings("unused")
now()189     public void now() {
190         // Warmup the TimeZone data so the following test does not include
191         // one-time initialization
192         LocalTime.now(Clock.systemDefaultZone());
193 
194         long diff = Integer.MAX_VALUE;
195         for (int i = 0; i < 2; i++) {
196             LocalTime expected = LocalTime.now(Clock.systemDefaultZone());
197             LocalTime test = LocalTime.now();
198             diff = test.toNanoOfDay() - expected.toNanoOfDay();
199             // Normalize for wrap-around midnight
200             diff = Math.floorMod(NANOS_PER_DAY + diff, NANOS_PER_DAY);
201             if (diff < 100000000) {
202                 break;
203             }
204             // A second iteration may be needed if the clock changed
205             // due to a DST change between the two calls to now.
206         }
207         assertTrue(diff < 100000000,   // less than 0.1 sec
208                 "LocalTime.now  vs LocalTime.now(Clock.systemDefaultZone()) not close");
209     }
210 
211 }
212