1 /* 2 * Copyright (c) 2021, 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 package test.java.time; 24 25 import static java.time.temporal.ChronoUnit.SECONDS; 26 import static org.testng.Assert.assertEquals; 27 import static org.testng.Assert.assertSame; 28 import static org.testng.Assert.assertTrue; 29 30 import java.time.Clock; 31 import java.time.Duration; 32 import java.time.Instant; 33 import java.time.InstantSource; 34 import java.time.ZoneId; 35 import java.time.ZoneOffset; 36 37 import org.testng.annotations.Test; 38 39 /** 40 * Test instant source. 41 */ 42 @Test 43 public class TestInstantSource { 44 45 private static final ZoneId PARIS = ZoneId.of("Europe/Paris"); 46 test_system()47 public void test_system() { 48 // main tests for Clock.currentInstant() are in TestClock_System 49 var test = InstantSource.system(); 50 assertSame(test.withZone(ZoneOffset.UTC), Clock.systemUTC()); 51 assertEquals(test.withZone(PARIS), Clock.system(PARIS)); 52 var millis = System.currentTimeMillis(); 53 var testMillis = test.millis(); 54 var testInstantMillis = test.instant().toEpochMilli(); 55 assertTrue(Math.abs(testMillis - millis) < 1000); 56 assertTrue(Math.abs(testInstantMillis - millis) < 1000); 57 assertSame(test, InstantSource.system()); 58 assertEquals(test.hashCode(), InstantSource.system().hashCode()); 59 assertEquals(test.toString(), "SystemInstantSource"); 60 } 61 62 public void test_tick() { 63 var millis = 257265861691L; 64 var instant = Instant.ofEpochMilli(millis); 65 var duration = Duration.ofSeconds(1); 66 var test = InstantSource.tick(InstantSource.fixed(instant), duration); 67 assertEquals(test.withZone(ZoneOffset.UTC), Clock.tick(Clock.fixed(instant, ZoneOffset.UTC), duration)); 68 assertEquals(test.withZone(PARIS), Clock.tick(Clock.fixed(instant, PARIS), duration)); 69 assertEquals(test.millis(), (millis / 1000) * 1000); 70 assertEquals(test.instant(), instant.truncatedTo(SECONDS)); 71 assertEquals(test, InstantSource.tick(InstantSource.fixed(instant), duration)); 72 assertEquals(test.hashCode(), InstantSource.tick(InstantSource.fixed(instant), duration).hashCode()); 73 } 74 75 public void test_fixed() { 76 var millis = 257265861691L; 77 var instant = Instant.ofEpochMilli(millis); 78 var test = InstantSource.fixed(instant); 79 assertEquals(test.withZone(ZoneOffset.UTC), Clock.fixed(instant, ZoneOffset.UTC)); 80 assertEquals(test.withZone(PARIS), Clock.fixed(instant, PARIS)); 81 assertEquals(test.millis(), millis); 82 assertEquals(test.instant(), instant); 83 assertEquals(test, InstantSource.fixed(instant)); 84 assertEquals(test.hashCode(), InstantSource.fixed(instant).hashCode()); 85 } 86 87 public void test_offset() { 88 var millis = 257265861691L; 89 var instant = Instant.ofEpochMilli(millis); 90 var duration = Duration.ofSeconds(120); 91 var test = InstantSource.offset(InstantSource.fixed(instant), duration); 92 assertEquals(test.withZone(ZoneOffset.UTC), Clock.offset(Clock.fixed(instant, ZoneOffset.UTC), duration)); 93 assertEquals(test.withZone(PARIS), Clock.offset(Clock.fixed(instant, PARIS), duration)); 94 assertEquals(test.millis(), millis + 120_000); 95 assertEquals(test.instant(), instant.plusSeconds(120)); 96 assertEquals(test, InstantSource.offset(InstantSource.fixed(instant), duration)); 97 assertEquals(test.hashCode(), InstantSource.offset(InstantSource.fixed(instant), duration).hashCode()); 98 } 99 100 static class MockInstantSource implements InstantSource { 101 static final Instant FIXED = Instant.now(); 102 103 @Override 104 public Instant instant() { 105 return FIXED; 106 } 107 } 108 109 public void test_mock() { 110 var test = new MockInstantSource(); 111 assertEquals(test.withZone(ZoneOffset.UTC).getZone(), ZoneOffset.UTC); 112 assertEquals(test.withZone(PARIS).getZone(), PARIS); 113 assertEquals(test.withZone(ZoneOffset.UTC).withZone(PARIS).getZone(), PARIS); 114 assertEquals(test.millis(), MockInstantSource.FIXED.toEpochMilli()); 115 assertEquals(test.instant(), MockInstantSource.FIXED); 116 assertEquals(test.withZone(ZoneOffset.UTC), test.withZone(ZoneOffset.UTC)); 117 assertEquals(test.withZone(ZoneOffset.UTC).hashCode(), test.withZone(ZoneOffset.UTC).hashCode()); 118 } 119 120 } 121