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  
18  package org.apache.commons.dbcp2.datasources;
19  
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  
22  import java.util.Hashtable;
23  
24  import javax.naming.CompositeName;
25  import javax.naming.Context;
26  import javax.naming.InitialContext;
27  import javax.naming.Name;
28  import javax.naming.Reference;
29  import javax.naming.StringRefAddr;
30  import javax.naming.spi.ObjectFactory;
31  
32  import org.junit.jupiter.params.ParameterizedTest;
33  import org.junit.jupiter.params.provider.CsvSource;
34  
35  /**
36   * Tests SharedPoolDataSourceFactory.
37   */
38  public class TestFactory {
39  
40      /**
41       * Tests Bugzilla Bug 24082: bug in InstanceKeyDataSourceFactory.
42       *
43       * There's a fatal bug in InstanceKeyDataSourceFactory that means you can't instantiate more than one factory.
44       * https://issues.apache.org/bugzilla/show_bug.cgi?id=24082
45       */
46      @ParameterizedTest
47      // @formatter:off
48      @CsvSource({
49          "dataSourceName,                 java:comp/env/jdbc/bookstoreCPDS",
50          "description,                    This is a test.",
51          "jndiEnvironment,                X",
52          "loginTimeout,                   30000",
53          "blockWhenExhausted,             false",
54          "evictionPolicyClassName,        org.apache.commons.pool2.impl.DefaultEvictionPolicy",
55          "lifo,                           true",
56          "maxIdlePerKey,                  4",
57          "maxTotalPerKey,                 4",
58          "maxWaitMillis,                  30001",
59          "minEvictableIdleTimeMillis,     30002",
60          "minIdlePerKey,                  4",
61          "numTestsPerEvictionRun,         2",
62          "softMinEvictableIdleTimeMillis, 30003",
63          "testOnCreate,                   true",
64          "testOnBorrow,                   true",
65          "testOnReturn,                   true",
66          "testWhileIdle,                  true",
67          "timeBetweenEvictionRunsMillis,  30004",
68          "validationQuery,                select 1",
69          "validationQueryTimeout,         30005",
70          "rollbackAfterValidation,        false",
71          "maxConnLifetimeMillis,          60000",
72          "defaultAutoCommit,              true",
73          "defaultTransactionIsolation,    X",
74          "defaultReadOnly,                true" })
75      // @formatter:on
76      public void testJNDI2Pools(final String string, final String value) throws Exception {
77          final Reference refObj = new Reference(SharedPoolDataSource.class.getName());
78          refObj.add(new StringRefAddr(string, value));
79          final Context context = new InitialContext();
80          final Hashtable<?, ?> env = new Hashtable<>();
81  
82          final ObjectFactory factory = new SharedPoolDataSourceFactory();
83  
84          final Name name = new CompositeName("myDB");
85          final Object obj = factory.getObjectInstance(refObj, name, context, env);
86          assertNotNull(obj);
87  
88          final Name name2 = new CompositeName("myDB2");
89          final Object obj2 = factory.getObjectInstance(refObj, name2, context, env);
90          assertNotNull(obj2);
91      }
92  }