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.Stack ; 31 import java.util.Vector ; 32 33 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException; 34 import org.objectweb.cjdbc.controller.connection.FailFastPoolConnectionManager; 35 import org.objectweb.cjdbc.scenario.templates.NoTemplate; 36 import org.objectweb.cjdbc.scenario.tools.mock.MockDriver; 37 import org.objectweb.cjdbc.scenario.tools.util.PrivilegedAccessor; 38 39 import com.mockobjects.sql.MockConnection2; 40 41 47 public class FailFastPoolConnectionManagerTest extends NoTemplate 48 { 49 50 private static final int POOL_SIZE = 30; 51 52 53 private MockDriver mockDriver; 54 55 56 private FailFastPoolConnectionManager pool; 57 58 61 protected void setUp() 62 { 63 try 65 { 66 mockDriver = new MockDriver(); 67 mockDriver.setExpectedConnectCalls(POOL_SIZE); 68 for (int i = 0; i < POOL_SIZE; i++) 69 { 70 mockDriver.setupConnect(new MockConnection2()); 71 } 72 73 DriverManager.registerDriver(mockDriver); 74 } 75 catch (SQLException e) 76 { 77 fail("Failed to register driver: " + e); 78 } 79 80 try 82 { 83 pool = new FailFastPoolConnectionManager("", "", "", "", null, null, 84 POOL_SIZE); 85 } 86 catch (Exception e) 87 { 88 fail("Failed to create pool connection manager: " + e); 89 } 90 91 try 93 { 94 pool.initializeConnections(); 95 } 96 catch (SQLException e) 97 { 98 fail("Failed to initialize pool connection manager: " + e); 99 } 100 mockDriver.verify(); 101 } 102 103 106 protected void tearDown() 107 { 108 try 110 { 111 DriverManager.deregisterDriver(mockDriver); 112 } 113 catch (SQLException e) 114 { 115 fail("Failed to deregister driver: " + e); 116 } 117 } 118 119 122 public void testGetConnection() throws Exception 123 { 124 Connection c = null; 126 127 int free = ((Stack ) PrivilegedAccessor.getValue(pool, "freeConnections")) 128 .size(); 129 int active = ((Vector ) PrivilegedAccessor.getValue(pool, 130 "activeConnections")).size(); 131 assertEquals(30, ((Integer ) PrivilegedAccessor.getValue(pool, "poolSize")) 132 .intValue()); 133 134 int i = 0; 135 while (i < POOL_SIZE) 136 { 137 try 138 { 139 c = pool.getConnection(); 140 } 141 catch (UnreachableBackendException e) 142 { 143 fail("Backend unreachable during test."); 144 } 145 if (c == null) 146 fail("Failed to get a connection from the pool"); 147 else 148 { 149 i++; 150 assertEquals(--free, ((Stack ) PrivilegedAccessor.getValue(pool, 151 "freeConnections")).size()); 152 assertEquals(++active, ((Vector ) PrivilegedAccessor.getValue(pool, 153 "activeConnections")).size()); 154 155 } 156 } 157 158 try 160 { 161 c = pool.getConnection(); 162 } 163 catch (UnreachableBackendException e) 164 { 165 fail("Backend unreachable during test."); 166 } 167 if (c != null) 168 fail("Got more connection than available from the pool"); 169 } 170 171 174 public void testReleaseConnection() throws Exception 175 { 176 Connection c = null; 178 try 179 { 180 c = pool.getConnection(); 181 } 182 catch (UnreachableBackendException e) 183 { 184 fail("Backend unreachable during test."); 185 } 186 if (c == null) 187 fail("Failed to get connection from the pool"); 188 assertEquals(POOL_SIZE - 1, ((Stack ) PrivilegedAccessor.getValue(pool, 189 "freeConnections")).size()); 190 assertEquals(1, ((Vector ) PrivilegedAccessor.getValue(pool, 191 "activeConnections")).size()); 192 193 pool.releaseConnection(c); 195 assertEquals(POOL_SIZE, ((Stack ) PrivilegedAccessor.getValue(pool, 196 "freeConnections")).size()); 197 assertEquals(0, ((Vector ) PrivilegedAccessor.getValue(pool, 198 "activeConnections")).size()); 199 200 } 201 } | Popular Tags |