View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.util;
29  
30  import static org.hamcrest.MatcherAssert.assertThat;
31  
32  import java.text.ParseException;
33  import java.time.Duration;
34  import java.util.concurrent.TimeUnit;
35  
36  import org.hamcrest.CoreMatchers;
37  import org.junit.jupiter.api.Assertions;
38  import org.junit.jupiter.api.Test;
39  
40  public class TestTimeValue {
41  
42      private void checkToDays(final long value, final TimeUnit timeUnit) {
43          Assertions.assertEquals(timeUnit.toDays(value), TimeValue.of(value, timeUnit).toDays());
44      }
45  
46      private void checkToHours(final long value, final TimeUnit timeUnit) {
47          Assertions.assertEquals(timeUnit.toHours(value), TimeValue.of(value, timeUnit).toHours());
48      }
49  
50      private void checkToMicroseconds(final long value, final TimeUnit timeUnit) {
51          Assertions.assertEquals(timeUnit.toMicros(value), TimeValue.of(value, timeUnit).toMicroseconds());
52      }
53  
54      private void checkToMilliseconds(final long value, final TimeUnit timeUnit) {
55          Assertions.assertEquals(timeUnit.toMillis(value), TimeValue.of(value, timeUnit).toMilliseconds());
56      }
57  
58      private void checkToMinutes(final long value, final TimeUnit timeUnit) {
59          Assertions.assertEquals(timeUnit.toMinutes(value), TimeValue.of(value, timeUnit).toMinutes());
60      }
61  
62      private void checkToNanoseconds(final long value, final TimeUnit timeUnit) {
63          Assertions.assertEquals(timeUnit.toNanos(value), TimeValue.of(value, timeUnit).toNanoseconds());
64      }
65  
66      private void checkToSeconds(final long value, final TimeUnit timeUnit) {
67          Assertions.assertEquals(timeUnit.toSeconds(value), TimeValue.of(value, timeUnit).toSeconds());
68      }
69  
70      private void test(final long value) {
71          for (final TimeUnit timeUnit : TimeUnit.values()) {
72              checkToDays(value, timeUnit);
73              checkToHours(value, timeUnit);
74              checkToMinutes(value, timeUnit);
75              checkToSeconds(value, timeUnit);
76              checkToMilliseconds(value, timeUnit);
77              checkToMicroseconds(value, timeUnit);
78              checkToNanoseconds(value, timeUnit);
79          }
80      }
81  
82      @Test
83      public void test0() {
84          test(0);
85      }
86  
87      @Test
88      public void test1() {
89          test(1);
90      }
91  
92      @Test
93      public void testConvert() {
94          Assertions.assertEquals(0, TimeValue.ofMilliseconds(0).convert(TimeUnit.DAYS));
95          Assertions.assertEquals(1000, TimeValue.ofSeconds(1).convert(TimeUnit.MILLISECONDS));
96      }
97  
98      @Test
99      public void testDivide() {
100         // nominator is 0, result should be 0.
101         Assertions.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toDays());
102         Assertions.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toHours());
103         Assertions.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMicroseconds());
104         Assertions.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMilliseconds());
105         Assertions.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMinutes());
106         Assertions.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toNanoseconds());
107         Assertions.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toSeconds());
108         Assertions.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMillisecondsIntBound());
109         Assertions.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toSecondsIntBound());
110         //
111         Assertions.assertEquals(50, TimeValue.ofMilliseconds(100).divide(2).toMilliseconds());
112         Assertions.assertEquals(0, TimeValue.ofMinutes(1).divide(2).toSeconds());
113         Assertions.assertEquals(30, TimeValue.ofMinutes(1).divide(2, TimeUnit.SECONDS).toSeconds());
114         Assertions.assertEquals(30000, TimeValue.ofMinutes(1).divide(2, TimeUnit.MILLISECONDS).toMilliseconds());
115     }
116 
117     @Test
118     public void testDivideBy0() {
119         Assertions.assertThrows(ArithmeticException.class, () ->
120                 TimeValue.ofMilliseconds(0).divide(0));
121     }
122 
123     private void testFactory(final TimeUnit timeUnit) {
124         Assertions.assertEquals(timeUnit, TimeValue.of(1, timeUnit).getTimeUnit());
125         //
126         final Duration duration = Duration.of(1, TimeValue.toChronoUnit(timeUnit));
127         assertConvertion(duration);
128     }
129 
130     @Test
131     public void testFactoryForDays() {
132         testFactory(TimeUnit.DAYS);
133     }
134 
135     @Test
136     public void testFactoryForDuration() {
137         assertConvertion(Duration.ZERO);
138         assertConvertion(Duration.ofDays(1));
139         assertConvertion(Duration.ofHours(1));
140         assertConvertion(Duration.ofMillis(1));
141         assertConvertion(Duration.ofNanos(1));
142         assertConvertion(Duration.ofSeconds(1));
143         assertConvertion(Duration.ofSeconds(1, 1));
144     }
145 
146     private void assertConvertion(final Duration duration) {
147         Assertions.assertEquals(duration, TimeValue.of(duration).toDuration());
148     }
149 
150     @Test
151     public void testFactoryForHours() {
152         testFactory(TimeUnit.HOURS);
153     }
154 
155     @Test
156     public void testFactoryForMicroseconds() {
157         testFactory(TimeUnit.MICROSECONDS);
158     }
159 
160     @Test
161     public void testFactoryForMilliseconds() {
162         testFactory(TimeUnit.MILLISECONDS);
163     }
164 
165     @Test
166     public void testFactoryForMinutes() {
167         testFactory(TimeUnit.MINUTES);
168     }
169 
170     @Test
171     public void testFactoryForNanoseconds() {
172         testFactory(TimeUnit.NANOSECONDS);
173     }
174 
175     @Test
176     public void testFactoryForSeconds() {
177         testFactory(TimeUnit.SECONDS);
178     }
179 
180     @Test
181     public void testMin() {
182         final TimeValue nanos1 = TimeValue.ofNanoseconds(1);
183         final TimeValue micros1 = TimeValue.ofMicroseconds(1);
184         final TimeValue millis1 = TimeValue.ofMilliseconds(1);
185         final TimeValue seconds1 = TimeValue.ofSeconds(1);
186         final TimeValue minutes1 = TimeValue.ofMinutes(1);
187         final TimeValue hours1 = TimeValue.ofHours(1);
188         final TimeValue days1 = TimeValue.ofDays(1);
189         //
190         Assertions.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(nanos1));
191         Assertions.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(micros1));
192         Assertions.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(millis1));
193         Assertions.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(seconds1));
194         Assertions.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(minutes1));
195         Assertions.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(hours1));
196         Assertions.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(days1));
197         //
198         Assertions.assertEquals(nanos1, nanos1.min(nanos1));
199         Assertions.assertEquals(nanos1, nanos1.min(micros1));
200         Assertions.assertEquals(nanos1, nanos1.min(millis1));
201         Assertions.assertEquals(nanos1, nanos1.min(seconds1));
202         Assertions.assertEquals(nanos1, nanos1.min(minutes1));
203         Assertions.assertEquals(nanos1, nanos1.min(hours1));
204         Assertions.assertEquals(nanos1, nanos1.min(days1));
205         //
206         Assertions.assertEquals(nanos1, micros1.min(nanos1));
207         Assertions.assertEquals(micros1, micros1.min(micros1));
208         Assertions.assertEquals(micros1, micros1.min(millis1));
209         Assertions.assertEquals(micros1, micros1.min(seconds1));
210         Assertions.assertEquals(micros1, micros1.min(minutes1));
211         Assertions.assertEquals(micros1, micros1.min(hours1));
212         Assertions.assertEquals(micros1, micros1.min(days1));
213         //
214         Assertions.assertEquals(nanos1, millis1.min(nanos1));
215         Assertions.assertEquals(micros1, millis1.min(micros1));
216         Assertions.assertEquals(millis1, millis1.min(millis1));
217         Assertions.assertEquals(millis1, millis1.min(seconds1));
218         Assertions.assertEquals(millis1, millis1.min(minutes1));
219         Assertions.assertEquals(millis1, millis1.min(hours1));
220         Assertions.assertEquals(millis1, millis1.min(days1));
221         //
222         Assertions.assertEquals(nanos1, seconds1.min(nanos1));
223         Assertions.assertEquals(micros1, seconds1.min(micros1));
224         Assertions.assertEquals(millis1, seconds1.min(millis1));
225         Assertions.assertEquals(seconds1, seconds1.min(seconds1));
226         Assertions.assertEquals(seconds1, seconds1.min(minutes1));
227         Assertions.assertEquals(seconds1, seconds1.min(hours1));
228         Assertions.assertEquals(seconds1, seconds1.min(days1));
229         //
230         Assertions.assertEquals(nanos1, minutes1.min(nanos1));
231         Assertions.assertEquals(micros1, minutes1.min(micros1));
232         Assertions.assertEquals(millis1, minutes1.min(millis1));
233         Assertions.assertEquals(seconds1, minutes1.min(seconds1));
234         Assertions.assertEquals(minutes1, minutes1.min(minutes1));
235         Assertions.assertEquals(minutes1, minutes1.min(hours1));
236         Assertions.assertEquals(minutes1, minutes1.min(days1));
237         //
238         Assertions.assertEquals(nanos1, hours1.min(nanos1));
239         Assertions.assertEquals(micros1, hours1.min(micros1));
240         Assertions.assertEquals(millis1, hours1.min(millis1));
241         Assertions.assertEquals(seconds1, hours1.min(seconds1));
242         Assertions.assertEquals(minutes1, hours1.min(minutes1));
243         Assertions.assertEquals(hours1, hours1.min(hours1));
244         Assertions.assertEquals(hours1, hours1.min(days1));
245         //
246         Assertions.assertEquals(nanos1, days1.min(nanos1));
247         Assertions.assertEquals(micros1, days1.min(micros1));
248         Assertions.assertEquals(millis1, days1.min(millis1));
249         Assertions.assertEquals(seconds1, days1.min(seconds1));
250         Assertions.assertEquals(minutes1, days1.min(minutes1));
251         Assertions.assertEquals(hours1, days1.min(hours1));
252         Assertions.assertEquals(days1, days1.min(days1));
253     }
254 
255     @Test
256     public void testMaxInt() {
257         test(Integer.MAX_VALUE);
258     }
259 
260     @Test
261     public void testMaxLong() {
262         test(Long.MAX_VALUE);
263     }
264 
265     @Test
266     public void testNegative1() {
267         test(-1);
268     }
269 
270     @Test
271     public void testToString() {
272         Assertions.assertEquals("9223372036854775807 SECONDS", TimeValue.ofSeconds(Long.MAX_VALUE).toString());
273         Assertions.assertEquals("0 MILLISECONDS", TimeValue.ZERO_MILLISECONDS.toString());
274     }
275 
276     @Test
277     public void testFromString() throws ParseException {
278         final TimeValue maxSeconds = TimeValue.ofSeconds(Long.MAX_VALUE);
279         Assertions.assertEquals(maxSeconds, TimeValue.parse("9223372036854775807 SECONDS"));
280         Assertions.assertEquals(maxSeconds, TimeValue.parse("9223372036854775807 SECONDS"));
281         Assertions.assertEquals(maxSeconds, TimeValue.parse(" 9223372036854775807 SECONDS "));
282         Assertions.assertEquals(maxSeconds, TimeValue.parse("9223372036854775807 Seconds"));
283         Assertions.assertEquals(maxSeconds, TimeValue.parse("9223372036854775807  Seconds"));
284         Assertions.assertEquals(maxSeconds, TimeValue.parse("9223372036854775807\tSeconds"));
285         Assertions.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.parse("0 MILLISECONDS"));
286         Assertions.assertEquals(TimeValue.ofMilliseconds(1), TimeValue.parse("1 MILLISECOND"));
287     }
288 
289     @Test
290     public void testToDuration() throws ParseException {
291         Assertions.assertEquals(Long.MAX_VALUE, TimeValue.parse("9223372036854775807 SECONDS").toDuration().getSeconds());
292     }
293 
294     @Test
295     public void testEqualsAndHashCode() {
296         final TimeValue tv1 = TimeValue.ofMilliseconds(1000L);
297         final TimeValue tv2 = TimeValue.ofMilliseconds(1001L);
298         final TimeValue tv3 = TimeValue.ofMilliseconds(1000L);
299         final TimeValue tv4 = TimeValue.ofSeconds(1L);
300         final TimeValue tv5 = TimeValue.ofSeconds(1000L);
301 
302         assertThat(tv1.equals(tv1), CoreMatchers.equalTo(true));
303         assertThat(tv1.equals(null), CoreMatchers.equalTo(false));
304         assertThat(tv1.equals(tv2), CoreMatchers.equalTo(false));
305         assertThat(tv1.equals(tv3), CoreMatchers.equalTo(true));
306         assertThat(tv1.equals(tv4), CoreMatchers.equalTo(true));
307         assertThat(tv4.equals(tv1), CoreMatchers.equalTo(true));
308         assertThat(tv1.equals(tv5), CoreMatchers.equalTo(false));
309 
310         assertThat(tv1.hashCode() == tv2.hashCode(), CoreMatchers.equalTo(false));
311         assertThat(tv1.hashCode() == tv3.hashCode(), CoreMatchers.equalTo(true));
312         assertThat(tv1.hashCode() == tv4.hashCode(), CoreMatchers.equalTo(true));
313         assertThat(tv4.hashCode() == tv1.hashCode(), CoreMatchers.equalTo(true));
314         assertThat(tv1.hashCode() == tv5.hashCode(), CoreMatchers.equalTo(false));
315     }
316 
317     @Test
318     public void testCompareTo() {
319         final TimeValue tv1 = TimeValue.ofMilliseconds(1000L);
320         final TimeValue tv2 = TimeValue.ofMilliseconds(1001L);
321         final TimeValue tv3 = TimeValue.ofMilliseconds(1000L);
322         final TimeValue tv4 = TimeValue.ofSeconds(1L);
323         final TimeValue tv5 = TimeValue.ofSeconds(60L);
324         final TimeValue tv6 = TimeValue.ofMinutes(1L);
325 
326         assertThat(tv1.compareTo(tv1) == 0, CoreMatchers.equalTo(true));
327         assertThat(tv1.compareTo(tv2) < 0, CoreMatchers.equalTo(true));
328         assertThat(tv1.compareTo(tv3) == 0, CoreMatchers.equalTo(true));
329         assertThat(tv1.compareTo(tv4) == 0, CoreMatchers.equalTo(true));
330         assertThat(tv1.compareTo(tv5) < 0, CoreMatchers.equalTo(true));
331         assertThat(tv6.compareTo(tv5) == 0, CoreMatchers.equalTo(true));
332         assertThat(tv6.compareTo(tv4) > 0, CoreMatchers.equalTo(true));
333         Assertions.assertThrows(NullPointerException.class, () -> tv1.compareTo(null));
334     }
335 
336 }