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 java.text.ParseException;
31  import java.util.concurrent.TimeUnit;
32  
33  import org.hamcrest.CoreMatchers;
34  import org.hamcrest.MatcherAssert;
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  public class TestTimeValue {
39  
40      private void checkToDays(final long value, final TimeUnit timeUnit) {
41          Assert.assertEquals(timeUnit.toDays(value), TimeValue.of(value, timeUnit).toDays());
42      }
43  
44      private void checkToHours(final long value, final TimeUnit timeUnit) {
45          Assert.assertEquals(timeUnit.toHours(value), TimeValue.of(value, timeUnit).toHours());
46      }
47  
48      private void checkToMicroseconds(final long value, final TimeUnit timeUnit) {
49          Assert.assertEquals(timeUnit.toMicros(value), TimeValue.of(value, timeUnit).toMicroseconds());
50      }
51  
52      private void checkToMilliseconds(final long value, final TimeUnit timeUnit) {
53          Assert.assertEquals(timeUnit.toMillis(value), TimeValue.of(value, timeUnit).toMilliseconds());
54      }
55  
56      private void checkToMinutes(final long value, final TimeUnit timeUnit) {
57          Assert.assertEquals(timeUnit.toMinutes(value), TimeValue.of(value, timeUnit).toMinutes());
58      }
59  
60      private void checkToNanoseconds(final long value, final TimeUnit timeUnit) {
61          Assert.assertEquals(timeUnit.toNanos(value), TimeValue.of(value, timeUnit).toNanoseconds());
62      }
63  
64      private void checkToSeconds(final long value, final TimeUnit timeUnit) {
65          Assert.assertEquals(timeUnit.toSeconds(value), TimeValue.of(value, timeUnit).toSeconds());
66      }
67  
68      private void test(final long value) {
69          for (final TimeUnit timeUnit : TimeUnit.values()) {
70              checkToDays(value, timeUnit);
71              checkToHours(value, timeUnit);
72              checkToMinutes(value, timeUnit);
73              checkToSeconds(value, timeUnit);
74              checkToMilliseconds(value, timeUnit);
75              checkToMicroseconds(value, timeUnit);
76              checkToNanoseconds(value, timeUnit);
77          }
78      }
79  
80      @Test
81      public void test0() {
82          test(0);
83      }
84  
85      @Test
86      public void test1() {
87          test(1);
88      }
89  
90      @Test
91      public void testConvert() {
92          Assert.assertEquals(0, TimeValue.ofMilliseconds(0).convert(TimeUnit.DAYS));
93          Assert.assertEquals(1000, TimeValue.ofSeconds(1).convert(TimeUnit.MILLISECONDS));
94      }
95  
96      @Test
97      public void testDivide() {
98          // nominator is 0, result should be 0.
99          Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toDays());
100         Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toHours());
101         Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMicroseconds());
102         Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMilliseconds());
103         Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMinutes());
104         Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toNanoseconds());
105         Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toSeconds());
106         Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toMillisecondsIntBound());
107         Assert.assertEquals(0, TimeValue.ofMilliseconds(0).divide(2).toSecondsIntBound());
108         //
109         Assert.assertEquals(50, TimeValue.ofMilliseconds(100).divide(2).toMilliseconds());
110         Assert.assertEquals(0, TimeValue.ofMinutes(1).divide(2).toSeconds());
111         Assert.assertEquals(30, TimeValue.ofMinutes(1).divide(2, TimeUnit.SECONDS).toSeconds());
112         Assert.assertEquals(30000, TimeValue.ofMinutes(1).divide(2, TimeUnit.MILLISECONDS).toMilliseconds());
113     }
114 
115     @Test(expected = ArithmeticException.class)
116     public void testDivideBy0() {
117         TimeValue.ofMilliseconds(0).divide(0);
118     }
119 
120     private void testFactory(final TimeUnit timeUnit) {
121         Assert.assertEquals(timeUnit, TimeValue.of(1, timeUnit).getTimeUnit());
122     }
123 
124     @Test
125     public void testFactoryForDays() {
126         testFactory(TimeUnit.DAYS);
127     }
128 
129     @Test
130     public void testFactoryForHours() {
131         testFactory(TimeUnit.HOURS);
132     }
133 
134     @Test
135     public void testFactoryForMicroseconds() {
136         testFactory(TimeUnit.MICROSECONDS);
137     }
138 
139     @Test
140     public void testFactoryForMilliseconds() {
141         testFactory(TimeUnit.MILLISECONDS);
142     }
143 
144     @Test
145     public void testFactoryForMinutes() {
146         testFactory(TimeUnit.MINUTES);
147     }
148 
149     @Test
150     public void testFactoryForNanoseconds() {
151         testFactory(TimeUnit.NANOSECONDS);
152     }
153 
154     @Test
155     public void testFactoryForSeconds() {
156         testFactory(TimeUnit.SECONDS);
157     }
158 
159     @Test
160     public void testMin() {
161         final TimeValue nanos1 = TimeValue.ofNanoseconds(1);
162         final TimeValue micros1 = TimeValue.ofMicroseconds(1);
163         final TimeValue millis1 = TimeValue.ofMilliseconds(1);
164         final TimeValue seconds1 = TimeValue.ofSeconds(1);
165         final TimeValue minutes1 = TimeValue.ofMinutes(1);
166         final TimeValue hours1 = TimeValue.ofHours(1);
167         final TimeValue days1 = TimeValue.ofDays(1);
168         //
169         Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(nanos1));
170         Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(micros1));
171         Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(millis1));
172         Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(seconds1));
173         Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(minutes1));
174         Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(hours1));
175         Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.ZERO_MILLISECONDS.min(days1));
176         //
177         Assert.assertEquals(nanos1, nanos1.min(nanos1));
178         Assert.assertEquals(nanos1, nanos1.min(micros1));
179         Assert.assertEquals(nanos1, nanos1.min(millis1));
180         Assert.assertEquals(nanos1, nanos1.min(seconds1));
181         Assert.assertEquals(nanos1, nanos1.min(minutes1));
182         Assert.assertEquals(nanos1, nanos1.min(hours1));
183         Assert.assertEquals(nanos1, nanos1.min(days1));
184         //
185         Assert.assertEquals(nanos1, micros1.min(nanos1));
186         Assert.assertEquals(micros1, micros1.min(micros1));
187         Assert.assertEquals(micros1, micros1.min(millis1));
188         Assert.assertEquals(micros1, micros1.min(seconds1));
189         Assert.assertEquals(micros1, micros1.min(minutes1));
190         Assert.assertEquals(micros1, micros1.min(hours1));
191         Assert.assertEquals(micros1, micros1.min(days1));
192         //
193         Assert.assertEquals(nanos1, millis1.min(nanos1));
194         Assert.assertEquals(micros1, millis1.min(micros1));
195         Assert.assertEquals(millis1, millis1.min(millis1));
196         Assert.assertEquals(millis1, millis1.min(seconds1));
197         Assert.assertEquals(millis1, millis1.min(minutes1));
198         Assert.assertEquals(millis1, millis1.min(hours1));
199         Assert.assertEquals(millis1, millis1.min(days1));
200         //
201         Assert.assertEquals(nanos1, seconds1.min(nanos1));
202         Assert.assertEquals(micros1, seconds1.min(micros1));
203         Assert.assertEquals(millis1, seconds1.min(millis1));
204         Assert.assertEquals(seconds1, seconds1.min(seconds1));
205         Assert.assertEquals(seconds1, seconds1.min(minutes1));
206         Assert.assertEquals(seconds1, seconds1.min(hours1));
207         Assert.assertEquals(seconds1, seconds1.min(days1));
208         //
209         Assert.assertEquals(nanos1, minutes1.min(nanos1));
210         Assert.assertEquals(micros1, minutes1.min(micros1));
211         Assert.assertEquals(millis1, minutes1.min(millis1));
212         Assert.assertEquals(seconds1, minutes1.min(seconds1));
213         Assert.assertEquals(minutes1, minutes1.min(minutes1));
214         Assert.assertEquals(minutes1, minutes1.min(hours1));
215         Assert.assertEquals(minutes1, minutes1.min(days1));
216         //
217         Assert.assertEquals(nanos1, hours1.min(nanos1));
218         Assert.assertEquals(micros1, hours1.min(micros1));
219         Assert.assertEquals(millis1, hours1.min(millis1));
220         Assert.assertEquals(seconds1, hours1.min(seconds1));
221         Assert.assertEquals(minutes1, hours1.min(minutes1));
222         Assert.assertEquals(hours1, hours1.min(hours1));
223         Assert.assertEquals(hours1, hours1.min(days1));
224         //
225         Assert.assertEquals(nanos1, days1.min(nanos1));
226         Assert.assertEquals(micros1, days1.min(micros1));
227         Assert.assertEquals(millis1, days1.min(millis1));
228         Assert.assertEquals(seconds1, days1.min(seconds1));
229         Assert.assertEquals(minutes1, days1.min(minutes1));
230         Assert.assertEquals(hours1, days1.min(hours1));
231         Assert.assertEquals(days1, days1.min(days1));
232     }
233 
234     @Test
235     public void testMaxInt() {
236         test(Integer.MAX_VALUE);
237     }
238 
239     @Test
240     public void testMaxLong() {
241         test(Long.MAX_VALUE);
242     }
243 
244     @Test
245     public void testNegative1() {
246         test(-1);
247     }
248 
249     @Test
250     public void testToString() {
251         Assert.assertEquals("9223372036854775807 SECONDS", TimeValue.ofSeconds(Long.MAX_VALUE).toString());
252         Assert.assertEquals("0 MILLISECONDS", TimeValue.ZERO_MILLISECONDS.toString());
253     }
254 
255     @Test
256     public void testFromString() throws ParseException {
257         Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9223372036854775807 SECONDS"));
258         Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9223372036854775807 SECONDS"));
259         Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse(" 9223372036854775807 SECONDS "));
260         Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9223372036854775807 Seconds"));
261         Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9223372036854775807  Seconds"));
262         Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9223372036854775807\tSeconds"));
263         Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.parse("0 MILLISECONDS"));
264         Assert.assertEquals(TimeValue.ofMilliseconds(1), TimeValue.parse("1 MILLISECOND"));
265     }
266 
267     @Test
268     public void testEqualsAndHashCode() throws ParseException {
269         final TimeValue tv1 = TimeValue.ofMilliseconds(1000L);
270         final TimeValue tv2 = TimeValue.ofMilliseconds(1001L);
271         final TimeValue tv3 = TimeValue.ofMilliseconds(1000L);
272         final TimeValue tv4 = TimeValue.ofSeconds(1L);
273         final TimeValue tv5 = TimeValue.ofSeconds(1000L);
274 
275         MatcherAssert.assertThat(tv1.equals(tv1), CoreMatchers.equalTo(true));
276         MatcherAssert.assertThat(tv1.equals(null), CoreMatchers.equalTo(false));
277         MatcherAssert.assertThat(tv1.equals(tv2), CoreMatchers.equalTo(false));
278         MatcherAssert.assertThat(tv1.equals(tv3), CoreMatchers.equalTo(true));
279         MatcherAssert.assertThat(tv1.equals(tv4), CoreMatchers.equalTo(true));
280         MatcherAssert.assertThat(tv4.equals(tv1), CoreMatchers.equalTo(true));
281         MatcherAssert.assertThat(tv1.equals(tv5), CoreMatchers.equalTo(false));
282 
283         MatcherAssert.assertThat(tv1.hashCode() == tv2.hashCode(), CoreMatchers.equalTo(false));
284         MatcherAssert.assertThat(tv1.hashCode() == tv3.hashCode(), CoreMatchers.equalTo(true));
285         MatcherAssert.assertThat(tv1.hashCode() == tv4.hashCode(), CoreMatchers.equalTo(true));
286         MatcherAssert.assertThat(tv4.hashCode() == tv1.hashCode(), CoreMatchers.equalTo(true));
287         MatcherAssert.assertThat(tv1.hashCode() == tv5.hashCode(), CoreMatchers.equalTo(false));
288     }
289 
290     @Test
291     public void testCompareTo() throws ParseException {
292         final TimeValue tv1 = TimeValue.ofMilliseconds(1000L);
293         final TimeValue tv2 = TimeValue.ofMilliseconds(1001L);
294         final TimeValue tv3 = TimeValue.ofMilliseconds(1000L);
295         final TimeValue tv4 = TimeValue.ofSeconds(1L);
296         final TimeValue tv5 = TimeValue.ofSeconds(60L);
297         final TimeValue tv6 = TimeValue.ofMinutes(1L);
298 
299         MatcherAssert.assertThat(tv1.compareTo(tv1) == 0, CoreMatchers.equalTo(true));
300         MatcherAssert.assertThat(tv1.compareTo(tv2) < 0, CoreMatchers.equalTo(true));
301         MatcherAssert.assertThat(tv1.compareTo(tv3) == 0, CoreMatchers.equalTo(true));
302         MatcherAssert.assertThat(tv1.compareTo(tv4) == 0, CoreMatchers.equalTo(true));
303         MatcherAssert.assertThat(tv1.compareTo(tv5) < 0, CoreMatchers.equalTo(true));
304         MatcherAssert.assertThat(tv6.compareTo(tv5) == 0, CoreMatchers.equalTo(true));
305         MatcherAssert.assertThat(tv6.compareTo(tv4) > 0, CoreMatchers.equalTo(true));
306         try {
307             tv1.compareTo(null);
308             Assert.fail("NullPointerException expected");
309         } catch (final NullPointerException expected) {
310         }
311     }
312 
313 }