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 */
275 //-----------------------------------------------------------------------
276 // now(ZoneId)
277 //-----------------------------------------------------------------------
278 @Test(expectedExceptions=NullPointerException.class)
279 public void now_ZoneId_nullZoneId() {
280 LocalDateTime.now((ZoneId) null);
281 }
282
283 @Test
284 public void now_ZoneId() {
285 ZoneId zone = ZoneId.of("UTC+01:02:03");
286 LocalDateTime expected = LocalDateTime.now(Clock.system(zone));
287 LocalDateTime test = LocalDateTime.now(zone);
288 for (int i = 0; i < 100; i++) {
289 if (expected.equals(test)) {
290 return;
291 }
292 expected = LocalDateTime.now(Clock.system(zone));
293 test = LocalDateTime.now(zone);
294 }
295 assertEquals(test, expected);
296 }
297
298 //-----------------------------------------------------------------------
299 // now(Clock)
300 //-----------------------------------------------------------------------
301 @Test(expectedExceptions=NullPointerException.class)
302 public void now_Clock_nullClock() {
303 LocalDateTime.now((Clock) null);
304 }
305
306 @Test
307 public void now_Clock_allSecsInDay_utc() {
308 for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
309 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
310 Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
311 LocalDateTime test = LocalDateTime.now(clock);
312 assertEquals(test.getYear(), 1970);
313 assertEquals(test.getMonth(), Month.JANUARY);
314 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2));
315 assertEquals(test.getHour(), (i / (60 * 60)) % 24);
|
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 */
275 //-----------------------------------------------------------------------
276 // now(ZoneId)
277 //-----------------------------------------------------------------------
278 @Test(expectedExceptions=NullPointerException.class)
279 public void now_ZoneId_nullZoneId() {
280 LocalDateTime.now((ZoneId) null);
281 }
282
283 @Test
284 public void now_ZoneId() {
285 ZoneId zone = ZoneId.of("UTC+01:02:03");
286 LocalDateTime expected = LocalDateTime.now(Clock.system(zone));
287 LocalDateTime test = LocalDateTime.now(zone);
288 for (int i = 0; i < 100; i++) {
289 if (expected.equals(test)) {
290 return;
291 }
292 expected = LocalDateTime.now(Clock.system(zone));
293 test = LocalDateTime.now(zone);
294 }
295 assertEquals(test.truncatedTo(ChronoUnit.SECONDS),
296 expected.truncatedTo(ChronoUnit.SECONDS));
297 }
298
299 //-----------------------------------------------------------------------
300 // now(Clock)
301 //-----------------------------------------------------------------------
302 @Test(expectedExceptions=NullPointerException.class)
303 public void now_Clock_nullClock() {
304 LocalDateTime.now((Clock) null);
305 }
306
307 @Test
308 public void now_Clock_allSecsInDay_utc() {
309 for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
310 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
311 Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
312 LocalDateTime test = LocalDateTime.now(clock);
313 assertEquals(test.getYear(), 1970);
314 assertEquals(test.getMonth(), Month.JANUARY);
315 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2));
316 assertEquals(test.getHour(), (i / (60 * 60)) % 24);
|