1 16 17 package org.apache.commons.dbcp; 18 19 import java.io.PrintStream ; 20 import java.io.PrintWriter ; 21 import java.sql.Connection ; 22 import java.sql.DriverManager ; 23 import java.sql.SQLException ; 24 25 import junit.framework.Test; 26 import junit.framework.TestSuite; 27 28 import org.apache.commons.pool.ObjectPool; 29 import org.apache.commons.pool.impl.GenericKeyedObjectPool; 30 import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory; 31 import org.apache.commons.pool.impl.GenericObjectPool; 32 33 40 public class TestManual extends TestConnectionPool { 41 public TestManual(String testName) { 42 super(testName); 43 } 44 45 public static Test suite() { 46 return new TestSuite(TestManual.class); 47 } 48 49 protected Connection getConnection() throws Exception { 50 return DriverManager.getConnection("jdbc:apache:commons:dbcp:test"); 51 } 52 53 private PoolingDriver driver = null; 54 55 public void setUp() throws Exception { 56 super.setUp(); 57 GenericObjectPool pool = new GenericObjectPool(null, getMaxActive(), GenericObjectPool.WHEN_EXHAUSTED_BLOCK, getMaxWait(), 10, true, true, 10000L, 5, 5000L, true); 58 DriverConnectionFactory cf = new DriverConnectionFactory(new TesterDriver(),"jdbc:apache:commons:testdriver",null); 59 GenericKeyedObjectPoolFactory opf = new GenericKeyedObjectPoolFactory(null, 10, GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK, 2000L, 10, true, true, 10000L, 5, 5000L, true); 60 PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, pool, opf, "SELECT COUNT(*) FROM DUAL", false, true); 61 assertNotNull(pcf); 62 driver = new PoolingDriver(); 63 driver.registerPool("test",pool); 64 PoolingDriver.setAccessToUnderlyingConnectionAllowed(true); 65 DriverManager.registerDriver(driver); 66 } 67 68 public void tearDown() throws Exception { 69 driver.closePool("test"); 70 DriverManager.deregisterDriver(driver); 71 } 72 73 74 public void testReportedBug28912() throws Exception { 75 Connection conn1 = getConnection(); 76 assertNotNull(conn1); 77 conn1.close(); 78 79 Connection conn2 = getConnection(); 80 assertNotNull(conn2); 81 82 try { 83 conn1.close(); 84 fail("Expected SQLException"); 85 } 86 catch (SQLException e) { } 87 } 88 89 90 public void testReportedBug12400() throws Exception { 91 ObjectPool connectionPool = new GenericObjectPool( 92 null, 93 70, 94 GenericObjectPool.WHEN_EXHAUSTED_BLOCK, 95 60000, 96 10); 97 ConnectionFactory connectionFactory = new DriverManagerConnectionFactory( 98 "jdbc:apache:commons:testdriver", 99 "username", 100 "password"); 101 PoolableConnectionFactory poolableConnectionFactory = 102 new PoolableConnectionFactory( 103 connectionFactory, 104 connectionPool, 105 null, 106 null, 107 false, 108 true); 109 assertNotNull(poolableConnectionFactory); 110 PoolingDriver driver = new PoolingDriver(); 111 driver.registerPool("neusoftim",connectionPool); 112 Connection [] conn = new Connection [25]; 113 for(int i=0;i<25;i++) { 114 conn[i] = DriverManager.getConnection("jdbc:apache:commons:dbcp:neusoftim"); 115 for(int j=0;j<i;j++) { 116 assertTrue(conn[j] != conn[i]); 117 assertTrue(!conn[j].equals(conn[i])); 118 } 119 } 120 for(int i=0;i<25;i++) { 121 conn[i].close(); 122 } 123 } 124 125 public void testClosePool() throws Exception { 126 Connection conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:test"); 127 assertNotNull(conn); 128 conn.close(); 129 130 PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); 131 driver.closePool("test"); 132 133 try { 134 conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:test"); 135 fail("expected SQLException"); 136 } 137 catch (SQLException e) { 138 } 140 } 141 142 public void testLogWriter() throws Exception { 143 PrintStream ps = System.out; 144 PrintWriter pw = new PrintWriter (System.err); 145 SQLException ex; 146 147 DriverManager.setLogWriter(pw); 148 ex = new SQLNestedException("A", new Exception ("a")); 149 ex.printStackTrace(); 150 ex.printStackTrace(ps); 151 ex.printStackTrace(pw); 152 ex = new SQLNestedException("B", null); 153 ex.printStackTrace(); 154 ex.printStackTrace(ps); 155 ex.printStackTrace(pw); 156 ex = new SQLNestedException(null, new Exception ("c")); 157 ex.printStackTrace(); 158 ex.printStackTrace(ps); 159 ex.printStackTrace(pw); 160 ex = new SQLNestedException(null, null); 161 ex.printStackTrace(); 162 ex.printStackTrace(ps); 163 ex.printStackTrace(pw); 164 165 DriverManager.setLogWriter(null); 166 ex = new SQLNestedException("A", new Exception ("a")); 167 ex.printStackTrace(); 168 ex.printStackTrace(ps); 169 ex.printStackTrace(pw); 170 ex = new SQLNestedException("B", null); 171 ex.printStackTrace(); 172 ex.printStackTrace(ps); 173 ex.printStackTrace(pw); 174 ex = new SQLNestedException(null, new Exception ("c")); 175 ex.printStackTrace(); 176 ex.printStackTrace(ps); 177 ex.printStackTrace(pw); 178 ex = new SQLNestedException(null, null); 179 ex.printStackTrace(); 180 ex.printStackTrace(ps); 181 ex.printStackTrace(pw); 182 } 183 } 184 | Popular Tags |