1 19 20 package org.apache.avalon.excalibur.datasource.test; 21 22 import java.sql.Connection ; 23 import java.sql.SQLException ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.Random ; 27 28 import org.apache.avalon.excalibur.datasource.DataSourceComponent; 29 import org.apache.avalon.excalibur.testcase.CascadingAssertionFailedError; 30 import org.apache.avalon.excalibur.testcase.ExcaliburTestCase; 31 import org.apache.avalon.framework.component.Component; 32 import org.apache.avalon.framework.component.ComponentException; 33 34 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 35 36 43 public class DataSourceJdbcTestCase 44 extends ExcaliburTestCase 45 { 46 protected boolean m_isSuccessful; 47 protected CyclicBarrier m_barrier; 48 protected int m_connectionCount; 49 50 public DataSourceJdbcTestCase( String name ) 51 { 52 super( name ); 53 } 54 55 public void testOverAllocation() 56 { 57 DataSourceComponent ds = null; 58 m_isSuccessful = false; 59 LinkedList connectionList = new LinkedList (); 60 61 try 62 { 63 ds = (DataSourceComponent)manager.lookup( DataSourceComponent.ROLE ); 64 65 for( int i = 0; i < 10; i++ ) 66 { 67 connectionList.add( ds.getConnection() ); 68 } 69 getLogger().info( "Testing overallocation of connections. Should see a warning next." ); 70 connectionList.add( ds.getConnection() ); 71 } 72 catch( SQLException se ) 73 { 74 this.m_isSuccessful = true; 75 getLogger().info( "The test was successful" ); 76 } 77 catch( ComponentException ce ) 78 { 79 if( getLogger().isDebugEnabled() ) 80 { 81 getLogger().debug( "There was an error in the OverAllocation test", ce ); 82 } 83 84 throw new CascadingAssertionFailedError( "There was an error in the OverAllocation test", ce ); 85 } 86 finally 87 { 88 assertTrue( "The DataSourceComponent could not be retrieved.", null != ds ); 89 90 Iterator connections = connectionList.iterator(); 91 92 while( connections.hasNext() ) 93 { 94 try 95 { 96 ( (Connection )connections.next() ).close(); 97 } 98 catch( SQLException se ) 99 { 100 } 102 } 103 104 connectionList.clear(); 105 106 manager.release( (Component)ds ); 107 } 108 109 assertTrue( "Exception was not thrown when too many datasource components were retrieved.", this.m_isSuccessful ); 110 } 111 112 public void testNormalUse() 113 { 114 DataSourceComponent ds = null; 115 m_isSuccessful = true; 116 117 try 118 { 119 ds = (DataSourceComponent)manager.lookup( DataSourceComponent.ROLE ); 120 121 m_connectionCount = 0; 122 m_barrier = new CyclicBarrier( 11 ); 123 124 for( int i = 0; i < 10; i++ ) 125 { 126 ( new Thread ( new ConnectionThread( this, ds ) ) ).start(); 127 } 128 129 try 130 { 131 m_barrier.barrier(); 132 } 133 catch( Exception ie ) 134 { 135 } 137 138 getLogger().info( "The normal use test passed with " + this.m_connectionCount + " requests and 10 concurrent threads running" ); 139 } 140 catch( ComponentException ce ) 141 { 142 if( getLogger().isDebugEnabled() ) 143 { 144 getLogger().debug( "There was an error in the normal use test", ce ); 145 } 146 147 throw new CascadingAssertionFailedError( "There was an error in the normal use test", ce ); 148 } 149 finally 150 { 151 assertTrue( "The DataSourceComponent could not be retrieved.", null != ds ); 152 153 manager.release( (Component)ds ); 154 } 155 156 assertTrue( "Normal use test failed", this.m_isSuccessful ); 157 } 158 159 static class ConnectionThread 160 implements Runnable  161 { 162 protected DataSourceComponent m_datasource; 163 protected DataSourceJdbcTestCase m_testcase; 164 165 ConnectionThread( DataSourceJdbcTestCase testcase, 166 final DataSourceComponent datasource ) 167 { 168 m_datasource = datasource; 169 m_testcase = testcase; 170 } 171 172 public void run() 173 { 174 long end = System.currentTimeMillis() + 5000; Random rnd = new Random (); 176 177 while( System.currentTimeMillis() < end && m_testcase.m_isSuccessful ) 178 { 179 try 180 { 181 Connection con = this.m_datasource.getConnection(); 182 Thread.sleep( (long)rnd.nextInt( 100 ) ); con.close(); 184 this.m_testcase.m_connectionCount++; 185 } 186 catch( final SQLException se ) 187 { 188 m_testcase.m_isSuccessful = false; 189 m_testcase.getLogger().info( "Failed to get Connection, test failed", se ); 190 } 191 catch( final InterruptedException ie ) 192 { 193 } 195 } 196 197 try 198 { 199 m_testcase.m_barrier.barrier(); 200 } 201 catch( final InterruptedException ie ) 202 { 203 } 205 } 206 } 207 } 208 209 | Popular Tags |