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 */
229 //-----------------------------------------------------------------------
230 // now(ZoneId)
231 //-----------------------------------------------------------------------
232 @Test(expectedExceptions=NullPointerException.class)
233 public void now_ZoneId_nullZoneId() {
234 LocalTime.now((ZoneId) null);
235 }
236
237 @Test
238 public void now_ZoneId() {
239 ZoneId zone = ZoneId.of("UTC+01:02:03");
240 LocalTime expected = LocalTime.now(Clock.system(zone));
241 LocalTime test = LocalTime.now(zone);
242 for (int i = 0; i < 100; i++) {
243 if (expected.equals(test)) {
244 return;
245 }
246 expected = LocalTime.now(Clock.system(zone));
247 test = LocalTime.now(zone);
248 }
249 assertEquals(test, expected);
250 }
251
252 //-----------------------------------------------------------------------
253 // now(Clock)
254 //-----------------------------------------------------------------------
255 @Test(expectedExceptions=NullPointerException.class)
256 public void now_Clock_nullClock() {
257 LocalTime.now((Clock) null);
258 }
259
260 @Test
261 public void now_Clock_allSecsInDay() {
262 for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
263 Instant instant = Instant.ofEpochSecond(i, 8);
264 Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
265 LocalTime test = LocalTime.now(clock);
266 assertEquals(test.getHour(), (i / (60 * 60)) % 24);
267 assertEquals(test.getMinute(), (i / 60) % 60);
268 assertEquals(test.getSecond(), i % 60);
269 assertEquals(test.getNano(), 8);
|
1 /*
2 * Copyright (c) 2012, 2015, 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 */
229 //-----------------------------------------------------------------------
230 // now(ZoneId)
231 //-----------------------------------------------------------------------
232 @Test(expectedExceptions=NullPointerException.class)
233 public void now_ZoneId_nullZoneId() {
234 LocalTime.now((ZoneId) null);
235 }
236
237 @Test
238 public void now_ZoneId() {
239 ZoneId zone = ZoneId.of("UTC+01:02:03");
240 LocalTime expected = LocalTime.now(Clock.system(zone));
241 LocalTime test = LocalTime.now(zone);
242 for (int i = 0; i < 100; i++) {
243 if (expected.equals(test)) {
244 return;
245 }
246 expected = LocalTime.now(Clock.system(zone));
247 test = LocalTime.now(zone);
248 }
249 assertEquals(test.truncatedTo(ChronoUnit.SECONDS),
250 expected.truncatedTo(ChronoUnit.SECONDS));
251 }
252
253 //-----------------------------------------------------------------------
254 // now(Clock)
255 //-----------------------------------------------------------------------
256 @Test(expectedExceptions=NullPointerException.class)
257 public void now_Clock_nullClock() {
258 LocalTime.now((Clock) null);
259 }
260
261 @Test
262 public void now_Clock_allSecsInDay() {
263 for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
264 Instant instant = Instant.ofEpochSecond(i, 8);
265 Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
266 LocalTime test = LocalTime.now(clock);
267 assertEquals(test.getHour(), (i / (60 * 60)) % 24);
268 assertEquals(test.getMinute(), (i / 60) % 60);
269 assertEquals(test.getSecond(), i % 60);
270 assertEquals(test.getNano(), 8);
|