⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringregressiontest.java

📁 开发MySql数据库的最新JDBC驱动。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		String origString = new String(origByteStream, "SJIS");		byte[] newByteStream = StringUtils.getBytes(origString, "SJIS",				"ISO8859_1              ", false, null);		//		// Print the hex values of the string (should have an extra 0x5c)		//		bytesOut = new StringBuffer();		for (int i = 0; i < newByteStream.length; i++) {			bytesOut.append(Integer.toHexString(newByteStream[i] & 255));			bytesOut.append(" ");		}		System.out.println(bytesOut.toString());		//		// Now, insert and retrieve the value from the database		//		Connection sjisConn = null;		Statement sjisStmt = null;		try {			Properties props = new Properties();			props.put("useUnicode", "true");			props.put("characterEncoding", "SJIS");			sjisConn = getConnectionWithProps(props);			sjisStmt = sjisConn.createStatement();			this.rs = sjisStmt					.executeQuery("SHOW VARIABLES LIKE 'character_set%'");			while (this.rs.next()) {				System.out.println(this.rs.getString(1) + " = "						+ this.rs.getString(2));			}			sjisStmt.executeUpdate("DROP TABLE IF EXISTS sjisTest");			if (versionMeetsMinimum(4, 1)) {				sjisStmt						.executeUpdate("CREATE TABLE sjisTest (field1 char(50)) DEFAULT CHARACTER SET SJIS");			} else {				sjisStmt						.executeUpdate("CREATE TABLE sjisTest (field1 char(50))");			}			this.pstmt = sjisConn					.prepareStatement("INSERT INTO sjisTest VALUES (?)");			this.pstmt.setString(1, origString);			this.pstmt.executeUpdate();			this.rs = sjisStmt.executeQuery("SELECT * FROM sjisTest");			while (this.rs.next()) {				byte[] testValueAsBytes = this.rs.getBytes(1);				bytesOut = new StringBuffer();				for (int i = 0; i < testValueAsBytes.length; i++) {					bytesOut.append(Integer							.toHexString(testValueAsBytes[i] & 255));					bytesOut.append(" ");				}				System.out.println("Value retrieved from database: "						+ bytesOut.toString());				String testValue = this.rs.getString(1);				assertTrue(testValue.equals(origString));			}		} finally {			this.stmt.executeUpdate("DROP TABLE IF EXISTS sjisTest");		}	}	/**	 * Tests that UTF-8 character conversion works correctly.	 * 	 * @throws Exception	 *             if any errors occur	 */	public void testUtf8Encoding() throws Exception {		Properties props = new Properties();		props.put("characterEncoding", "UTF8");		props.put("useUnicode", "true");		props.put("jdbcCompliantTruncation", "false");		Connection utfConn = DriverManager.getConnection(dbUrl, props);		testConversionForString("UTF8", utfConn, "\u043c\u0438\u0445\u0438");	}	/**	 * DOCUMENT ME!	 * 	 * @throws Exception	 *             ...	 */	public void testUtf8Encoding2() throws Exception {		String field1 = "K��sel";		String field2 = "B�b";		byte[] field1AsBytes = field1.getBytes("utf-8");		byte[] field2AsBytes = field2.getBytes("utf-8");		Properties props = new Properties();		props.put("characterEncoding", "UTF8");		props.put("useUnicode", "true");		Connection utfConn = DriverManager.getConnection(dbUrl, props);		Statement utfStmt = utfConn.createStatement();		try {			utfStmt.executeUpdate("DROP TABLE IF EXISTS testUtf8");			utfStmt					.executeUpdate("CREATE TABLE testUtf8 (field1 varchar(32), field2 varchar(32)) CHARACTER SET UTF8");			utfStmt.executeUpdate("INSERT INTO testUtf8 VALUES ('" + field1					+ "','" + field2 + "')");			PreparedStatement pStmt = utfConn					.prepareStatement("INSERT INTO testUtf8 VALUES (?, ?)");			pStmt.setString(1, field1);			pStmt.setString(2, field2);			pStmt.executeUpdate();			ResultSet rs = utfStmt.executeQuery("SELECT * FROM testUtf8");			assertTrue(rs.next());			// Compare results stored using direct statement			// Compare to original string			assertTrue(field1.equals(rs.getString(1)));			assertTrue(field2.equals(rs.getString(2)));			// Compare byte-for-byte, ignoring encoding			assertTrue(bytesAreSame(field1AsBytes, rs.getBytes(1)));			assertTrue(bytesAreSame(field2AsBytes, rs.getBytes(2)));			assertTrue(rs.next());			// Compare to original string			assertTrue(field1.equals(rs.getString(1)));			assertTrue(field2.equals(rs.getString(2)));			// Compare byte-for-byte, ignoring encoding			assertTrue(bytesAreSame(field1AsBytes, rs.getBytes(1)));			assertTrue(bytesAreSame(field2AsBytes, rs.getBytes(2)));		} finally {			utfStmt.executeUpdate("DROP TABLE IF EXISTS testUtf8");		}	}	private boolean bytesAreSame(byte[] byte1, byte[] byte2) {		if (byte1.length != byte2.length) {			return false;		}		for (int i = 0; i < byte1.length; i++) {			if (byte1[i] != byte2[i]) {				return false;			}		}		return true;	}	private void testConversionForString(String charsetName,			Connection convConn, String charsToTest) throws Exception {		PreparedStatement pStmt = null;		try {			this.stmt = convConn.createStatement();			this.stmt.executeUpdate("DROP TABLE IF EXISTS charConvTest");			this.stmt					.executeUpdate("CREATE TABLE charConvTest (field1 varchar(255))");			this.stmt.executeUpdate("INSERT INTO charConvTest VALUES ('"					+ charsToTest + "')");			this.stmt.executeUpdate("DROP TABLE IF EXISTS charConvTest_"					+ charsetName);			if (!versionMeetsMinimum(4, 1)) {				this.stmt.executeUpdate("CREATE TABLE charConvTest_"						+ charsetName + "(field1 CHAR(50))");			} else {				this.stmt.executeUpdate("CREATE TABLE charConvTest_"						+ charsetName + "(field1 CHAR(50) CHARACTER SET "						+ charsetName + ")");			}			this.stmt.executeUpdate("INSERT INTO charConvTest_" + charsetName					+ " VALUES ('" + charsToTest + "')");			pStmt = convConn.prepareStatement("INSERT INTO charConvTest_"					+ charsetName + " VALUES (?)");			pStmt.setString(1, charsToTest);			pStmt.executeUpdate();			this.rs = this.stmt.executeQuery("SELECT * FROM charConvTest_"					+ charsetName);			boolean hadRows = false;			assertTrue(this.rs.next());			String testValue = this.rs.getString(1);			System.out.println(testValue);			assertTrue(testValue.equals(charsToTest));		} finally {			this.stmt.executeUpdate("DROP TABLE IF EXISTS charConvTest_"					+ charsetName);		}	}	private void testConversionForString(String charsetName, String charsToTest)			throws Exception {		testConversionForString(charsetName, this.conn, charsToTest);	}	/**	 * Tests fix for BUG#7601, '+' duplicated in fixDecimalExponent().	 * 	 * @throws Exception	 *             if the test fails	 */	public void testBug7601() throws Exception {		assertTrue("1.5E+7".equals(StringUtils.fixDecimalExponent("1.5E+7")));		assertTrue("1.5E-7".equals(StringUtils.fixDecimalExponent("1.5E-7")));		assertTrue("1.5E+7".equals(StringUtils.fixDecimalExponent("1.5E7")));	}	public void testBug11629() throws Exception {		if (isRunningOnJdk131()) {			return;		}		PrintStream oldOut = System.out;		PrintStream oldError = System.err;		try {			ByteArrayOutputStream bOut = new ByteArrayOutputStream();			PrintStream newOut = new PrintStream(bOut);			System.setOut(newOut);			ByteArrayOutputStream bErr = new ByteArrayOutputStream();			PrintStream newErr = new PrintStream(bErr);			System.setErr(newErr);			Properties props = new Properties();			props.setProperty("characterEncoding", "utf8");			getConnectionWithProps(props).close();			String withExclaims = new String(bOut.toByteArray());			assertTrue(withExclaims.indexOf("!") == -1);			assertTrue(withExclaims.length() == 0); // to catch any other			// System.out.printlns()			withExclaims = new String(bErr.toByteArray());			assertTrue(withExclaims.indexOf("!") == -1);			assertTrue(withExclaims.length() == 0); // to catch any other			// System.err.printlns()		} finally {			System.setOut(oldOut);			System.setErr(oldError);		}	}	/**	 * Tests fix for BUG#11614 - StringUtils.getBytes() doesn't work when using	 * multibyte character encodings and a length in _characters_ is specified.	 * 	 * @throws Exception	 *             if the test fails.	 */	public void testBug11614() throws Exception {		if (isRunningOnJdk131()) {			return; // test not valid on JDK-1.3.1		}		if (versionMeetsMinimum(4, 1)) {			createTable(					"testBug11614",					"(`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,"							+ "`text` TEXT NOT NULL,"							+ "PRIMARY KEY(`id`)) CHARACTER SET utf8 COLLATE utf8_general_ci");			Properties props = new Properties();			props.setProperty("characterEncoding", "utf8");			Connection utf8Conn = null;			try {				utf8Conn = getConnectionWithProps(props);				utf8Conn						.createStatement()						.executeUpdate(								"INSERT INTO testBug11614  (`id`,`text`) values (1,'')");				this.rs = utf8Conn.createStatement().executeQuery(						"SELECT `text` FROM testBug11614 WHERE id=1");				assertTrue(this.rs.next());				Clob c = this.rs.getClob(1);				c.truncate(0);				int blockSize = 8192;				int sizeToTest = blockSize + 100;				StringBuffer blockBuf = new StringBuffer(sizeToTest);				for (int i = 0; i < sizeToTest; i++) {					blockBuf.append('\u00f6');				}				String valueToTest = blockBuf.toString();				c.setString(1, valueToTest);				this.pstmt = utf8Conn						.prepareStatement("UPDATE testBug11614 SET `text` = ? WHERE id=1");				this.pstmt.setClob(1, c);				this.pstmt.executeUpdate();				this.pstmt.close();				String fromDatabase = getSingleIndexedValueWithQuery(utf8Conn,						1, "SELECT `text` FROM testBug11614").toString();				assertEquals(valueToTest, fromDatabase);			} finally {				if (this.rs != null) {					this.rs.close();					this.rs = null;				}				if (this.pstmt != null) {					this.pstmt.close();					this.pstmt = null;				}				if (utf8Conn != null) {					utf8Conn.close();				}			}		}	}	public void testCodePage1252() throws Exception {		if (versionMeetsMinimum(4, 1, 0)) {			/*			 * from			 * ftp://ftp.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT			 * 			 * 0x80 0x20AC #EURO SIGN 0x81 #UNDEFINED 0x82 0x201A #SINGLE LOW-9			 * QUOTATION MARK 0x83 0x0192 #LATIN SMALL LETTER F WITH HOOK 0x84			 * 0x201E #DOUBLE LOW-9 QUOTATION MARK 0x85 0x2026 #HORIZONTAL			 * ELLIPSIS 0x86 0x2020 #DAGGER 0x87 0x2021 #DOUBLE DAGGER 0x88			 * 0x02C6 #MODIFIER LETTER CIRCUMFLEX ACCENT 0x89 0x2030 #PER MILLE			 * SIGN 0x8A 0x0160 #LATIN CAPITAL LETTER S WITH CARON 0x8B 0x2039			 * #SINGLE LEFT-POINTING ANGLE QUOTATION MARK 0x8C 0x0152 #LATIN			 * CAPITAL LIGATURE OE 0x8D #UNDEFINED 0x8E 0x017D #LATIN CAPITAL			 * LETTER Z WITH CARON 0x8F #UNDEFINED 0x90 #UNDEFINED			 */			String codePage1252 = new String(new byte[] { (byte) 0x80,					(byte) 0x82, (byte) 0x83, (byte) 0x84, (byte) 0x85,					(byte) 0x86, (byte) 0x87, (byte) 0x88, (byte) 0x89,					(byte) 0x8a, (byte) 0x8b, (byte) 0x8c, (byte) 0x8e },					"Cp1252");			System.out.println(codePage1252);			Properties props = new Properties();			props.setProperty("characterEncoding", "Cp1252");			Connection cp1252Conn = getConnectionWithProps(props);			createTable("testCp1252",					"(field1 varchar(32) CHARACTER SET latin1)");			cp1252Conn.createStatement().executeUpdate(					"INSERT INTO testCp1252 VALUES ('" + codePage1252 + "')");			this.rs = cp1252Conn.createStatement().executeQuery(					"SELECT field1 FROM testCp1252");			this.rs.next();			assertEquals(this.rs.getString(1), codePage1252);		}	}		/**	 * Tests fix for BUG#23645 - Some collations/character sets reported as "unknown"	 * (specifically cias variants of existing character sets), and inability to override	 * the detected server character set.	 * 	 * @throws Exception if the test fails.	 */	public void testBug23645() throws Exception {		if (versionMeetsMinimum(4, 1)) {			// Part of this isn't easily testable, hence the assertion in CharsetMapping			// that checks for mappings existing in both directions...						// What we test here is the ability to override the character mapping			// when the server returns an "unknown" character encoding.						String currentlyConfiguredCharacterSet = getSingleIndexedValueWithQuery(2, "SHOW VARIABLES LIKE 'character_set_connection'").toString();			System.out.println(currentlyConfiguredCharacterSet);						String javaNameForMysqlName = CharsetMapping.getJavaEncodingForMysqlEncoding(currentlyConfiguredCharacterSet, null);			System.out.println(javaNameForMysqlName);						for (int i = 1; i < CharsetMapping.INDEX_TO_CHARSET.length; i++) {				String possibleCharset = CharsetMapping.INDEX_TO_CHARSET[i];								if (!javaNameForMysqlName.equals(possibleCharset)) {					System.out.println(possibleCharset);										Properties props = new Properties();					props.setProperty("characterEncoding", possibleCharset);					props.setProperty("com.mysql.jdbc.faultInjection.serverCharsetIndex", "65535");										Connection forcedCharConn = null;										forcedCharConn = getConnectionWithProps(props);										String forcedCharset = getSingleIndexedValueWithQuery(forcedCharConn, 2, "SHOW VARIABLES LIKE 'character_set_connection'").toString();										System.out.println(forcedCharset);										break;				}			}					}	}		/**	 * Tests fix for BUG#24840 - character encoding of "US-ASCII"	 * doesn't map correctly for 4.1 or newer	 * 	 * @throws Exception if the test fails.	 */	public void testBug24840() throws Exception {		Properties props = new Properties();		props.setProperty("characterEncoding", "US-ASCII");				getConnectionWithProps(props).close();	}		/**	 * Tests fix for BUG#25047 - StringUtils.indexOfIgnoreCaseRespectQuotes() isn't	 * case-insensitive on the first character of the target.	 * 	 * @throws Exception if the test fails.	 */	public void testBug25047() throws Exception {		assertEquals(26, StringUtils.indexOfIgnoreCaseRespectQuotes(0, "insert into Test (TestID) values (?)",				"VALUES", '`', false));		assertEquals(26, StringUtils.indexOfIgnoreCaseRespectQuotes(0, "insert into Test (TestID) VALUES (?)",				"values", '`', false));				assertEquals(StringUtils.indexOfIgnoreCaseRespectQuotes(0, 				"insert into Test (TestID) values (?)", "VALUES",'`', false),				StringUtils.indexOfIgnoreCaseRespectQuotes(0, 						"insert into Test (TestID) VALUES (?)",  "VALUES",'`', false));		assertEquals(StringUtils.indexOfIgnoreCaseRespectQuotes(0,  				"insert into Test (TestID) values (?)", "values", '`', false),				StringUtils.indexOfIgnoreCaseRespectQuotes(0, 						"insert into Test (TestID) VALUES (?)", "values", '`', false));	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -