connectionregressiontest.java
来自「开发MySql数据库的最新JDBC驱动。」· Java 代码 · 共 2,143 行 · 第 1/4 页
JAVA
2,143 行
if (con != null) { try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } } } /** * Tests fix for BUG#9206, can not use 'UTF-8' for characterSetResults * configuration property. */ public void testBug9206() throws Exception { Properties props = new Properties(); props.setProperty("characterSetResults", "UTF-8"); getConnectionWithProps(props).close(); } /** * These two charsets have different names depending on version of MySQL * server. * * @throws Exception * if the test fails. */ public void testNewCharsetsConfiguration() throws Exception { Properties props = new Properties(); props.setProperty("useUnicode", "true"); props.setProperty("characterEncoding", "EUC_KR"); getConnectionWithProps(props).close(); props = new Properties(); props.setProperty("useUnicode", "true"); props.setProperty("characterEncoding", "KOI8_R"); getConnectionWithProps(props).close(); } /** * Tests fix for BUG#10144 - Memory leak in ServerPreparedStatement if * serverPrepare() fails. */ public void testBug10144() throws Exception { if (versionMeetsMinimum(4, 1)) { Properties props = new Properties(); props.setProperty("emulateUnsupportedPstmts", "false"); props.setProperty("useServerPrepStmts", "true"); Connection bareConn = getConnectionWithProps(props); int currentOpenStatements = ((com.mysql.jdbc.Connection) bareConn) .getActiveStatementCount(); try { bareConn.prepareStatement("Boo!"); fail("Should not've been able to prepare that one!"); } catch (SQLException sqlEx) { assertEquals(currentOpenStatements, ((com.mysql.jdbc.Connection) bareConn) .getActiveStatementCount()); } finally { if (bareConn != null) { bareConn.close(); } } } } /** * Tests fix for BUG#10496 - SQLException is thrown when using property * "characterSetResults" */ public void testBug10496() throws Exception { if (versionMeetsMinimum(5, 0, 3)) { Properties props = new Properties(); props.setProperty("useUnicode", "true"); props.setProperty("characterEncoding", "WINDOWS-31J"); props.setProperty("characterSetResults", "WINDOWS-31J"); getConnectionWithProps(props).close(); props = new Properties(); props.setProperty("useUnicode", "true"); props.setProperty("characterEncoding", "EUC_JP"); props.setProperty("characterSetResults", "EUC_JP"); getConnectionWithProps(props).close(); } } /** * Tests fix for BUG#11259, autoReconnect ping causes exception on * connection startup. * * @throws Exception * if the test fails. */ public void testBug11259() throws Exception { Connection dsConn = null; try { Properties props = new Properties(); props.setProperty("autoReconnect", "true"); dsConn = getConnectionWithProps(props); } finally { if (dsConn != null) { dsConn.close(); } } } /** * Tests fix for BUG#11879 -- ReplicationConnection won't switch to slave, * throws "Catalog can't be null" exception. * * @throws Exception * if the test fails */ public void testBug11879() throws Exception { if (runMultiHostTests()) { Connection replConn = null; try { replConn = getMasterSlaveReplicationConnection(); replConn.setReadOnly(true); replConn.setReadOnly(false); } finally { if (replConn != null) { replConn.close(); } } } } /** * Tests fix for BUG#11976 - maxPerformance.properties mis-spells * "elideSetAutoCommits". * * @throws Exception * if the test fails. */ public void testBug11976() throws Exception { if (isRunningOnJdk131()) { return; // test not valid on JDK-1.3.1 } Properties props = new Properties(); props.setProperty("useConfigs", "maxPerformance"); Connection maxPerfConn = getConnectionWithProps(props); assertEquals(true, ((com.mysql.jdbc.Connection) maxPerfConn) .getElideSetAutoCommits()); } /** * Tests fix for BUG#12218, properties shared between master and slave with * replication connection. * * @throws Exception * if the test fails. */ public void testBug12218() throws Exception { if (runMultiHostTests()) { Connection replConn = null; try { replConn = getMasterSlaveReplicationConnection(); assertTrue(!((ReplicationConnection) replConn) .getMasterConnection().hasSameProperties( ((ReplicationConnection) replConn) .getSlavesConnection())); } finally { if (replConn != null) { replConn.close(); } } } } /** * Tests fix for BUG#12229 - explainSlowQueries hangs with server-side * prepared statements. * * @throws Exception * if the test fails. */ public void testBug12229() throws Exception { createTable("testBug12229", "(`int_field` integer )"); this.stmt.executeUpdate("insert into testBug12229 values (123456),(1)"); Properties props = new Properties(); props.put("profileSQL", "true"); props.put("slowQueryThresholdMillis", "0"); props.put("logSlowQueries", "true"); props.put("explainSlowQueries", "true"); props.put("useServerPrepStmts", "true"); Connection explainConn = getConnectionWithProps(props); this.pstmt = explainConn .prepareStatement("SELECT `int_field` FROM `testBug12229` WHERE `int_field` = ?"); this.pstmt.setInt(1, 1); this.rs = this.pstmt.executeQuery(); assertTrue(this.rs.next()); this.rs = this.pstmt.executeQuery(); assertTrue(this.rs.next()); this.rs = this.pstmt.executeQuery(); assertTrue(this.rs.next()); } /** * Tests fix for BUG#12752 - Cp1251 incorrectly mapped to win1251 for * servers newer than 4.0.x. * * @throws Exception * if the test fails. */ public void testBug12752() throws Exception { Properties props = new Properties(); props.setProperty("characterEncoding", "Cp1251"); getConnectionWithProps(props).close(); } /** * Tests fix for BUG#12753, sessionVariables=....=...., doesn't work as it's * tokenized incorrectly. * * @throws Exception * if the test fails. */ public void testBug12753() throws Exception { if (versionMeetsMinimum(4, 1)) { Properties props = new Properties(); props.setProperty("sessionVariables", "sql_mode=ansi"); Connection sessionConn = null; try { sessionConn = getConnectionWithProps(props); String sqlMode = getMysqlVariable(sessionConn, "sql_mode"); assertTrue(sqlMode.indexOf("ANSI") != -1); } finally { if (sessionConn != null) { sessionConn.close(); sessionConn = null; } } } } /** * Tests fix for BUG#13048 - maxQuerySizeToLog is not respected. * * @throws Exception * if the test fails */ public void testBug13048() throws Exception { Connection profileConn = null; PrintStream oldErr = System.err; try { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); System.setErr(new PrintStream(bOut)); Properties props = new Properties(); props.setProperty("profileSQL", "true"); props.setProperty("maxQuerySizeToLog", "2"); props.setProperty("logger", "com.mysql.jdbc.log.StandardLogger"); profileConn = getConnectionWithProps(props); StringBuffer queryBuf = new StringBuffer("SELECT '"); for (int i = 0; i < 500; i++) { queryBuf.append("a"); } queryBuf.append("'"); this.rs = profileConn.createStatement().executeQuery( queryBuf.toString()); this.rs.close(); String logString = new String(bOut.toString("ISO8859-1")); assertTrue(logString.indexOf("... (truncated)") != -1); bOut = new ByteArrayOutputStream(); System.setErr(new PrintStream(bOut)); this.rs = profileConn.prepareStatement(queryBuf.toString()) .executeQuery(); logString = new String(bOut.toString("ISO8859-1")); assertTrue(logString.indexOf("... (truncated)") != -1); } finally { System.setErr(oldErr); if (profileConn != null) { profileConn.close(); } if (this.rs != null) { ResultSet toClose = this.rs; this.rs = null; toClose.close(); } } } /** * Tests fix for BUG#13453 - can't use & or = in URL configuration values * (we now allow you to use www-form-encoding). * * @throws Exception * if the test fails */ public void testBug13453() throws Exception { StringBuffer urlBuf = new StringBuffer(dbUrl); if (dbUrl.indexOf('?') == -1) { urlBuf.append('?'); } else { urlBuf.append('&'); } urlBuf.append("sessionVariables=@testBug13453='%25%26+%3D'"); Connection encodedConn = null; try { encodedConn = DriverManager.getConnection(urlBuf.toString(), null); this.rs = encodedConn.createStatement().executeQuery( "SELECT @testBug13453"); assertTrue(this.rs.next()); assertEquals("%& =", this.rs.getString(1)); } finally { if (this.rs != null) { this.rs.close(); this.rs = null; } if (encodedConn != null) { encodedConn.close(); } } } /** * Tests fix for BUG#15065 - Usage advisor complains about unreferenced * columns, even though they've been referenced. * * @throws Exception * if the test fails. */ public void testBug15065() throws Exception { if (isRunningOnJdk131()) { return; // test not valid on JDK-1.3.1 } createTable("testBug15065", "(field1 int)"); this.stmt.executeUpdate("INSERT INTO testBug15065 VALUES (1)"); Connection advisorConn = null; Statement advisorStmt = null; try { Properties props = new Properties(); props.setProperty("useUsageAdvisor", "true"); props.setProperty("logger", "com.mysql.jdbc.log.StandardLogger"); advisorConn = getConnectionWithProps(props); advisorStmt = advisorConn.createStatement(); Method[] getMethods = ResultSet.class.getMethods(); PrintStream oldErr = System.err; try { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); System.setErr(new PrintStream(bOut)); HashMap methodsToSkipMap = new HashMap(); // Needs an actual URL methodsToSkipMap.put("getURL", null); // Java6 JDBC4.0 methods we don't implement methodsToSkipMap.put("getNCharacterStream", null); methodsToSkipMap.put("getNClob", null); methodsToSkipMap.put("getNString", null); methodsToSkipMap.put("getRowId", null); methodsToSkipMap.put("getSQLXML", null); for (int j = 0; j < 2; j++) { for (int i = 0; i < getMethods.length; i++) { String methodName = getMethods[i].getName(); if (methodName.startsWith("get") && !methodsToSkipMap.containsKey(methodName)) { Class[] parameterTypes = getMethods[i] .getParameterTypes(); if (parameterTypes.length == 1 && parameterTypes[0] == Integer.TYPE) { if (j == 0) { this.rs = advisorStmt .executeQuery("SELECT COUNT(*) FROM testBug15065"); } else { this.rs = advisorConn .prepareStatement( "SELECT COUNT(*) FROM testBug15065") .executeQuery(); } this.rs.next(); try { getMethods[i].invoke(this.rs, new Object[] { new Integer(1) }); } catch (InvocationTargetException invokeEx) { // we don't care about bad values, just that // the // column gets "touched" if (!invokeEx .getCause() .getClass() .isAssignableFrom( java.sql.SQLException.class) && !invokeEx .getCause() .getClass() .getName() .equals( "com.mysql.jdbc.NotImplemented")) { throw invokeEx; } } this.rs.close(); this.rs = null; } } } } String logOut = bOut.toString("ISO8859-1"); if (logOut.indexOf(".Level") != -1) { return; // we ignore for warnings } assertTrue("Usage advisor complained about columns:\n\n" + logOut, logOut.indexOf("columns") == -1); } finally { System.setErr(oldErr); } } finally { if (advisorConn != null) { advisorConn.close(); } } } /** * Tests fix for BUG#15544, no "dos" character set in MySQL > 4.1.0 * * @throws Exception * if the test fails */ public void testBug15544() throws Exception { Properties props = new Properties(); props.setProperty("characterEncoding", "Cp437"); Connection dosConn = null; try { dosConn = getConnectionWithProps(props); } finally { if (dosConn != null) { dosConn.close(); } } } public void testCSC5765() throws Exception { if (isRunningOnJdk131()) { return; // test not valid on JDK-1.3.1 } Properties props = new Properties(); props.setProperty("useUnicode", "true"); props.setProperty("characterEncoding", "utf8"); props.setProperty("characterSetResults", "utf8"); props.setProperty("connectionCollation", "utf8_bin"); Connection utf8Conn = null; try { utf8Conn = getConnectionWithProps(props); this.rs = utf8Conn.createStatement().executeQuery( "SHOW VARIABLES LIKE 'character_%'"); while (this.rs.next()) { System.out.println(this.rs.getString(1) + " = " + this.rs.getString(2)); } this.rs = utf8Conn.createStatement().executeQuery( "SHOW VARIABLES LIKE 'collation_%'"); while (this.rs.next()) { System.out.println(this.rs.getString(1) + " = " + this.rs.getString(2)); } } finally { if (utf8Conn != null) { utf8Conn.close(); } } } private Connection getMasterSlaveReplicationConnection() throws SQLException { Connection replConn = new ReplicationDriver().connect( getMasterSlaveUrl(), getMasterSlaveProps()); return replConn; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?