📄 connectiontests.java
字号:
package poolman.tests.basic;import junit.framework.*;import poolman.tests.*;import java.sql.*;import javax.sql.*;import java.util.Enumeration;import java.util.Properties;import javax.naming.*;import com.codestudio.management.*;import com.codestudio.util.*;
import com.codestudio.sql.PoolMan;/** * This suite of tests cover basic connectivity scenarios * and the handling of Connection objects. */public class ConnectionTests extends TestCase { private String rawDrivername; private String rawURL; private Properties rawProps; private Context context; public ConnectionTests(String name) { super(name); } public static Test suite() { return new TestSuite(ConnectionTests.class); } /** Get the underlying driver information. */ protected void setUp() { // Get physical connection info from the test pool SQLManager manager = SQLManager.getInstance(); manager = SQLManager.getInstance(); JDBCPool pool = (JDBCPool) manager.getPool (PoolManTests.TEST_DB_NAME); this.rawDrivername = pool.getDriver(); this.rawURL = pool.getURL(); this.rawProps = new Properties(); rawProps.put("user", pool.getUserName()); rawProps.put("password", pool.getPassword()); // Get the NamingContext try { this.context = PoolManTests.getNamingContext(); } catch (Exception e) {} } protected void tearDown() { try { this.context.close(); } catch (Exception e) {} } public void testDataSourceUsage() { Connection con = null; try { //DataSource ds = (DataSource) context.lookup(PoolManTests.TEST_JNDI_NAME);
DataSource ds = PoolMan.getDataSource(PoolManTests.TEST_JNDI_NAME); con = ds.getConnection(); } catch (Exception e) { e.printStackTrace(); } assert (con != null); try { con.close(); } catch (Exception e) {} } public void testDriverManagerUsage() { Connection con = null; try { // load the PoolMan JDBC Driver Class.forName("com.codestudio.sql.PoolMan").newInstance(); // establish a Connection to the test database listed con = DriverManager.getConnection("jdbc:poolman://testdb"); } catch (Exception ex) {} assert(con !=null); try { con.close(); } catch (SQLException closeex) {} } /** Test PoolMan usage against standard JDBC usage. */ public void testConnectionComparison() { long poolmanElapsed = 0; long unpooledElapsed = 0; Connection con = null; //System.out.println(getClass().getName()); try { long start = System.currentTimeMillis(); con = getPoolManConnection(); poolmanElapsed = System.currentTimeMillis() - start; //System.out.println("\tPoolMan Connection Elapsed Time: " + poolmanElapsed); } catch (Exception e) {} finally { try { con.close(); } catch (SQLException sqe) {} } try { long start = System.currentTimeMillis(); con = getUnpooledConnection(); unpooledElapsed = System.currentTimeMillis() - start; //System.out.println("\tUnpooled Connection Elapsed Time: " + unpooledElapsed); } catch (Exception e) {} finally { try { con.close(); } catch (SQLException sqe) {} } assert(poolmanElapsed < unpooledElapsed); } public void testDereferenceConnection() { try { Connection con = getPoolManConnection(); con.close(); assert("Connection should have been dereferenced and unavailable to the caller", con.isClosed()); } catch (Exception e) {} } private Connection getPoolManConnection() { try { Class.forName("com.codestudio.sql.PoolMan").newInstance(); return DriverManager.getConnection("jdbc:poolman://testdb"); } catch (Exception sqle) { sqle.printStackTrace(); fail("Unable to retrieve PoolMan Connection: " + sqle); } return null; } private Connection getUnpooledConnection() { try { Class.forName(this.rawDrivername).newInstance(); return DriverManager.getConnection(this.rawURL, this.rawProps); } catch (Exception sqle) { sqle.printStackTrace(); fail("Unable to return Unpooled Connection: " + sqle.toString()); } return null; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -