View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.pool2.impl;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.time.Duration;
22  import java.time.Instant;
23  import java.time.temporal.ChronoUnit;
24  import java.util.concurrent.TimeUnit;
25  
26  import org.apache.commons.pool2.BasePooledObjectFactory;
27  import org.apache.commons.pool2.PooledObject;
28  import org.junit.jupiter.api.Test;
29  
30  public class TestPoolImplUtils {
31  
32      @SuppressWarnings("unused")
33      private abstract static class FactoryAB<A, B> extends BasePooledObjectFactory<B> {
34          // empty by design
35      }
36  
37      private abstract static class FactoryBA<A, B> extends FactoryAB<B, A> {
38          // empty by design
39      }
40  
41      private abstract static class FactoryC<C> extends FactoryBA<C, String> {
42          // empty by design
43      }
44  
45      @SuppressWarnings("unused")
46      private abstract static class FactoryDE<D, E> extends FactoryC<D> {
47          // empty by design
48      }
49  
50      private abstract static class FactoryF<F> extends FactoryDE<Long, F> {
51          // empty by design
52      }
53  
54      private static class NotSimpleFactory extends FactoryF<Integer> {
55          @Override
56          public Long create() throws Exception {
57              return null;
58          }
59  
60          @Override
61          public PooledObject<Long> wrap(final Long obj) {
62              return null;
63          }
64      }
65  
66      private static class SimpleFactory extends BasePooledObjectFactory<String> {
67          @Override
68          public String create() {
69              return null;
70          }
71  
72          @Override
73          public PooledObject<String> wrap(final String obj) {
74              return null;
75          }
76      }
77  
78      private static final Instant INSTANT_1 = Instant.ofEpochMilli(1);
79  
80      private static final Instant INSTANT_0 = Instant.ofEpochMilli(0);
81  
82      @Test
83      public void testFactoryTypeNotSimple() {
84          final Class<?> result = PoolImplUtils.getFactoryType(NotSimpleFactory.class);
85          assertEquals(Long.class, result);
86      }
87  
88      @Test
89      public void testFactoryTypeSimple() {
90          final Class<?> result = PoolImplUtils.getFactoryType(SimpleFactory.class);
91          assertEquals(String.class, result);
92      }
93  
94      @Test
95      public void testMaxInstants() {
96          assertEquals(INSTANT_1, PoolImplUtils.max(INSTANT_0, INSTANT_1));
97          assertEquals(INSTANT_1, PoolImplUtils.max(INSTANT_1, INSTANT_0));
98          assertEquals(INSTANT_1, PoolImplUtils.max(INSTANT_1, INSTANT_1));
99          assertEquals(INSTANT_0, PoolImplUtils.max(INSTANT_0, INSTANT_0));
100     }
101 
102     @Test
103     public void testMinInstants() {
104         assertEquals(INSTANT_0, PoolImplUtils.min(INSTANT_0, INSTANT_1));
105         assertEquals(INSTANT_0, PoolImplUtils.min(INSTANT_1, INSTANT_0));
106         assertEquals(INSTANT_1, PoolImplUtils.min(INSTANT_1, INSTANT_1));
107         assertEquals(INSTANT_0, PoolImplUtils.min(INSTANT_0, INSTANT_0));
108     }
109 
110     @Test
111     public void testToChronoUnit() {
112         assertEquals(ChronoUnit.NANOS, PoolImplUtils.toChronoUnit(TimeUnit.NANOSECONDS));
113         assertEquals(ChronoUnit.MICROS, PoolImplUtils.toChronoUnit(TimeUnit.MICROSECONDS));
114         assertEquals(ChronoUnit.MILLIS, PoolImplUtils.toChronoUnit(TimeUnit.MILLISECONDS));
115         assertEquals(ChronoUnit.SECONDS, PoolImplUtils.toChronoUnit(TimeUnit.SECONDS));
116         assertEquals(ChronoUnit.MINUTES, PoolImplUtils.toChronoUnit(TimeUnit.MINUTES));
117         assertEquals(ChronoUnit.HOURS, PoolImplUtils.toChronoUnit(TimeUnit.HOURS));
118         assertEquals(ChronoUnit.DAYS, PoolImplUtils.toChronoUnit(TimeUnit.DAYS));
119     }
120 
121     @Test
122     public void testToDuration() {
123         assertEquals(Duration.ZERO, PoolImplUtils.toDuration(0, TimeUnit.MILLISECONDS));
124         assertEquals(Duration.ofMillis(1), PoolImplUtils.toDuration(1, TimeUnit.MILLISECONDS));
125         for (final TimeUnit tu : TimeUnit.values()) {
126             // All TimeUnit should be handled.
127             assertEquals(Duration.ZERO, PoolImplUtils.toDuration(0, tu));
128         }
129     }
130 }