1 24 25 package org.objectweb.cjdbc.scenario.standalone.connection; 26 27 import java.sql.Connection ; 28 import java.sql.DriverManager ; 29 import java.sql.SQLException ; 30 import java.util.EmptyStackException ; 31 import java.util.Stack ; 32 import java.util.Vector ; 33 34 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException; 35 import org.objectweb.cjdbc.controller.connection.AbstractPoolConnectionManager; 36 import org.objectweb.cjdbc.scenario.templates.NoTemplate; 37 import org.objectweb.cjdbc.scenario.tools.mock.MockDriver; 38 import org.objectweb.cjdbc.scenario.tools.util.PrivilegedAccessor; 39 40 import com.mockobjects.sql.MockConnection2; 41 42 48 public class AbstractPoolConnectionManagerTest extends NoTemplate 49 { 50 51 private MockDriver mockDriver; 52 53 54 private AbstractPoolConnectionManager pool; 55 56 57 private Stack freeConnections; 58 59 60 private Vector activeConnections; 61 62 69 private void initializePool(int maxConnectionNumber, int requestedPoolSize) 70 { 71 mockDriver = new MyMockDriver(maxConnectionNumber); 74 75 try 77 { 78 DriverManager.registerDriver(mockDriver); 79 } 80 catch (SQLException e) 81 { 82 fail("Failed to register driver: " + e); 83 } 84 85 try 87 { 88 pool = new MockPoolConnectionManager("", "", "", "", requestedPoolSize); 89 } 90 catch (Exception e) 91 { 92 fail("Failed to create pool connection manager: " + e); 93 } 94 95 try 97 { 98 pool.initializeConnections(); 99 } 100 catch (SQLException e) 101 { 102 fail("Failed to initialize pool connection manager: " + e); 103 } 104 105 try 107 { 108 freeConnections = (Stack ) PrivilegedAccessor.getValue(pool, 109 "freeConnections"); 110 activeConnections = (Vector ) PrivilegedAccessor.getValue(pool, 111 "activeConnections"); 112 } 113 catch (Exception e) 114 { 115 fail("Unexpected exception thrown: " + e); 116 } 117 118 try 120 { 121 DriverManager.deregisterDriver(mockDriver); 122 } 123 catch (SQLException e) 124 { 125 fail("Failed to deregister driver: " + e); 126 } 127 } 128 129 132 public void testInitializeConnections() 133 { 134 try 135 { 136 initializePool(0, 10); 138 assertEquals(0, freeConnections.size()); 139 assertEquals(0, activeConnections.size()); 140 assertEquals(0, ((Integer ) PrivilegedAccessor.getValue(pool, "poolSize")) 141 .intValue()); 142 assertTrue(pool.isInitialized()); 143 144 initializePool(20, 10); 146 assertEquals(10, freeConnections.size()); 147 assertEquals(0, activeConnections.size()); 148 assertEquals(10, 149 ((Integer ) PrivilegedAccessor.getValue(pool, "poolSize")).intValue()); 150 assertTrue(pool.isInitialized()); 151 152 initializePool(20, 20); 154 assertEquals(20, freeConnections.size()); 155 assertEquals(0, activeConnections.size()); 156 assertEquals(20, 157 ((Integer ) PrivilegedAccessor.getValue(pool, "poolSize")).intValue()); 158 assertTrue(pool.isInitialized()); 159 160 initializePool(20, 30); 162 assertEquals(20, freeConnections.size()); 163 assertEquals(0, activeConnections.size()); 164 assertEquals(20, 165 ((Integer ) PrivilegedAccessor.getValue(pool, "poolSize")).intValue()); 166 assertTrue(pool.isInitialized()); 167 } 168 catch (Exception e) 169 { 170 fail("Unexpected exception thrown: " + e); 171 } 172 173 try 175 { 176 pool.initializeConnections(); 177 fail("Exception not thrown when trying to initialized twice a pool"); 178 } 179 catch (SQLException expected) 180 { 181 } 182 } 183 184 187 public void testFinalizeConnections() 188 { 189 initializePool(10, 10); 191 192 for (int i = 0; i < 6; i++) 194 { 195 try 196 { 197 pool.getConnection(); 198 } 199 catch (UnreachableBackendException e1) 200 { 201 fail("Backend unreachable during test."); 202 } 203 } 204 205 try 207 { 208 pool.finalizeConnections(); 209 } 210 catch (SQLException e) 211 { 212 fail("Failed to finalize pool connection manager: " + e); 213 } 214 215 mockDriver.verify(); 217 } 218 219 225 public class MockPoolConnectionManager extends AbstractPoolConnectionManager 226 { 227 230 public MockPoolConnectionManager(String backendUrl, String backendName, 231 String rLogin, String rPassword, int poolSize) 232 { 233 super(backendUrl, backendName, rLogin, rPassword, null, null, poolSize); 234 } 235 236 239 public Connection getConnection() 240 { 241 try 242 { 243 Connection c = (Connection ) this.freeConnections.removeLast(); 244 this.activeConnections.add(c); 245 return c; 246 } 247 catch (EmptyStackException e) 248 { 249 return null; 250 } 251 } 252 253 256 public void releaseConnection(Connection connection) 257 { 258 this.freeConnections.addLast(connection); 259 } 260 261 264 public void deleteConnection(Connection connection) 265 { 266 this.activeConnections.remove(connection); 267 } 268 269 272 public String getXmlImpl() 273 { 274 return null; 275 } 276 277 280 protected Object clone() throws CloneNotSupportedException 281 { 282 return null; 283 } 284 } 285 286 291 public class MyMockDriver extends MockDriver 292 { 293 294 private MockConnection2[] mockConnections; 295 296 302 public MyMockDriver(int maxConnectionNumber) 303 { 304 mockConnections = new MockConnection2[maxConnectionNumber]; 305 for (int i = 0; i < maxConnectionNumber; i++) 306 { 307 mockConnections[i] = new MockConnection2(); 308 mockConnections[i].setExpectedCloseCalls(1); 309 mockConnections[i].setupIsClosed(true); 310 setupConnect(mockConnections[i]); 311 } 312 setupExceptionConnect(new SQLException ( 313 "Max number of connection reached (" + maxConnectionNumber + ")")); 314 } 315 316 321 public void verify() 322 { 323 super.verify(); 324 for (int i = 0; i < mockConnections.length; i++) 325 { 326 mockConnections[i].verify(); 327 } 328 } 329 } 330 } | Popular Tags |