1 package org.apache.commons.jcs3.auxiliary.disk.jdbc;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
42 public class JDBCDataSourceFactoryUnitTest
43 extends TestCase
44 {
45
46
47
48 public void testConfigureDataSourceFactory_Simple() throws SQLException
49 {
50
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
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
83 assertEquals( "Wrong pool name", poolName, spds.getDescription() );
84 assertEquals( "Wrong maxActive value", maxActive, spds.getMaxTotal() );
85 }
86
87
88
89
90 public void testConfigureDataSourceFactory_Attributes() throws SQLException
91 {
92
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
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
117 assertEquals( "Wrong maxActive value", maxActive, spds.getMaxTotal() );
118 }
119
120
121
122
123 public void testConfigureDataSourceFactory_JNDI() throws SQLException
124 {
125
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
142 final DataSourceFactory result = factory.getDataSourceFactory( cattr, null );
143 assertTrue("Should be a JNDI data source factory", result instanceof JndiDataSourceFactory);
144 }
145
146
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
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
200 throw new RuntimeException(e);
201 }
202 }
203 }
204 }
205