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;
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  public class TestBasePoolableObjectFactory {
30  
31      private static class TestFactory extends BasePooledObjectFactory<AtomicInteger> {
32          @Override
33          public AtomicInteger create() {
34              return new AtomicInteger(0);
35          }
36          @Override
37          public void destroyObject(final PooledObject<AtomicInteger> p, final DestroyMode destroyMode){
38              if (destroyMode.equals(DestroyMode.ABANDONED)) {
39                  p.getObject().incrementAndGet();
40              }
41          }
42          @Override
43          public PooledObject<AtomicInteger> wrap(final AtomicInteger value) {
44              return new DefaultPooledObject<>(value);
45          }
46      }
47  
48      @Test
49      public void testDefaultMethods() throws Exception {
50          final PooledObjectFactory<AtomicInteger> factory = new TestFactory();
51  
52          factory.activateObject(null); // a no-op
53          factory.passivateObject(null); // a no-op
54          factory.destroyObject(null); // a no-op
55          assertTrue(factory.validateObject(null)); // constant true
56      }
57  
58      /**
59       * Default destroy does nothing to underlying AtomicInt, ABANDONED mode
60       * increments the value.  Verify that destroy with no mode does default,
61       * destroy with ABANDONED mode increments.
62       *
63       * @throws RuntimeException May occur in some failure modes
64       */
65      @Test
66      public 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  }