1 package org.apache.commons.jcs.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.Connection;
23 import java.sql.DriverManager;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Properties;
27 import java.util.Set;
28 import java.util.concurrent.Executors;
29
30 import junit.framework.TestCase;
31
32 import org.apache.commons.jcs.JCS;
33 import org.apache.commons.jcs.access.CacheAccess;
34 import org.apache.commons.jcs.auxiliary.disk.jdbc.dsfactory.DataSourceFactory;
35 import org.apache.commons.jcs.engine.behavior.ICacheElement;
36 import org.apache.commons.jcs.engine.control.MockCompositeCacheManager;
37 import org.apache.commons.jcs.utils.serialization.StandardSerializer;
38 import org.apache.commons.jcs.utils.threadpool.DaemonThreadFactory;
39
40
41
42
43
44
45 public class JDBCDiskCacheUnitTest
46 extends TestCase
47 {
48
49 @Override
50 public void setUp()
51 {
52 JCS.setConfigFilename( "/TestJDBCDiskCache.ccf" );
53 }
54
55
56
57
58
59 public void testSimpleJDBCPutGetWithHSQL()
60 throws Exception
61 {
62 System.setProperty( "hsqldb.cache_scale", "8" );
63
64 String rafroot = "target";
65 Properties p = new Properties();
66 String driver = p.getProperty( "driver", "org.hsqldb.jdbcDriver" );
67 String url = p.getProperty( "url", "jdbc:hsqldb:" );
68 String database = p.getProperty( "database", rafroot + "/cache_hsql_db" );
69 String user = p.getProperty( "user", "sa" );
70 String password = p.getProperty( "password", "" );
71
72 new org.hsqldb.jdbcDriver();
73 Class.forName( driver ).newInstance();
74 Connection cConn = DriverManager.getConnection( url + database, user, password );
75
76 HsqlSetupTableUtil.setupTABLE( cConn, "JCS_STORE2" );
77
78 runTestForRegion( "testCache1", 200 );
79 }
80
81
82
83
84
85
86
87
88
89 public void runTestForRegion( String region, int items )
90 throws Exception
91 {
92 CacheAccess<String, String> jcs = JCS.getInstance( region );
93
94
95
96
97
98 for ( int i = 0; i <= items; i++ )
99 {
100 jcs.put( i + ":key", region + " data " + i );
101 }
102
103
104
105 Thread.sleep( 1000 );
106
107
108
109
110
111 for ( int i = 0; i <= items; i++ )
112 {
113 String value = jcs.get( i + ":key" );
114
115 assertEquals( "key = [" + i + ":key] value = [" + value + "]", region + " data " + i, value );
116 }
117
118
119 Set<String> keys = new HashSet<String>();
120 for ( int i = 0; i <= items; i++ )
121 {
122 keys.add( i + ":key" );
123 }
124
125 Map<String, ICacheElement<String, String>> elements = jcs.getCacheElements( keys );
126 for ( int i = 0; i <= items; i++ )
127 {
128 ICacheElement<String, String> element = elements.get( i + ":key" );
129 assertNotNull( "element " + i + ":key is missing", element );
130 assertEquals( "value " + i + ":key", region + " data " + i, element.getVal() );
131 }
132
133
134 for ( int i = 0; i <= items; i++ )
135 {
136 jcs.remove( i + ":key" );
137 }
138
139
140 for ( int i = 0; i <= items; i++ )
141 {
142 assertNull( "Removed key should be null: " + i + ":key", jcs.get( i + ":key" ) );
143 }
144 }
145
146
147
148
149
150
151 public void testInitializePoolAccess_withPoolName()
152 throws Exception
153 {
154
155 String poolName = "testInitializePoolAccess_withPoolName";
156
157 String url = "jdbc:hsqldb:";
158 String userName = "sa";
159 String password = "";
160 int maxActive = 10;
161 String driverClassName = "org.hsqldb.jdbcDriver";
162
163 Properties props = new Properties();
164 String prefix = JDBCDiskCacheFactory.POOL_CONFIGURATION_PREFIX
165 + poolName
166 + JDBCDiskCacheFactory.ATTRIBUTE_PREFIX;
167 props.put( prefix + ".url", url );
168 props.put( prefix + ".userName", userName );
169 props.put( prefix + ".password", password );
170 props.put( prefix + ".maxActive", String.valueOf( maxActive ) );
171 props.put( prefix + ".driverClassName", driverClassName );
172
173 JDBCDiskCacheAttributes cattr = new JDBCDiskCacheAttributes();
174 cattr.setConnectionPoolName( poolName );
175 cattr.setTableName("JCSTESTTABLE_InitializePoolAccess");
176
177 MockCompositeCacheManager compositeCacheManager = new MockCompositeCacheManager();
178 compositeCacheManager.setConfigurationProperties( props );
179 JDBCDiskCacheFactory dcFactory = new JDBCDiskCacheFactory();
180 dcFactory.initialize();
181 dcFactory.setScheduledExecutorService(Executors.newScheduledThreadPool(2,
182 new DaemonThreadFactory("JCS-JDBCDiskCacheManager-", Thread.MIN_PRIORITY)));
183
184 JDBCDiskCache<String, String> diskCache = dcFactory.createCache( cattr, compositeCacheManager, null, new StandardSerializer() );
185 assertNotNull( "Should have a cache instance", diskCache );
186
187
188 DataSourceFactory result = dcFactory.getDataSourceFactory(cattr, props);
189
190
191 assertNotNull( "Should have a data source factory class", result );
192 assertEquals( "wrong name", poolName, result.getName() );
193
194 System.setProperty( "hsqldb.cache_scale", "8" );
195
196 String rafroot = "target";
197 String database = rafroot + "/cache_hsql_db";
198
199 new org.hsqldb.jdbcDriver();
200 Class.forName( driverClassName ).newInstance();
201 Connection cConn = DriverManager.getConnection( url + database, userName, password );
202
203 HsqlSetupTableUtil.setupTABLE( cConn, "JCSTESTTABLE_InitializePoolAccess" );
204
205 }
206 }