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    *      https://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;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.util.concurrent.atomic.AtomicInteger;
23  
24  import org.apache.commons.pool2.impl.DefaultPooledObject;
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   */
29  class TestBasePoolableObjectFactory {
30  
31      private static final class TestFactory extends BasePooledObjectFactory<AtomicInteger> {
32          @Override
33          public AtomicInteger create() {
34              return new AtomicInteger();
35          }
36  
37          @Override
38          public void destroyObject(final PooledObject<AtomicInteger> p, final DestroyMode destroyMode) {
39              if (destroyMode.equals(DestroyMode.ABANDONED)) {
40                  p.getObject().incrementAndGet();
41              }
42          }
43  
44          @Override
45          public PooledObject<AtomicInteger> wrap(final AtomicInteger value) {
46              return new DefaultPooledObject<>(value);
47          }
48      }
49  
50      @Test
51      void testDefaultMethods() throws Exception {
52          final PooledObjectFactory<AtomicInteger> factory = new TestFactory();
53          factory.activateObject(null); // a no-op
54          factory.passivateObject(null); // a no-op
55          factory.destroyObject(null); // a no-op
56          assertTrue(factory.validateObject(null)); // constant true
57      }
58  
59      /**
60       * Default destroy does nothing to underlying AtomicInt, ABANDONED mode increments the value. Verify that destroy with no mode does default, destroy with
61       * ABANDONED mode increments.
62       *
63       * @throws RuntimeException May occur in some failure modes
64       */
65      @Test
66      void testDestroyModes() throws Exception {
67          final PooledObjectFactory<AtomicInteger> factory = new TestFactory();
68          final PooledObject<AtomicInteger> pooledObj = factory.makeObject();
69          final AtomicInteger obj = pooledObj.getObject();
70          factory.destroyObject(pooledObj);
71          assertEquals(0, obj.get());
72          factory.destroyObject(pooledObj, DestroyMode.ABANDONED);
73          assertEquals(1, obj.get());
74      }
75  }