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 */
243 //-----------------------------------------------------------------------
244 // now(ZoneId)
245 //-----------------------------------------------------------------------
246 @Test(expectedExceptions=NullPointerException.class)
247 public void now_ZoneId_nullZoneId() {
248 ZonedDateTime.now((ZoneId) null);
249 }
250
251 @Test
252 public void now_ZoneId() {
253 ZoneId zone = ZoneId.of("UTC+01:02:03");
254 ZonedDateTime expected = ZonedDateTime.now(Clock.system(zone));
255 ZonedDateTime test = ZonedDateTime.now(zone);
256 for (int i = 0; i < 100; i++) {
257 if (expected.equals(test)) {
258 return;
259 }
260 expected = ZonedDateTime.now(Clock.system(zone));
261 test = ZonedDateTime.now(zone);
262 }
263 assertEquals(test, expected);
264 }
265
266 //-----------------------------------------------------------------------
267 // now(Clock)
268 //-----------------------------------------------------------------------
269 @Test(expectedExceptions=NullPointerException.class)
270 public void now_Clock_nullClock() {
271 ZonedDateTime.now((Clock) null);
272 }
273
274 @Test
275 public void now_Clock_allSecsInDay_utc() {
276 for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
277 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
278 Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
279 ZonedDateTime test = ZonedDateTime.now(clock);
280 assertEquals(test.getYear(), 1970);
281 assertEquals(test.getMonth(), Month.JANUARY);
282 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2));
283 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 */
243 //-----------------------------------------------------------------------
244 // now(ZoneId)
245 //-----------------------------------------------------------------------
246 @Test(expectedExceptions=NullPointerException.class)
247 public void now_ZoneId_nullZoneId() {
248 ZonedDateTime.now((ZoneId) null);
249 }
250
251 @Test
252 public void now_ZoneId() {
253 ZoneId zone = ZoneId.of("UTC+01:02:03");
254 ZonedDateTime expected = ZonedDateTime.now(Clock.system(zone));
255 ZonedDateTime test = ZonedDateTime.now(zone);
256 for (int i = 0; i < 100; i++) {
257 if (expected.equals(test)) {
258 return;
259 }
260 expected = ZonedDateTime.now(Clock.system(zone));
261 test = ZonedDateTime.now(zone);
262 }
263 assertEquals(test.truncatedTo(ChronoUnit.SECONDS),
264 expected.truncatedTo(ChronoUnit.SECONDS));
265 }
266
267 //-----------------------------------------------------------------------
268 // now(Clock)
269 //-----------------------------------------------------------------------
270 @Test(expectedExceptions=NullPointerException.class)
271 public void now_Clock_nullClock() {
272 ZonedDateTime.now((Clock) null);
273 }
274
275 @Test
276 public void now_Clock_allSecsInDay_utc() {
277 for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
278 Instant instant = Instant.ofEpochSecond(i).plusNanos(123456789L);
279 Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
280 ZonedDateTime test = ZonedDateTime.now(clock);
281 assertEquals(test.getYear(), 1970);
282 assertEquals(test.getMonth(), Month.JANUARY);
283 assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2));
284 assertEquals(test.getHour(), (i / (60 * 60)) % 24);
|