View Javadoc
1   package org.apache.commons.jcs.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 junit.framework.TestCase;
34  
35  import org.apache.commons.dbcp.BasicDataSource;
36  import org.apache.commons.dbcp.datasources.SharedPoolDataSource;
37  import org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory.DataSourceFactory;
38  import org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory.JndiDataSourceFactory;
39  import org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory.SharedPoolDataSourceFactory;
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          String poolName = "testConfigurePoolAccessAttributes_Simple";
52  
53          String url = "adfads";
54          String userName = "zvzvz";
55          String password = "qewrrewq";
56          int maxActive = 10;
57          String driverClassName = "org.hsqldb.jdbcDriver";
58  
59          Properties props = new Properties();
60          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          JDBCDiskCacheFactory factory = new JDBCDiskCacheFactory();
70          factory.initialize();
71  
72          JDBCDiskCacheAttributes cattr = new JDBCDiskCacheAttributes();
73          cattr.setConnectionPoolName( poolName );
74  
75          // DO WORK
76          DataSourceFactory result = factory.getDataSourceFactory( cattr, props );
77          assertTrue("Should be a shared pool data source factory", result instanceof SharedPoolDataSourceFactory);
78  
79          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.getMaxActive() );
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          String url = "adfads";
94          String userName = "zvzvz";
95          String password = "qewrrewq";
96          int maxActive = 10;
97          String driverClassName = "org.hsqldb.jdbcDriver";
98  
99          JDBCDiskCacheFactory factory = new JDBCDiskCacheFactory();
100         factory.initialize();
101 
102         JDBCDiskCacheAttributes cattr = new JDBCDiskCacheAttributes();
103         cattr.setUrl(url);
104         cattr.setUserName(userName);
105         cattr.setPassword(password);
106         cattr.setMaxActive(maxActive);
107         cattr.setDriverClassName(driverClassName);
108 
109         // DO WORK
110         DataSourceFactory result = factory.getDataSourceFactory( cattr, null );
111         assertTrue("Should be a shared pool data source factory", result instanceof SharedPoolDataSourceFactory);
112 
113         SharedPoolDataSource spds = (SharedPoolDataSource) result.getDataSource();
114         assertNotNull( "Should have a data source class", spds );
115 
116         // VERIFY
117         assertEquals( "Wrong maxActive value", maxActive, spds.getMaxActive() );
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         String jndiPath = "java:comp/env/jdbc/MyDB";
127         long ttl = 300000L;
128 
129         System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
130         		  MockInitialContextFactory.class.getName());
131 
132         MockInitialContextFactory.bind(jndiPath, new BasicDataSource());
133 
134         JDBCDiskCacheFactory factory = new JDBCDiskCacheFactory();
135         factory.initialize();
136 
137         JDBCDiskCacheAttributes cattr = new JDBCDiskCacheAttributes();
138         cattr.setJndiPath(jndiPath);
139         cattr.setJndiTTL(ttl);
140 
141         // DO WORK
142         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 Context context;
150 
151         static
152         {
153             try
154             {
155                 context = new InitialContext(true)
156                 {
157                     Map<String, Object> bindings = new HashMap<String, Object>();
158 
159                     @Override
160                     public void bind(String name, Object obj) throws NamingException
161                     {
162                         bindings.put(name, obj);
163                     }
164 
165                     @Override
166                     public Object lookup(String name) throws NamingException
167                     {
168                         return bindings.get(name);
169                     }
170                 };
171             }
172             catch (NamingException e)
173             {
174             	// can't happen.
175                 throw new RuntimeException(e);
176             }
177         }
178 
179         @Override
180 		public Context getInitialContext(Hashtable<?, ?> environment) throws NamingException
181         {
182             return context;
183         }
184 
185         public static void bind(String name, Object obj)
186         {
187             try
188             {
189                 context.bind(name, obj);
190             }
191             catch (NamingException e)
192             {
193             	// can't happen.
194                 throw new RuntimeException(e);
195             }
196         }
197     }
198 }
199