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.api.Test;
33  
34  /**
35   */
36  public class TestFactory {
37  
38      // Bugzilla Bug 24082: bug in InstanceKeyDataSourceFactory
39      // There's a fatal bug in InstanceKeyDataSourceFactory that means you can't
40      // instantiate more than one factory.
41      // https://issues.apache.org/bugzilla/show_bug.cgi?id=24082
42      @Test
43      public void testJNDI2Pools() throws Exception {
44          final Reference refObj = new Reference(SharedPoolDataSource.class.getName());
45          refObj.add(new StringRefAddr("dataSourceName","java:comp/env/jdbc/bookstoreCPDS"));
46          final Context context = new InitialContext();
47          final Hashtable<?, ?> env = new Hashtable<>();
48  
49          final ObjectFactory factory = new SharedPoolDataSourceFactory();
50  
51          final Name name = new CompositeName("myDB");
52          final Object obj = factory.getObjectInstance(refObj, name, context, env);
53          assertNotNull(obj);
54  
55          final Name name2 = new CompositeName("myDB2");
56          final Object obj2 = factory.getObjectInstance(refObj, name2, context, env);
57          assertNotNull(obj2);
58      }
59  }