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 TestTimeout {
37  
38      private void checkToDays(final long value, final TimeUnit timeUnit) {
39          Assert.assertEquals(timeUnit.toDays(value), Timeout.of(value, timeUnit).toDays());
40      }
41  
42      private void checkToHours(final long value, final TimeUnit timeUnit) {
43          Assert.assertEquals(timeUnit.toHours(value), Timeout.of(value, timeUnit).toHours());
44      }
45  
46      private void checkToMicros(final long value, final TimeUnit timeUnit) {
47          Assert.assertEquals(timeUnit.toMicros(value), Timeout.of(value, timeUnit).toMicros());
48      }
49  
50      private void checkToMillis(final long value, final TimeUnit timeUnit) {
51          Assert.assertEquals(timeUnit.toMillis(value), Timeout.of(value, timeUnit).toMillis());
52      }
53  
54      private void checkToMinutes(final long value, final TimeUnit timeUnit) {
55          Assert.assertEquals(timeUnit.toMinutes(value), Timeout.of(value, timeUnit).toMinutes());
56      }
57  
58      private void checkToNanos(final long value, final TimeUnit timeUnit) {
59          Assert.assertEquals(timeUnit.toNanos(value), Timeout.of(value, timeUnit).toNanos());
60      }
61  
62      private void checkToSeconds(final long value, final TimeUnit timeUnit) {
63          Assert.assertEquals(timeUnit.toSeconds(value), Timeout.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      @Test
89      public void testDisabled() {
90          Assert.assertTrue(Timeout.DISABLED.isDisabled());
91          Assert.assertFalse(Timeout.DISABLED.isEnabled());
92      }
93  
94      private void testFactory(final TimeUnit timeUnit) {
95          Assert.assertEquals(timeUnit, Timeout.of(1, timeUnit).getTimeUnit());
96      }
97  
98      @Test
99      public void testFactoryForDays() {
100         testFactory(TimeUnit.DAYS);
101     }
102 
103     @Test
104     public void testFactoryForHours() {
105         testFactory(TimeUnit.HOURS);
106     }
107 
108     @Test
109     public void testFactoryForMicroseconds() {
110         testFactory(TimeUnit.MICROSECONDS);
111     }
112 
113     @Test
114     public void testFactoryForMillisseconds() {
115         testFactory(TimeUnit.MILLISECONDS);
116     }
117 
118     @Test
119     public void testFactoryForMinutes() {
120         testFactory(TimeUnit.MINUTES);
121     }
122 
123     @Test
124     public void testFactoryForNanoseconds() {
125         testFactory(TimeUnit.NANOSECONDS);
126     }
127 
128     @Test
129     public void testFactoryForSeconds() {
130         testFactory(TimeUnit.SECONDS);
131     }
132 
133     @Test
134     public void testMaxInt() {
135         test(Integer.MAX_VALUE);
136     }
137 
138     @Test
139     public void testMaxLong() {
140         test(Long.MAX_VALUE);
141     }
142 
143     @Test(expected = IllegalArgumentException.class)
144     public void testNegative1() {
145         test(-1);
146     }
147 
148     @Test
149     public void testToString() {
150         Assert.assertEquals("9,223,372,036,854,775,807 SECONDS", Timeout.ofSeconds(Long.MAX_VALUE).toString());
151         Assert.assertEquals("0 MILLISECONDS", Timeout.ZERO_MILLISECONDS.toString());
152     }
153 
154     @Test
155     public void testFromString() throws ParseException {
156         Assert.assertEquals(Timeout.ofSeconds(Long.MAX_VALUE), Timeout.parse("9,223,372,036,854,775,807 SECONDS"));
157         Assert.assertEquals(Timeout.ofSeconds(Long.MAX_VALUE), Timeout.parse("9,223,372,036,854,775,807 Seconds"));
158         Assert.assertEquals(Timeout.ofSeconds(Long.MAX_VALUE), Timeout.parse("9,223,372,036,854,775,807  Seconds"));
159         Assert.assertEquals(Timeout.ofSeconds(Long.MAX_VALUE), Timeout.parse("9,223,372,036,854,775,807\tSeconds"));
160         Assert.assertEquals(Timeout.ZERO_MILLISECONDS, Timeout.parse("0 MILLISECONDS"));
161     }
162 
163 }