db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1488411 - in /db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests: tests/jdbcapi/parameterMapping.java util/streams/CharAlphabet.java
Date Fri, 31 May 2013 22:12:51 GMT
Author: mamta
Date: Fri May 31 22:12:51 2013
New Revision: 1488411

URL: http://svn.apache.org/r1488411
Log:
DERBY-6237(PreparedStatement.execute() fails starting 10.2 when multiple rows are updated
and PreparedStatement.setCharacterStream(int, Reader, int) is used)

Adding test for PreparedStatement.setCharacterStream on CLOB column and using that prepared
statement to update more than one row of the table

In 10.1, setCharacterStream to update CLOB and varchar columns work even when update is going
to update more than one row but in 10.2, PreparedStatement.setCharacterStream on CLOB column
fails when running in embedded mode. Client server case works fine on 10.2. I have commented
out the test for embedded in 10.2 Once this jira is fixed in 10.2, we should uncomment these
tests.


Modified:
    db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/parameterMapping.java
    db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/util/streams/CharAlphabet.java

Modified: db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/parameterMapping.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/parameterMapping.java?rev=1488411&r1=1488410&r2=1488411&view=diff
==============================================================================
--- db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/parameterMapping.java
(original)
+++ db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/parameterMapping.java
Fri May 31 22:12:51 2013
@@ -24,11 +24,14 @@ package org.apache.derbyTesting.function
 import org.apache.derby.tools.ij;
 import org.apache.derbyTesting.functionTests.util.TestUtil;
 import org.apache.derbyTesting.functionTests.util.BigDecimalHandler;
+import org.apache.derbyTesting.functionTests.util.streams.CharAlphabet;
+import org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader;
 
 
 import java.sql.*;
 import java.math.*;
 import java.io.*;
+import java.util.Arrays;
 
 public class parameterMapping {
 	
@@ -303,6 +306,7 @@ public class parameterMapping {
 			// make the initial connection.
 			 ij.getPropertyArg(args);
 			 Connection conn = ij.startJBMS();
+			 testDERBY6237(conn);
 
 			 conn.setAutoCommit(false);
 
@@ -3040,4 +3044,138 @@ public class parameterMapping {
 
 		return "0x" + Integer.toHexString(((int) b1) & 0xff) + "," + "0x" + Integer.toHexString(((int)
b2) & 0xff);
 	}
+	
+	//numberOfRowsToUpdate - value 1 or 2
+	//testVariation - if 1 then update CLOB with short data
+	//                if 2 then update CLOB with large data
+	private static void helperTestDerby6237(int numberOfRowsToUpdate, 
+            int testVariation,
+            Connection conn) throws Exception
+	{
+        CharAlphabet a1 = CharAlphabet.singleChar('a');
+
+        //Following will update one or 2 rows depending on the 1st param
+        PreparedStatement ps = conn.prepareStatement(
+            "UPDATE TestUpdateCharStream SET c3 = ?, " + 
+            "c2 = c2 + 1 WHERE c1 IN (?, ?)");
+
+        switch (testVariation) {
+        case 1 :
+        	//test short data
+            ps.setCharacterStream(1,
+                    new LoopingAlphabetReader(50, a1), 50);
+            break;
+        case 2 :
+        	//test large data
+            ps.setCharacterStream(1,
+                    new LoopingAlphabetReader(50000, a1), 50000);
+            break;
+        }
+        
+        //First value in IN clause is getting set to 'AAAAA'
+        // Using setCharacterStream on VARCHAR to set the value
+        ps.setCharacterStream(2, new CharArrayReader("AAAAA".toCharArray()), 5);
+        
+        if (numberOfRowsToUpdate == 1 ) {
+            //Second value in IN clause is also getting set to 'AAAAA', which 
+            // means prepared statement will update only one row
+            ps.setObject(3, "AAAAA", Types.VARCHAR);
+        } else {
+            //Second value in IN clause is also getting set to 'EEEEE', which 
+            // means prepared statement will update two rows
+            ps.setObject(3, "EEEEE", Types.VARCHAR);
+        }        	
+        ps.execute();
+        
+        //verify updated data
+        ResultSet rs;
+        ps = conn.prepareStatement(
+                "select c3 from TestUpdateCharStream " + 
+                "WHERE c1 IN (?, ?)");
+        ps.setCharacterStream(1, new CharArrayReader("AAAAA".toCharArray()), 5);
+        if (numberOfRowsToUpdate == 1 ) {
+            ps.setObject(2, "AAAAA", Types.VARCHAR);
+        } else {
+            ps.setObject(2, "EEEEE", Types.VARCHAR);
+        }
+    	rs = ps.executeQuery();
+    	char[] c;
+    	if (testVariation == 1){
+        	//we are here to test short data 
+            c = new char[50];
+            Arrays.fill(c, 'a'); 
+    	} else {
+        	//we are here to test large data 
+            c = new char[50000];
+            Arrays.fill(c, 'a');         		
+    	}
+    	for (int i=0;i<numberOfRowsToUpdate;i++) {
+        	rs.next();
+        	if (!compareClobReader2CharArray(c,rs.getCharacterStream(1))) {
+    			System.out.println("FAIL: data should have matched");
+    			rs.close();
+    			ps.close();
+    			return;
+        	}
+    	}
+    	rs.close();
+        ps.close();
+
+    }
+
+	private static boolean compareClobReader2CharArray
+		(char[] cArray, Reader charReader) throws Exception {
+		char[] clobChars = new char[cArray.length];
+
+		int readChars = 0;
+		int totalCharsRead = 0;
+
+		do {
+			readChars = charReader.read(clobChars, totalCharsRead, cArray.length - totalCharsRead);
+			if (readChars != -1) 
+				totalCharsRead += readChars;
+		} while (readChars != -1 && totalCharsRead < cArray.length);
+		charReader.close();
+		if (!java.util.Arrays.equals(cArray, clobChars))
+			return false;
+
+		return true;
+	}
+
+    /**
+     * DERBY-6237(PreparedStatement.execute() fails starting 10.2 when 
+     *  multiple rows are updated and 
+     *  PreparedStatement.setCharacterStream(int, Reader, int) is used) 
+     * In 10.1, setCharacterStream to update CLOB and varchar columns
+     *  work even when update is going to update more than one row
+     * @throws Exception
+     */
+	private static void testDERBY6237(Connection conn) throws Exception
+	{
+        Statement s = conn.createStatement();
+        s.executeUpdate("CREATE TABLE TestUpdateCharStream ("+
+                "c1 VARCHAR(64) NOT NULL, " +
+          	    "c2 INTEGER, " +
+                "c3 CLOB)"); 
+        s.executeUpdate("INSERT INTO TestUpdateCharStream (c1, c2) " +
+                "VALUES ('AAAAA', 1)");
+        s.executeUpdate("INSERT INTO TestUpdateCharStream (c1, c2) " +
+                "VALUES ('EEEEE', 1)");
+        
+        //update only one row and use short data
+        helperTestDerby6237(1,1,conn);
+        //update only one row and use large data
+        helperTestDerby6237(1,2,conn);
+        //update two rows and use short data
+        //Once DERBY-6237 is fixed, we should remove following if condition
+        if (!TestUtil.isEmbeddedFramework())
+            helperTestDerby6237(2,1,conn);
+        //update two rows and use large data
+        //Once DERBY-6237 is fixed, we should remove following if condition
+        if (!TestUtil.isEmbeddedFramework())
+        	helperTestDerby6237(2,2,conn);
+        s.execute("DROP TABLE TestUpdateCharStream");
+        s.close();
+    }
+
 }

Modified: db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/util/streams/CharAlphabet.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/util/streams/CharAlphabet.java?rev=1488411&r1=1488410&r2=1488411&view=diff
==============================================================================
--- db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/util/streams/CharAlphabet.java
(original)
+++ db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/util/streams/CharAlphabet.java
Fri May 31 22:12:51 2013
@@ -85,6 +85,13 @@ public class CharAlphabet {
                                 CharAlphabet.CJKSUBSET);
     }
 
+    /**
+     * Get an alphabet consisting of a single character.
+     */
+    public static CharAlphabet singleChar(char ch) {
+        return new CharAlphabet("Single char: " + ch, new char[] { ch });
+    }
+
     /** Name of the alphabet. */
     private final String name;
     /** Characters in the alphabet. */



Mime
View raw message