List:Commits« Previous MessageNext Message »
From:tikeda Date:January 27 2006 1:13am
Subject:Connector/J commit: r4863 - branches/branch_5_1/connector-j/src/testsuite/simple
View as plain text  
Modified:
   branches/branch_5_1/connector-j/src/testsuite/simple/StatementsTest.java
Log:
implemented PreparedStatement.setNCharacterStream(), ServerPreparedStatement.setNCharacterStream() and those test methods

Modified: branches/branch_5_1/connector-j/src/testsuite/simple/StatementsTest.java
===================================================================
--- branches/branch_5_1/connector-j/src/testsuite/simple/StatementsTest.java	2006-01-27 01:13:13 UTC (rev 4862)
+++ branches/branch_5_1/connector-j/src/testsuite/simple/StatementsTest.java	2006-01-27 01:13:35 UTC (rev 4863)
@@ -27,6 +27,7 @@
 import java.io.ByteArrayInputStream;
 import java.io.CharArrayReader;
 import java.io.Reader;
+import java.io.StringReader;
 import java.rmi.server.UID;
 import java.sql.CallableStatement;
 import java.sql.Connection;
@@ -1512,4 +1513,100 @@
         pstmt2.close();
         conn2.close();
     }
+    
+    /**
+     * Tests for PreparedStatement.setNCharcterStream()
+     * 
+     * @throws Exception
+     */
+    public void testSetNCharacterStream() throws Exception {
+        // suppose sql_mode don't include "NO_BACKSLASH_ESCAPES"
+        
+        createTable("testSetNCharacterStream", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10))");
+        Properties props1 = new Properties();
+        props1.put("useServerPrepStmts", "false"); // use client-side prepared statement
+        props1.put("UseStreamLengthsInPrepStmts", "true"); // use 3rd parameter of setNCharacterStream
+        props1.put("useUnicode", "true");
+        props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
+        Connection conn1 = getConnectionWithProps(props1);
+        com.mysql.jdbc.PreparedStatement pstmt1 = (com.mysql.jdbc.PreparedStatement)
+            conn1.prepareStatement("INSERT INTO testSetNCharacterStream (c1, c2) VALUES (?, ?)");
+        pstmt1.setNCharacterStream(1, null, 0);
+        Reader reader1 = new StringReader("\'aaa\'");
+        pstmt1.setNCharacterStream(2, reader1, 5);
+        pstmt1.execute();
+        ResultSet rs1 = this.stmt.executeQuery("SELECT c1, c2 FROM testSetNCharacterStream");
+        rs1.next();
+        assertEquals(null, rs1.getString(1));
+        assertEquals("\'aaa\'", rs1.getString(2));
+        rs1.close();
+        pstmt1.close();
+        conn1.close();
+        
+        createTable("testSetNCharacterStream", "(c1 NATIONAL CHARACTER(10), c2 NATIONAL CHARACTER(10))");
+        Properties props2 = new Properties();
+        props2.put("useServerPrepStmts", "false"); // use client-side prepared statement
+        props2.put("UseStreamLengthsInPrepStmts", "false"); // don't use 3rd parameter of setNCharacterStream
+        props2.put("useUnicode", "true");
+        props2.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
+        Connection conn2 = getConnectionWithProps(props2);
+        com.mysql.jdbc.PreparedStatement pstmt2 = (com.mysql.jdbc.PreparedStatement)
+        conn2.prepareStatement("INSERT INTO testSetNCharacterStream (c1, c2) VALUES (?, ?)");
+        pstmt2.setNCharacterStream(1, null, 0);
+        Reader reader2 = new StringReader("\'aaa\'");
+        pstmt2.setNCharacterStream(2, reader2, 5);
+        pstmt2.execute();
+        ResultSet rs2 = this.stmt.executeQuery("SELECT c1, c2 FROM testSetNCharacterStream");
+        rs2.next();
+        assertEquals(null, rs2.getString(1));
+        assertEquals("\'aaa\'", rs2.getString(2));
+        rs2.close();
+        pstmt2.close();
+        conn2.close();
+    }
+    
+    /**
+     * Tests for ServerPreparedStatement.setNCharcterStream()
+     * 
+     * @throws Exception
+     */
+    public void testSetNCharacterStreamServer() throws Exception {
+        createTable("testSetNCharacterStreamServer", "(c1 NATIONAL CHARACTER(10))");
+        Properties props1 = new Properties();
+        props1.put("useServerPrepStmts", "true"); // use server-side prepared statement
+        props1.put("useUnicode", "true");
+        props1.put("characterEncoding", "latin1"); // ensure charset isn't utf8 here
+        Connection conn1 = getConnectionWithProps(props1);
+        com.mysql.jdbc.ServerPreparedStatement pstmt1 = (com.mysql.jdbc.ServerPreparedStatement)
+            conn1.prepareStatement("INSERT INTO testSetNCharacterStreamServer (c1) VALUES (?)");
+        try {
+            Reader reader1 = new StringReader("aaa");
+            pstmt1.setNCharacterStream(1, reader1, 3);
+            fail();
+        } catch (SQLException e) {
+            // ok
+            assertEquals("Can not call setNCharacterStream() when connection character set isn't UTF-8",
+                e.getMessage());  
+        }
+        pstmt1.close();
+        conn1.close();
+        
+        createTable("testSetNCharacterStreamServer", "(c1 NATIONAL CHARACTER(10))");
+        Properties props2 = new Properties();
+        props2.put("useServerPrepStmts", "true"); // use server-side prepared statement
+        props2.put("useUnicode", "true");
+        props2.put("characterEncoding", "UTF-8"); // ensure charset is utf8 here
+        Connection conn2 = getConnectionWithProps(props2);
+        com.mysql.jdbc.ServerPreparedStatement pstmt2 = (com.mysql.jdbc.ServerPreparedStatement)
+            conn2.prepareStatement("INSERT INTO testSetNCharacterStreamServer (c1) VALUES (?)");
+        Reader reader2 = new StringReader("\'aaa\'");
+        pstmt2.setNCharacterStream(1, reader2, 5);
+        pstmt2.execute();
+        ResultSet rs2 = this.stmt.executeQuery("SELECT c1 FROM testSetNCharacterStreamServer");
+        rs2.next();
+        assertEquals("\'aaa\'", rs2.getString(1));
+        rs2.close();
+        pstmt2.close();
+        conn2.close();
+    }
 }

Thread
Connector/J commit: r4863 - branches/branch_5_1/connector-j/src/testsuite/simpletikeda27 Jan