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

📄 stringregressiontest.java

📁 mysql的jdbc驱动
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 * @throws Exception	 *             if an error occurs	 */	public void testNewlines() throws Exception {		String newlineStr = "Foo\nBar\n\rBaz";		this.stmt.executeUpdate("DROP TABLE IF EXISTS newlineRegressTest");		this.stmt				.executeUpdate("CREATE TABLE newlineRegressTest (field1 MEDIUMTEXT)");		try {			this.stmt.executeUpdate("INSERT INTO newlineRegressTest VALUES ('"					+ newlineStr + "')");			this.pstmt = this.conn					.prepareStatement("INSERT INTO newlineRegressTest VALUES (?)");			this.pstmt.setString(1, newlineStr);			this.pstmt.executeUpdate();			this.rs = this.stmt					.executeQuery("SELECT * FROM newlineRegressTest");			while (this.rs.next()) {				assertTrue(this.rs.getString(1).equals(newlineStr));			}		} finally {			this.stmt.executeUpdate("DROP TABLE IF EXISTS newlineRegressTest");		}	}	/**	 * Tests that single-byte character conversion works correctly.	 * 	 * @throws Exception	 *             if any errors occur	 */	// TODO: Use Unicode Literal escapes for this, for now, this test is	// broken :(	/*	 * public void testSingleByteConversion() throws Exception {	 * testConversionForString("latin1", "��� ����");	 * testConversionForString("latin1", "Kaarle ��nis Ilmari");	 * testConversionForString("latin1", "������������������"); }	 */	/**	 * Tests that the 0x5c escaping works (we didn't use to have this).	 * 	 * @throws Exception	 *             if an error occurs.	 */	public void testSjis5c() throws Exception {		byte[] origByteStream = new byte[] { (byte) 0x95, (byte) 0x5c,				(byte) 0x8e, (byte) 0x96 };		//		// Print the hex values of the string		//		StringBuffer bytesOut = new StringBuffer();		for (int i = 0; i < origByteStream.length; i++) {			bytesOut.append(Integer.toHexString(origByteStream[i] & 255));			bytesOut.append(" ");		}		System.out.println(bytesOut.toString());		String origString = new String(origByteStream, "SJIS");		byte[] newByteStream = StringUtils.getBytes(origString, "SJIS",				"ISO8859_1              ", false);		//		// 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")));	}		/**	 * 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 (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 testBug11629() throws Exception {		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);		}	}}

⌨️ 快捷键说明

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