stringregressiontest.java

来自「在资料浩瀚的互联网中」· Java 代码 · 共 665 行 · 第 1/2 页

JAVA
665
字号
            this.rs.next();            String retrievedString = this.rs.getString(1);            System.out.println(latin1String);            System.out.println(retrievedString);            if (!retrievedString.equals(latin1String)) {                int stringLength = Math.min(retrievedString.length(),                        latin1String.length());                for (int i = 0; i < stringLength; i++) {                    char rChar = retrievedString.charAt(i);                    char origChar = latin1String.charAt(i);                    if ((rChar != '?') && (rChar != origChar)) {                        fail("characters differ at position " + i + "'" + rChar                            + "' retrieved from database, original char was '"                            + origChar + "'");                    }                }            }        } finally {            if (this.rs != null) {                try {                	this.rs.close();                } catch (Exception ex) {                    // ignore                }            }            if (pStmt != null) {                try {                    pStmt.close();                } catch (Exception ex) {                    // ignore                }            }            this.stmt.executeUpdate("DROP TABLE IF EXISTS latin1RegressTest");        }    }    /**     * Tests newline being treated correctly.     *     * @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")));	}}

⌨️ 快捷键说明

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