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.junit.Assert;
34  import org.junit.Test;
35  
36  public class TestTimeValue {
37  
38      private void checkToDays(final long value, final TimeUnit timeUnit) {
39          Assert.assertEquals(timeUnit.toDays(value), TimeValue.of(value, timeUnit).toDays());
40      }
41  
42      private void checkToHours(final long value, final TimeUnit timeUnit) {
43          Assert.assertEquals(timeUnit.toHours(value), TimeValue.of(value, timeUnit).toHours());
44      }
45  
46      private void checkToMicros(final long value, final TimeUnit timeUnit) {
47          Assert.assertEquals(timeUnit.toMicros(value), TimeValue.of(value, timeUnit).toMicros());
48      }
49  
50      private void checkToMillis(final long value, final TimeUnit timeUnit) {
51          Assert.assertEquals(timeUnit.toMillis(value), TimeValue.of(value, timeUnit).toMillis());
52      }
53  
54      private void checkToMinutes(final long value, final TimeUnit timeUnit) {
55          Assert.assertEquals(timeUnit.toMinutes(value), TimeValue.of(value, timeUnit).toMinutes());
56      }
57  
58      private void checkToNanos(final long value, final TimeUnit timeUnit) {
59          Assert.assertEquals(timeUnit.toNanos(value), TimeValue.of(value, timeUnit).toNanos());
60      }
61  
62      private void checkToSeconds(final long value, final TimeUnit timeUnit) {
63          Assert.assertEquals(timeUnit.toSeconds(value), TimeValue.of(value, timeUnit).toSeconds());
64      }
65  
66      private void test(final long value) {
67          for (final TimeUnit timeUnit : TimeUnit.values()) {
68              checkToDays(value, timeUnit);
69              checkToHours(value, timeUnit);
70              checkToMinutes(value, timeUnit);
71              checkToSeconds(value, timeUnit);
72              checkToMillis(value, timeUnit);
73              checkToMicros(value, timeUnit);
74              checkToNanos(value, timeUnit);
75          }
76      }
77  
78      @Test
79      public void test0() {
80          test(0);
81      }
82  
83      @Test
84      public void test1() {
85          test(1);
86      }
87  
88      private void testFactory(final TimeUnit timeUnit) {
89          Assert.assertEquals(timeUnit, TimeValue.of(1, timeUnit).getTimeUnit());
90      }
91  
92      @Test
93      public void testFactoryForDays() {
94          testFactory(TimeUnit.DAYS);
95      }
96  
97      @Test
98      public void testFactoryForHours() {
99          testFactory(TimeUnit.HOURS);
100     }
101 
102     @Test
103     public void testFactoryForMicroseconds() {
104         testFactory(TimeUnit.MICROSECONDS);
105     }
106 
107     @Test
108     public void testFactoryForMillisseconds() {
109         testFactory(TimeUnit.MILLISECONDS);
110     }
111 
112     @Test
113     public void testFactoryForMinutes() {
114         testFactory(TimeUnit.MINUTES);
115     }
116 
117     @Test
118     public void testFactoryForNanoseconds() {
119         testFactory(TimeUnit.NANOSECONDS);
120     }
121 
122     @Test
123     public void testFactoryForSeconds() {
124         testFactory(TimeUnit.SECONDS);
125     }
126 
127     @Test
128     public void testMaxInt() {
129         test(Integer.MAX_VALUE);
130     }
131 
132     @Test
133     public void testMaxLong() {
134         test(Long.MAX_VALUE);
135     }
136 
137     @Test
138     public void testNegative1() {
139         test(-1);
140     }
141 
142     @Test
143     public void testToString() {
144         Assert.assertEquals("9,223,372,036,854,775,807 SECONDS", TimeValue.ofSeconds(Long.MAX_VALUE).toString());
145         Assert.assertEquals("0 MILLISECONDS", TimeValue.ZERO_MILLISECONDS.toString());
146     }
147 
148     @Test
149     public void testFromString() throws ParseException {
150         Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9,223,372,036,854,775,807 SECONDS"));
151         Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9,223,372,036,854,775,807 Seconds"));
152         Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9,223,372,036,854,775,807  Seconds"));
153         Assert.assertEquals(TimeValue.ofSeconds(Long.MAX_VALUE), TimeValue.parse("9,223,372,036,854,775,807\tSeconds"));
154         Assert.assertEquals(TimeValue.ZERO_MILLISECONDS, TimeValue.parse("0 MILLISECONDS"));
155     }
156 
157 }