View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.commons.time;
18  
19  import static org.junit.jupiter.api.Assertions.*;
20  
21  import java.time.*;
22  import java.time.temporal.ChronoUnit;
23  
24  import org.apache.juneau.*;
25  import org.junit.jupiter.api.*;
26  
27  /**
28   * Tests for {@link TimeProvider}.
29   */
30  class TimeProvider_Test extends TestBase {
31  
32  	//====================================================================================================
33  	// INSTANCE field tests
34  	//====================================================================================================
35  
36  	@Test
37  	void a01_instanceNotNull() {
38  		assertNotNull(TimeProvider.INSTANCE);
39  	}
40  
41  	//====================================================================================================
42  	// getSystemDefaultZoneId() tests
43  	//====================================================================================================
44  
45  	@Test
46  	void b01_getSystemDefaultZoneId() {
47  		ZoneId expected = ZoneId.systemDefault();
48  		ZoneId actual = TimeProvider.INSTANCE.getSystemDefaultZoneId();
49  		assertEquals(expected, actual);
50  	}
51  
52  	@Test
53  	void b02_getSystemDefaultZoneId_notNull() {
54  		ZoneId zoneId = TimeProvider.INSTANCE.getSystemDefaultZoneId();
55  		assertNotNull(zoneId);
56  	}
57  
58  	//====================================================================================================
59  	// now() tests
60  	//====================================================================================================
61  
62  	@Test
63  	void c01_now() {
64  		ZonedDateTime now = TimeProvider.INSTANCE.now();
65  		assertNotNull(now);
66  		assertEquals(ZoneId.systemDefault(), now.getZone());
67  	}
68  
69  	@Test
70  	void c02_now_isRecent() {
71  		ZonedDateTime before = ZonedDateTime.now();
72  		ZonedDateTime now = TimeProvider.INSTANCE.now();
73  		ZonedDateTime after = ZonedDateTime.now();
74  
75  		// The time should be between before and after (with some tolerance)
76  		assertTrue(now.isAfter(before.minusSeconds(1)) || now.isEqual(before.minusSeconds(1)));
77  		assertTrue(now.isBefore(after.plusSeconds(1)) || now.isEqual(after.plusSeconds(1)));
78  	}
79  
80  	@Test
81  	void c03_now_usesSystemDefaultZone() {
82  		ZonedDateTime now = TimeProvider.INSTANCE.now();
83  		assertEquals(ZoneId.systemDefault(), now.getZone());
84  	}
85  
86  	//====================================================================================================
87  	// now(ZoneId) tests
88  	//====================================================================================================
89  
90  	@Test
91  	void d01_nowWithZoneId() {
92  		ZoneId utc = ZoneId.of("UTC");
93  		ZonedDateTime now = TimeProvider.INSTANCE.now(utc);
94  		assertNotNull(now);
95  		assertEquals(utc, now.getZone());
96  	}
97  
98  	@Test
99  	void d02_nowWithZoneId_usesSpecifiedZone() {
100 		ZoneId newYork = ZoneId.of("America/New_York");
101 		ZonedDateTime now = TimeProvider.INSTANCE.now(newYork);
102 		assertEquals(newYork, now.getZone());
103 	}
104 
105 	@Test
106 	void d03_nowWithZoneId_isRecent() {
107 		ZoneId utc = ZoneId.of("UTC");
108 		ZonedDateTime before = ZonedDateTime.now(utc);
109 		ZonedDateTime now = TimeProvider.INSTANCE.now(utc);
110 		ZonedDateTime after = ZonedDateTime.now(utc);
111 
112 		// The time should be between before and after (with some tolerance)
113 		assertTrue(now.isAfter(before.minusSeconds(1)) || now.isEqual(before.minusSeconds(1)));
114 		assertTrue(now.isBefore(after.plusSeconds(1)) || now.isEqual(after.plusSeconds(1)));
115 	}
116 
117 	@Test
118 	void d04_nowWithZoneId_differentZones() {
119 		ZoneId utc = ZoneId.of("UTC");
120 		ZoneId newYork = ZoneId.of("America/New_York");
121 
122 		ZonedDateTime utcTime = TimeProvider.INSTANCE.now(utc);
123 		ZonedDateTime nyTime = TimeProvider.INSTANCE.now(newYork);
124 
125 		// Both should represent the same instant, just in different zones
126 		Instant utcInstant = utcTime.toInstant();
127 		Instant nyInstant = nyTime.toInstant();
128 
129 		// They should be very close (within 1 second)
130 		long diffSeconds = Math.abs(ChronoUnit.SECONDS.between(utcInstant, nyInstant));
131 		assertTrue(diffSeconds < 2, "Times should be within 1 second of each other");
132 	}
133 
134 	@Test
135 	void d05_nowWithZoneId_nullThrowsException() {
136 		assertThrows(NullPointerException.class, () -> {
137 			TimeProvider.INSTANCE.now(null);
138 		});
139 	}
140 
141 	//====================================================================================================
142 	// Custom instance tests
143 	//====================================================================================================
144 
145 	@Test
146 	void e01_customInstance() {
147 		TimeProvider provider = new TimeProvider();
148 		assertNotNull(provider);
149 		assertNotNull(provider.getSystemDefaultZoneId());
150 		assertNotNull(provider.now());
151 	}
152 
153 	@Test
154 	void e02_customInstance_behaviorMatchesInstance() {
155 		TimeProvider custom = new TimeProvider();
156 		ZoneId customZone = custom.getSystemDefaultZoneId();
157 		ZoneId instanceZone = TimeProvider.INSTANCE.getSystemDefaultZoneId();
158 		assertEquals(instanceZone, customZone);
159 	}
160 }
161