1 16 17 package org.apache.commons.dbcp; 18 19 import java.io.IOException ; 20 import java.sql.Connection ; 21 import java.sql.SQLException ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 26 32 public class TestBasicDataSource extends TestConnectionPool { 33 public TestBasicDataSource(String testName) { 34 super(testName); 35 } 36 37 public static Test suite() { 38 return new TestSuite(TestBasicDataSource.class); 39 } 40 41 protected Connection getConnection() throws Exception { 42 return ds.getConnection(); 43 } 44 45 protected BasicDataSource ds = null; 46 private static String CATALOG = "test catalog"; 47 48 public void setUp() throws Exception { 49 super.setUp(); 50 ds = new BasicDataSource(); 51 ds.setDriverClassName("org.apache.commons.dbcp.TesterDriver"); 52 ds.setUrl("jdbc:apache:commons:testdriver"); 53 ds.setMaxActive(getMaxActive()); 54 ds.setMaxWait(getMaxWait()); 55 ds.setDefaultAutoCommit(true); 56 ds.setDefaultReadOnly(false); 57 ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); 58 ds.setDefaultCatalog(CATALOG); 59 ds.setUsername("username"); 60 ds.setPassword("password"); 61 ds.setValidationQuery("SELECT DUMMY FROM DUAL"); 62 } 63 64 public void tearDown() throws Exception { 65 super.tearDown(); 66 ds = null; 67 } 68 69 public void testTransactionIsolationBehavior() throws Exception { 70 Connection conn = getConnection(); 71 assertTrue(conn != null); 72 assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn.getTransactionIsolation()); 73 conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); 74 conn.close(); 75 76 Connection conn2 = getConnection(); 77 assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn2.getTransactionIsolation()); 78 79 Connection conn3 = getConnection(); 80 assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn3.getTransactionIsolation()); 81 82 conn2.close(); 83 84 conn3.close(); 85 } 86 87 public void testPooling() throws Exception { 88 ds.setAccessToUnderlyingConnectionAllowed(true); 90 super.testPooling(); 91 } 92 93 public void testNoAccessToUnderlyingConnectionAllowed() throws Exception { 94 assertEquals(false, ds.isAccessToUnderlyingConnectionAllowed()); 96 97 Connection conn = getConnection(); 98 Connection dconn = ((DelegatingConnection) conn).getDelegate(); 99 assertNull(dconn); 100 101 dconn = ((DelegatingConnection) conn).getInnermostDelegate(); 102 assertNull(dconn); 103 } 104 105 public void testAccessToUnderlyingConnectionAllowed() throws Exception { 106 ds.setAccessToUnderlyingConnectionAllowed(true); 107 assertEquals(true, ds.isAccessToUnderlyingConnectionAllowed()); 108 109 Connection conn = getConnection(); 110 Connection dconn = ((DelegatingConnection) conn).getDelegate(); 111 assertNotNull(dconn); 112 113 dconn = ((DelegatingConnection) conn).getInnermostDelegate(); 114 assertNotNull(dconn); 115 116 assertTrue(dconn instanceof TesterConnection); 117 } 118 119 public void testEmptyValidationQuery() throws Exception { 120 assertNotNull(ds.getValidationQuery()); 121 122 ds.setValidationQuery(""); 123 assertNull(ds.getValidationQuery()); 124 125 ds.setValidationQuery(" "); 126 assertNull(ds.getValidationQuery()); 127 } 128 129 public void testInvalidValidationQuery() { 130 try { 131 ds.setValidationQuery("invalid"); 132 ds.getConnection(); 133 fail("expected SQLException"); 134 } 135 catch (SQLException e) { 136 if (e.toString().indexOf("invalid") < 0) { 137 fail("expected detailed error message"); 138 } 139 } 140 } 141 142 public void testSetValidationTestProperties() { 143 assertEquals(true, ds.getTestOnBorrow()); 145 assertEquals(false, ds.getTestOnReturn()); 146 assertEquals(false, ds.getTestWhileIdle()); 147 148 ds.setTestOnBorrow(true); 149 ds.setTestOnReturn(true); 150 ds.setTestWhileIdle(true); 151 assertEquals(true, ds.getTestOnBorrow()); 152 assertEquals(true, ds.getTestOnReturn()); 153 assertEquals(true, ds.getTestWhileIdle()); 154 155 ds.setTestOnBorrow(false); 156 ds.setTestOnReturn(false); 157 ds.setTestWhileIdle(false); 158 assertEquals(false, ds.getTestOnBorrow()); 159 assertEquals(false, ds.getTestOnReturn()); 160 assertEquals(false, ds.getTestWhileIdle()); 161 } 162 163 public void testNoValidationQuery() throws Exception { 164 ds.setTestOnBorrow(true); 165 ds.setTestOnReturn(true); 166 ds.setTestWhileIdle(true); 167 ds.setValidationQuery(""); 168 169 Connection conn = ds.getConnection(); 170 conn.close(); 171 172 assertEquals(false, ds.getTestOnBorrow()); 173 assertEquals(false, ds.getTestOnReturn()); 174 assertEquals(false, ds.getTestWhileIdle()); 175 } 176 177 public void testDefaultCatalog() throws Exception { 178 Connection [] c = new Connection [getMaxActive()]; 179 for (int i = 0; i < c.length; i++) { 180 c[i] = getConnection(); 181 assertTrue(c[i] != null); 182 assertEquals(CATALOG, c[i].getCatalog()); 183 } 184 185 for (int i = 0; i < c.length; i++) { 186 c[i].setCatalog("error"); 187 c[i].close(); 188 } 189 190 for (int i = 0; i < c.length; i++) { 191 c[i] = getConnection(); 192 assertTrue(c[i] != null); 193 assertEquals(CATALOG, c[i].getCatalog()); 194 } 195 196 for (int i = 0; i < c.length; i++) { 197 c[i].close(); 198 } 199 } 200 201 public void testSetAutoCommitTrueOnClose() throws Exception { 202 ds.setAccessToUnderlyingConnectionAllowed(true); 203 ds.setDefaultAutoCommit(false); 204 205 Connection conn = getConnection(); 206 assertNotNull(conn); 207 assertEquals(false, conn.getAutoCommit()); 208 209 Connection dconn = ((DelegatingConnection) conn).getInnermostDelegate(); 210 assertNotNull(dconn); 211 assertEquals(false, dconn.getAutoCommit()); 212 213 conn.close(); 214 215 assertEquals(true, dconn.getAutoCommit()); 216 } 217 218 public void testInitialSize() throws Exception { 219 ds.setMaxActive(20); 220 ds.setMaxIdle(20); 221 ds.setInitialSize(10); 222 223 Connection conn = getConnection(); 224 assertNotNull(conn); 225 conn.close(); 226 227 assertEquals(0, ds.getNumActive()); 228 assertEquals(10, ds.getNumIdle()); 229 } 230 231 public void testIsClosedFailure() throws SQLException { 234 ds.setAccessToUnderlyingConnectionAllowed(true); 235 Connection conn = ds.getConnection(); 236 assertNotNull(conn); 237 assertEquals(1, ds.getNumActive()); 238 239 TesterConnection tconn = (TesterConnection) ((DelegatingConnection)conn).getInnermostDelegate(); 241 tconn.setFailure(new IOException ("network error")); 242 243 try { 244 conn.close(); 245 fail("Expected SQLException"); 246 } 247 catch(SQLException ex) { } 248 249 assertEquals(0, ds.getNumActive()); 250 } 251 252 257 public void testPropertyTestOnReturn() throws Exception { 258 ds.setValidationQuery("select 1 from dual"); 259 ds.setTestOnBorrow(false); 260 ds.setTestWhileIdle(false); 261 ds.setTestOnReturn(true); 262 263 Connection conn = ds.getConnection(); 264 assertNotNull(conn); 265 266 assertEquals(false, ds.connectionPool.getTestOnBorrow()); 267 assertEquals(false, ds.connectionPool.getTestWhileIdle()); 268 assertEquals(true, ds.connectionPool.getTestOnReturn()); 269 } 270 271 276 public void testRollbackReadOnly() throws Exception { 277 ds.setDefaultReadOnly(true); 278 ds.setDefaultAutoCommit(false); 279 280 Connection conn = ds.getConnection(); 281 assertNotNull(conn); 282 conn.close(); 283 } 284 } 285 | Popular Tags |