1 /* ==================================================================== 2 * License: 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in 13 * the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * 3. The end-user documentation included with the redistribution, 17 * if any, must include the following acknowledgment: 18 * "This product includes software developed by 19 * Robert Half International (http://www.rhi.com/)." 20 * Alternately, this acknowledgment may appear in the software itself, 21 * if and wherever such third-party acknowledgments normally appear. 22 * 23 * 4. The names "Parc", "RHI", and "Robert Half International" must 24 * not be used to endorse or promote products derived from this 25 * software without prior written permission. For written 26 * permission, please contact pete.mckinstry@rhi.com. 27 * 28 * 5. Products derived from this software may not be called "PARC", 29 * nor may "PARC" appear in their name, without prior written 30 * permission of Robert Half International. 31 * 32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 35 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 36 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 39 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 41 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 42 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 43 * SUCH DAMAGE. 44 * ==================================================================== 45 * 46 */ 47 package com.rhi.architecture.db; 48 49 import com.rhi.architecture.logging.DefaultLogger; 50 import com.rhi.architecture.logging.Logger; 51 import com.rhi.architecture.resource.ResourceContext; 52 53 import java.io.IOException; 54 import java.io.InputStream; 55 import java.sql.Connection; 56 import java.sql.PreparedStatement; 57 import java.sql.ResultSet; 58 import java.sql.SQLException; 59 import java.util.ArrayList; 60 import java.util.ListIterator; 61 import java.util.NoSuchElementException; 62 import java.util.Properties; 63 64 import javax.sql.DataSource; 65 66 import junit.framework.Test; 67 import junit.framework.TestCase; 68 import junit.framework.TestSuite; 69 70 import org.apache.commons.pool.impl.GenericObjectPool; 71 72 /*** 73 * Unit Test Driver 74 * 75 * @author Pete McKinstry 76 * @copyright 2001, Robert Half Int'l., Inc. All rights reserved. 77 * 78 * @since 1.0 79 */ 80 public class DataSourceFactoryTest extends TestCase { 81 82 private static Logger log = new DefaultLogger(); 83 84 private ResourceContext ctx = null; 85 86 /*** 87 * constructor 88 * @param name 89 */ 90 public DataSourceFactoryTest(String name) { 91 super(name); 92 } 93 94 protected void setUp() throws Exception { 95 super.setUp(); 96 97 try { 98 ctx = new ResourceContext(); 99 //Properties p = getProperties(); 100 //p.list(System.err); 101 log.debug("calling ResourceContext.init()"); 102 ctx.init(getProperties()); 103 } 104 catch (Exception e) { 105 log.debug(e.toString()); 106 e.printStackTrace(); 107 throw new java.lang.RuntimeException( 108 "ResourceContext failed to initialize. " 109 + " e = " 110 + e.toString()); 111 } 112 } 113 114 protected Properties getProperties() { 115 Properties p = new Properties(); 116 117 InputStream str = 118 Thread.currentThread().getContextClassLoader().getResourceAsStream( 119 "DataSourceFactoryTest.properties"); 120 if (str != null) { 121 try { 122 p.load(str); 123 } 124 catch (IOException e) { 125 throw new RuntimeException("Unable to load properties."); 126 } 127 } 128 else { 129 throw new RuntimeException("properties file not found."); 130 } 131 132 return p; 133 } 134 135 protected void tearDown() throws Exception { 136 super.tearDown(); 137 ctx.cleanup(); 138 } 139 140 /*** 141 * test createDataSource method. 142 * @throws Exception 143 */ 144 public void testCreateDataSource() throws Exception { 145 DataSourceFactory factory = 146 (DataSourceFactory) ctx.lookup(DataSourceFactory.KEY); 147 Connection con = null; 148 PreparedStatement stmt = null; 149 ResultSet rs = null; 150 try { 151 Properties props = new Properties(); 152 InputStream str = 153 Thread.currentThread().getContextClassLoader() 154 .getResourceAsStream("DataSourceFactoryTest.properties"); 155 if (str == null) { 156 throw new Exception("properties file failed to load."); 157 } 158 props.load(str); 159 // loading the default data source. 160 DataSource dataSource = factory.getDataSource(); 161 assertNotNull("null datasource", dataSource); 162 con = dataSource.getConnection(); 163 assertNotNull("connection null", con); 164 stmt = con.prepareStatement( 165 "create table unit_test ( name varchar )"); 166 stmt.executeUpdate(); 167 stmt = con.prepareStatement( 168 "drop table unit_test"); 169 stmt.executeUpdate(); 170 } 171 catch (SQLException e) { 172 log.error("SQLException = " + e, e); 173 fail("SQLException = " + e); 174 } 175 finally { 176 DBUtil.close(rs); 177 DBUtil.close(stmt); 178 DBUtil.close(con); 179 } 180 } 181 182 /*** 183 * test creation of multiple connections w/ DataSource 184 * @throws Exception 185 */ 186 public void testMultipleConnections() throws Exception { 187 log.debug("testMultipleConnections"); 188 DataSourceFactory factory = 189 (DataSourceFactory) ctx.lookup(DataSourceFactory.KEY); 190 // close it so that we can reload it w/ the test settings. 191 factory.close(); 192 ArrayList connections = new ArrayList(); 193 int test_size = 5; 194 try { 195 Properties props = new Properties(); 196 InputStream str = 197 Thread.currentThread().getContextClassLoader() 198 .getResourceAsStream("DataSourceFactoryTest.properties"); 199 if (str == null) { 200 throw new Exception("properties file failed to load."); 201 } 202 props.load(str); 203 // ensure the values are the way I want them. 204 props.setProperty(DataSourceFactory.MAX_SIZE_KEY, "5"); 205 props.setProperty(DataSourceFactory.MAX_WAIT_KEY, "1"); 206 factory.setAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL); 207 // reload the configured data sources. 208 factory.init(props); 209 DataSource dataSource = factory.getDataSource(); 210 assertNotNull("null datasource", dataSource); 211 212 for (int i = 0; i < test_size; i++) { 213 Connection conn = dataSource.getConnection(); 214 assertNotNull("connection null", conn); 215 connections.add(conn); 216 } 217 log.debug("testMultipleConnections successful."); 218 } 219 catch (NoSuchElementException e) { 220 fail("NoSuchElementException thrown when pool not exhausted."); 221 } 222 catch (SQLException e) { 223 log.error("SQLException = " + e, e); 224 fail("SQLException = " + e); 225 } 226 finally { 227 ListIterator iter = connections.listIterator(); 228 while (iter.hasNext()) { 229 Connection conn = (Connection)iter.next(); 230 DBUtil.close(conn); 231 } 232 } 233 log.debug("testMultipleConnections done"); 234 } 235 236 /*** 237 * test max size of connection pool. 238 * @throws Exception 239 */ 240 public void testConnectionPoolMaxSize() throws Exception { 241 log.debug("testConnectionPoolMaxSize"); 242 DataSourceFactory factory = 243 (DataSourceFactory) ctx.lookup(DataSourceFactory.KEY); 244 // reset the factory. 245 factory.close(); 246 ArrayList connections = new ArrayList(); 247 try { 248 Properties props = new Properties(); 249 InputStream str = 250 Thread.currentThread().getContextClassLoader() 251 .getResourceAsStream("DataSourceFactoryTest.properties"); 252 if (str == null) { 253 throw new Exception("properties file failed to load."); 254 } 255 props.load(str); 256 // ensure the values are the way I want them. 257 props.setProperty(DataSourceFactory.MAX_SIZE_KEY, "1"); 258 props.setProperty(DataSourceFactory.MAX_WAIT_KEY, "1"); 259 factory.setAction(GenericObjectPool.WHEN_EXHAUSTED_FAIL); 260 factory.init(props); 261 DataSource dataSource = factory.getDataSource(); 262 assertNotNull("null datasource", dataSource); 263 264 Connection first = dataSource.getConnection(); 265 assertNotNull("connection null", first); 266 connections.add(first); 267 try { 268 Connection test = dataSource.getConnection(); 269 if (test!=null) { 270 connections.add(test); 271 } 272 fail("connection pool growing past max size!"); 273 } 274 catch (NoSuchElementException e) { 275 // okay. this is the expected result. 276 //log.debug("NotSuchElementException thrown = " + e); 277 //e.printStackTrace(); 278 } 279 } 280 catch (SQLException e) { 281 log.error("SQLException = " + e, e); 282 fail("SQLException = " + e); 283 } 284 finally { 285 ListIterator iter = connections.listIterator(); 286 while (iter.hasNext()) { 287 Connection conn = (Connection)iter.next(); 288 DBUtil.close(conn); 289 } 290 } 291 log.debug("testPoolMaxSize done"); 292 } 293 294 /*** 295 * test data source lookup (simple) 296 * @throws Exception 297 */ 298 public void testLookupDataSource() throws Exception { 299 DataSourceFactory factory = 300 (DataSourceFactory) ctx.lookup(DataSourceFactory.KEY); 301 assertTrue("factory null", factory != null); 302 } 303 304 /*** 305 * suite of all tests 306 * @return Test - all tests 307 */ 308 public static Test suite() { 309 return new TestSuite(DataSourceFactoryTest.class); 310 } 311 312 }

This page was automatically generated by Maven