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