View Javadoc
1   package org.apache.commons.jcs3.auxiliary.disk.jdbc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.sql.SQLException;
23  import java.util.HashMap;
24  import java.util.Hashtable;
25  import java.util.Map;
26  import java.util.Properties;
27  
28  import javax.naming.Context;
29  import javax.naming.InitialContext;
30  import javax.naming.NamingException;
31  import javax.naming.spi.InitialContextFactory;
32  
33  import org.apache.commons.dbcp2.BasicDataSource;
34  import org.apache.commons.dbcp2.datasources.SharedPoolDataSource;
35  import org.apache.commons.jcs3.auxiliary.disk.jdbc.dsfactory.DataSourceFactory;
36  import org.apache.commons.jcs3.auxiliary.disk.jdbc.dsfactory.JndiDataSourceFactory;
37  import org.apache.commons.jcs3.auxiliary.disk.jdbc.dsfactory.SharedPoolDataSourceFactory;
38  
39  import junit.framework.TestCase;
40  
41  /** Unit tests for the data source factories */
42  public class JDBCDataSourceFactoryUnitTest
43      extends TestCase
44  {
45      /** Verify that we can configure the object based on the props.
46       *  @throws SQLException
47       */
48      public void testConfigureDataSourceFactory_Simple() throws SQLException
49      {
50          // SETUP
51          final String poolName = "testConfigurePoolAccessAttributes_Simple";
52  
53          final String url = "adfads";
54          final String userName = "zvzvz";
55          final String password = "qewrrewq";
56          final int maxActive = 10;
57          final String driverClassName = "org.hsqldb.jdbcDriver";
58  
59          final Properties props = new Properties();
60          final String prefix = JDBCDiskCacheFactory.POOL_CONFIGURATION_PREFIX
61      		+ poolName
62              + JDBCDiskCacheFactory.ATTRIBUTE_PREFIX;
63          props.put( prefix + ".url", url );
64          props.put( prefix + ".userName", userName );
65          props.put( prefix + ".password", password );
66          props.put( prefix + ".maxActive", String.valueOf( maxActive ) );
67          props.put( prefix + ".driverClassName", driverClassName );
68  
69          final JDBCDiskCacheFactory factory = new JDBCDiskCacheFactory();
70          factory.initialize();
71  
72          final JDBCDiskCacheAttributes cattr = new JDBCDiskCacheAttributes();
73          cattr.setConnectionPoolName( poolName );
74  
75          // DO WORK
76          final DataSourceFactory result = factory.getDataSourceFactory( cattr, props );
77          assertTrue("Should be a shared pool data source factory", result instanceof SharedPoolDataSourceFactory);
78  
79          final SharedPoolDataSource spds = (SharedPoolDataSource) result.getDataSource();
80          assertNotNull( "Should have a data source class", spds );
81  
82          // VERIFY
83          assertEquals( "Wrong pool name", poolName, spds.getDescription() );
84          assertEquals( "Wrong maxActive value", maxActive, spds.getMaxTotal() );
85      }
86  
87      /** Verify that we can configure the object based on the attributes.
88       *  @throws SQLException
89       */
90      public void testConfigureDataSourceFactory_Attributes() throws SQLException
91      {
92          // SETUP
93          final String url = "adfads";
94          final String userName = "zvzvz";
95          final String password = "qewrrewq";
96          final int maxActive = 10;
97          final String driverClassName = "org.hsqldb.jdbcDriver";
98  
99          final JDBCDiskCacheFactory factory = new JDBCDiskCacheFactory();
100         factory.initialize();
101 
102         final JDBCDiskCacheAttributes cattr = new JDBCDiskCacheAttributes();
103         cattr.setUrl(url);
104         cattr.setUserName(userName);
105         cattr.setPassword(password);
106         cattr.setMaxTotal(maxActive);
107         cattr.setDriverClassName(driverClassName);
108 
109         // DO WORK
110         final DataSourceFactory result = factory.getDataSourceFactory( cattr, null );
111         assertTrue("Should be a shared pool data source factory", result instanceof SharedPoolDataSourceFactory);
112 
113         final SharedPoolDataSource spds = (SharedPoolDataSource) result.getDataSource();
114         assertNotNull( "Should have a data source class", spds );
115 
116         // VERIFY
117         assertEquals( "Wrong maxActive value", maxActive, spds.getMaxTotal() );
118     }
119 
120     /** Verify that we can configure the object based on JNDI.
121      *  @throws SQLException
122      */
123     public void testConfigureDataSourceFactory_JNDI() throws SQLException
124     {
125         // SETUP
126         final String jndiPath = "java:comp/env/jdbc/MyDB";
127         final long ttl = 300000L;
128 
129         System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
130                 MockInitialContextFactory.class.getName());
131 
132         MockInitialContextFactory.bind(jndiPath, new BasicDataSource());
133 
134         final JDBCDiskCacheFactory factory = new JDBCDiskCacheFactory();
135         factory.initialize();
136 
137         final JDBCDiskCacheAttributes cattr = new JDBCDiskCacheAttributes();
138         cattr.setJndiPath(jndiPath);
139         cattr.setJndiTTL(ttl);
140 
141         // DO WORK
142         final DataSourceFactory result = factory.getDataSourceFactory( cattr, null );
143         assertTrue("Should be a JNDI data source factory", result instanceof JndiDataSourceFactory);
144     }
145 
146     /* For JNDI mocking */
147     public static class MockInitialContextFactory implements InitialContextFactory
148     {
149         private static final Context context;
150 
151         static
152         {
153             try
154             {
155                 context = new InitialContext(true)
156                 {
157                     final Map<String, Object> bindings = new HashMap<>();
158 
159                     @Override
160                     public void bind(final String name, final Object obj) throws NamingException
161                     {
162                         bindings.put(name, obj);
163                     }
164 
165                     @Override
166                     public Object lookup(final String name) throws NamingException
167                     {
168                         return bindings.get(name);
169                     }
170 
171                     @Override
172                     public Hashtable<?, ?> getEnvironment() throws NamingException
173                     {
174                         return new Hashtable<>();
175                     }
176                 };
177             }
178             catch (final NamingException e)
179             {
180             	// can't happen.
181                 throw new RuntimeException(e);
182             }
183         }
184 
185         @Override
186 		public Context getInitialContext(final Hashtable<?, ?> environment) throws NamingException
187         {
188             return context;
189         }
190 
191         public static void bind(final String name, final Object obj)
192         {
193             try
194             {
195                 context.bind(name, obj);
196             }
197             catch (final NamingException e)
198             {
199             	// can't happen.
200                 throw new RuntimeException(e);
201             }
202         }
203     }
204 }
205