📄 connectionregressiontest.java
字号:
"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") && !invokeEx .getCause() .getClass() .getName() .equals( "java.sql.SQLFeatureNotSupportedException")) { 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(); } } } protected Connection getMasterSlaveReplicationConnection() throws SQLException { Connection replConn = new ReplicationDriver().connect( getMasterSlaveUrl(), getMasterSlaveProps()); return replConn; } protected String getMasterSlaveUrl() throws SQLException { StringBuffer urlBuf = new StringBuffer("jdbc:mysql://"); Properties defaultProps = getPropertiesFromTestsuiteUrl(); String hostname = defaultProps .getProperty(NonRegisteringDriver.HOST_PROPERTY_KEY); int colonIndex = hostname.indexOf(":"); String portNumber = "3306"; if (colonIndex != -1 && !hostname.startsWith(":")) { portNumber = hostname.substring(colonIndex + 1); hostname = hostname.substring(0, colonIndex); } else if (hostname.startsWith(":")) { portNumber = hostname.substring(1); hostname = "localhost"; } else { portNumber = defaultProps .getProperty(NonRegisteringDriver.PORT_PROPERTY_KEY); } for (int i = 0; i < 2; i++) { urlBuf.append(hostname); urlBuf.append(":"); urlBuf.append(portNumber); if (i == 0) { urlBuf.append(","); } } urlBuf.append("/"); return urlBuf.toString(); } protected Properties getMasterSlaveProps() throws SQLException { Properties props = getPropertiesFromTestsuiteUrl(); props.remove(NonRegisteringDriver.HOST_PROPERTY_KEY); props.remove(NonRegisteringDriver.PORT_PROPERTY_KEY); return props; } /** * Tests fix for BUG#15570 - ReplicationConnection incorrectly copies state, * doesn't transfer connection context correctly when transitioning between * the same read-only states. * * (note, this test will fail if the test user doesn't have permission to * "USE 'mysql'". * * @throws Exception * if the test fails. */ public void testBug15570() throws Exception { Connection replConn = null; try { replConn = getMasterSlaveReplicationConnection(); int masterConnectionId = Integer .parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString()); replConn.setReadOnly(false); assertEquals(masterConnectionId, Integer .parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString())); String currentCatalog = replConn.getCatalog(); replConn.setCatalog(currentCatalog); assertEquals(currentCatalog, replConn.getCatalog()); replConn.setReadOnly(true); int slaveConnectionId = Integer .parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString()); // The following test is okay for now, as the chance // of MySQL wrapping the connection id counter during our // testsuite is very small. assertTrue("Slave id " + slaveConnectionId + " is not newer than master id " + masterConnectionId, slaveConnectionId > masterConnectionId); assertEquals(currentCatalog, replConn.getCatalog()); String newCatalog = "mysql"; replConn.setCatalog(newCatalog); assertEquals(newCatalog, replConn.getCatalog()); replConn.setReadOnly(true); assertEquals(newCatalog, replConn.getCatalog()); replConn.setReadOnly(false); assertEquals(masterConnectionId, Integer .parseInt(getSingleIndexedValueWithQuery(replConn, 1, "SELECT CONNECTION_ID()").toString())); } finally { if (replConn != null) { replConn.close(); } } } /** * Tests bug where downed slave caused round robin load balance not to * cycle back to first host in the list. * * @throws Exception * if the test fails...Note, test is timing-dependent, but * should work in most cases. */ public void testBug23281() throws Exception { Properties props = new Driver().parseURL(BaseTestCase.dbUrl, null); props.setProperty("autoReconnect", "false"); props.setProperty("roundRobinLoadBalance", "true"); props.setProperty("failoverReadOnly", "false"); if (!isRunningOnJdk131()) { props.setProperty("connectTimeout", "5000"); } // Re-build the connection information int firstIndexOfHost = BaseTestCase.dbUrl.indexOf("//") + 2; int lastIndexOfHost = BaseTestCase.dbUrl.indexOf("/", firstIndexOfHost); String hostPortPair = BaseTestCase.dbUrl.substring(firstIndexOfHost, lastIndexOfHost); StringTokenizer st = new StringTokenizer(hostPortPair, ":"); String host = null; String port = null; if (st.hasMoreTokens()) { String possibleHostOrPort = st.nextToken(); if (Character.isDigit(possibleHostOrPort.charAt(0)) && (possibleHostOrPort.indexOf(".") == -1 /* IPV4 */) && (possibleHostOrPort.indexOf("::") == -1 /* IPV6 */)) { port = possibleHostOrPort; host = "localhost"; } else { host = possibleHostOrPort; } } if (st.hasMoreTokens()) { port = st.nextToken(); } if (host == null) { host = ""; } if (port == null) { port = "3306"; } StringBuffer newHostBuf = new StringBuffer(); newHostBuf.append(host); if (port != null) { newHostBuf.append(":"); newHostBuf.append(port); } newHostBuf.append(","); //newHostBuf.append(host); newHostBuf.append("192.0.2.1"); // non-exsitent machine from RFC3330 test network newHostBuf.append(":65532"); // make sure the slave fails props.remove("PORT"); props.remove("HOST"); Connection failoverConnection = null; try { failoverConnection = getConnectionWithProps("jdbc:mysql://" + newHostBuf.toString() + "/", props); String originalConnectionId = getSingleIndexedValueWithQuery( failoverConnection, 1, "SELECT CONNECTION_ID()").toString(); System.out.println(originalConnectionId); Connection nextConnection = getConnectionWithProps("jdbc:mysql://" + newHostBuf.toString() + "/", props); String nextId = getSingleIndexedValueWithQuery( nextConnection, 1, "SELECT CONNECTION_ID()").toString(); System.out.println(nextId); } finally { if (failoverConnection != null) { failoverConnection.close(); } } } /** * Tests to insure proper behavior for BUG#24706. * * @throws Exception if the test fails. */ public void testBug24706() throws Exception { if (!versionMeetsMinimum(5, 0)) { return; // server status isn't there to support this feature } Properties props = new Properties(); props.setProperty("elideSetAutoCommits", "true"); props.setProperty("logger", "StandardLogger"); props.setProperty("profileSQL", "true"); Connection c = null; StringBuffer logBuf = new StringBuffer(); StandardLogger.bufferedLog = logBuf; try { c = getConnectionWithProps(props); c.setAutoCommit(true); c.createStatement().execute("SELECT 1"); c.setAutoCommit(true); c.setAutoCommit(false); c.createStatement().execute("SELECT 1"); c.setAutoCommit(false); // We should only see _one_ "set autocommit=" sent to the server String log = logBuf.toString(); int searchFrom = 0; int count = 0; int found = 0; while ((found = log.indexOf("SET autocommit=", searchFrom)) != -1) { searchFrom = found + 1; count++; } // The SELECT doesn't actually start a transaction, so being pedantic the // driver issues SET autocommit=0 again in this case. assertEquals(2, count); } finally { StandardLogger.bufferedLog = null; if (c != null) { c.close(); } } } /** * Tests fix for BUG#25514 - Timer instance used for Statement.setQueryTimeout() * created per-connection, rather than per-VM, causing memory leak. * * @throws Exception if the test fails.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -