db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmars...@apache.org
Subject svn commit: r409746 - in /db/derby/code/branches/10.1/java: client/org/apache/derby/client/am/ClobWriter.java testing/org/apache/derbyTesting/functionTests/master/lobStreams.out testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/lobStreams.java
Date Fri, 26 May 2006 20:54:00 GMT
Author: kmarsden
Date: Fri May 26 13:53:59 2006
New Revision: 409746

URL: http://svn.apache.org/viewvc?rev=409746&view=rev
Log:
DERBY-1354 - Writer.write(int c) to writer from Clob.setCharacterStream(long pos) appends
integer value, not character

Merge to 10.1 from trunk. Also included DERBY-1245

Contributed by Anders Morken



Modified:
    db/derby/code/branches/10.1/java/client/org/apache/derby/client/am/ClobWriter.java
    db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/master/lobStreams.out
    db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/lobStreams.java

Modified: db/derby/code/branches/10.1/java/client/org/apache/derby/client/am/ClobWriter.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.1/java/client/org/apache/derby/client/am/ClobWriter.java?rev=409746&r1=409745&r2=409746&view=diff
==============================================================================
--- db/derby/code/branches/10.1/java/client/org/apache/derby/client/am/ClobWriter.java (original)
+++ db/derby/code/branches/10.1/java/client/org/apache/derby/client/am/ClobWriter.java Fri
May 26 13:53:59 2006
@@ -39,7 +39,7 @@
 
     public void write(int c) {
         StringBuffer sb = new StringBuffer(clob_.string_.substring(0, (int) offset_ - 1));
-        sb.append(c);
+        sb.append((char)c);
         clob_.string_ = sb.toString();
         clob_.asciiStream_ = new java.io.StringBufferInputStream(clob_.string_);
         clob_.unicodeStream_ = new java.io.StringBufferInputStream(clob_.string_);

Modified: db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/master/lobStreams.out
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/master/lobStreams.out?rev=409746&r1=409745&r2=409746&view=diff
==============================================================================
--- db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/master/lobStreams.out
(original)
+++ db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/master/lobStreams.out
Fri May 26 13:53:59 2006
@@ -3,9 +3,16 @@
 testBlobWrite3Param finished
 START: testBlobWrite1Param
 testBlobWrite1Param finished
-START: testClobWrite3Param
-testClobWrite3Param finished
-START: testClobWrite1Param
-testClobWrite1Param finished
-FINISHED TEST blobSetBinaryStream :-)
+START: testClobAsciiWrite3Param
+testClobAsciiWrite3Param finished
+START: testClobAsciiWrite1Param
+testClobAsciiWrite1Param finished
+START: testClobCharacterWrite3ParamChar
+testClobCharacterWrite3ParamChar finished
+START: testClobCharacterWrite3ParamString
+testClobCharacterWrite3ParamString finished
+START: testClobCharacterWrite1ParamString
+testClobCharacterWrite1ParamString finished
+START: testClobCharacterWrite1Char
+testClobCharacterWrite1Char finished
 Test lob stream with multiple writes finished

Modified: db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/lobStreams.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/lobStreams.java?rev=409746&r1=409745&r2=409746&view=diff
==============================================================================
--- db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/lobStreams.java
(original)
+++ db/derby/code/branches/10.1/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/lobStreams.java
Fri May 26 13:53:59 2006
@@ -24,8 +24,11 @@
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
+import java.io.FileReader;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
 import java.sql.Blob;
 import java.sql.Clob;
 import java.sql.Connection;
@@ -40,21 +43,46 @@
 public class lobStreams {
     
         static String[] fileName = new String[2];
+        static String sep;
         static long fileLength;
 
         static boolean debug = true;
         private static final String START = "\nSTART: ";
+		
+		private static final String unicodeTestString = "This is a test string containing a few
" +
+				"non-ascii characters:\nÆØÅ and æøå are used in norwegian: 'Blåbærsyltetøy'
means" +
+				"'blueberry jam', and tastes great on pancakes. =)";
 
         static
         {
-            fileName[0] = "extin/aclob.utf";
-            fileName[1] = "extin/littleclob.utf";
+//            fileName[0] = "extin" + sep + "aclob.utf";
+ //           fileName[1] = "extin" + sep + "littleclob.utf";
+            fileName[0] =  "aclob.utf";
+            fileName[1] =  "littleclob.utf";
         }
         
         public static void main(String[] args)
         {
             System.out.println("Test lob stream with multiple writes starting");
 
+            // check to see if we have the correct extin path, if the files aren't right
here, try one more time
+	    boolean exists = (new File("extin", fileName[0])).exists();
+            String sep =  System.getProperty("file.separator");
+	    if (!exists) 
+            {
+                // assume it's in a dir up, if that's wrong too, too bad...
+                String userdir =  System.getProperty("user.dir");
+                fileName[0] = userdir + sep + ".." + sep + "extin" + sep + fileName[0];
+                fileName[1] = userdir + sep + ".." + sep + "extin" + sep + fileName[1];
+            }
+            else
+            {
+                // assume it's in a dir up, if that's wrong too, too bad...
+                fileName[0] = "extin" + sep + fileName[0];
+                fileName[1] = "extin" + sep + fileName[1];
+            }
+
+
             try
             {
                 // use the ij utility to read the property file and
@@ -70,16 +98,25 @@
                 resetBlobClob(conn);
                 testBlobWrite1Param(conn);
                 resetBlobClob(conn);
-                testClobWrite3Param(conn);
+                testClobAsciiWrite3Param(conn);
+                resetBlobClob(conn);
+                testClobAsciiWrite1Param(conn);
                 resetBlobClob(conn);
-                testClobWrite1Param(conn);
+                testClobCharacterWrite3ParamChar(conn);
+                resetBlobClob(conn);
+                testClobCharacterWrite3ParamString(conn);
+                resetBlobClob(conn);
+                testClobCharacterWrite1ParamString(conn);
+                resetBlobClob(conn);
+                testClobCharacterWrite1Char(conn);
 
                 // restart the connection
                 conn.commit();
+                cleanUp(conn);
+                conn.commit();
                 conn.close();
-                System.out.println("FINISHED TEST blobSetBinaryStream :-)");
 
-            }
+			}
             catch (Throwable e)
             {
                 System.out.println("FAIL -- unexpected exception:" + e.toString());
@@ -133,6 +170,9 @@
             
         }
 
+		/**
+		 * Tests the BlobOutputStream.write(byte  b[], int off, int len) method
+		 **/
         private static void testBlobWrite3Param(Connection conn)
         {
             try {
@@ -206,6 +246,9 @@
             }
         }
 
+		/**
+		 * Tests the BlobOutputStream.write(int b) method
+		 **/
         private static void testBlobWrite1Param(Connection conn)
         {
             try {
@@ -278,10 +321,13 @@
             }
         }
 
-        private static void testClobWrite3Param(Connection conn)
+		/**
+		 * Tests the ClobOutputStream.write(byte  b[], int off, int len) method
+		 **/
+        private static void testClobAsciiWrite3Param(Connection conn)
         {
             try {
-                System.out.println(START + "testClobWrite3Param");
+                System.out.println(START + "testClobAsciiWrite3Param");
                
                 PreparedStatement stmt3 = conn.prepareStatement(
                     "SELECT c FROM testBlobX1 WHERE a = 1");
@@ -342,7 +388,7 @@
                 rs3.close();
                 stmt3.close();
                
-                System.out.println("testClobWrite3Param finished");
+                System.out.println("testClobAsciiWrite3Param finished");
             } catch (SQLException e) {
                 TestUtil.dumpSQLExceptions(e);
             } catch (Throwable e) {
@@ -350,10 +396,13 @@
             }
         }
 
-        private static void testClobWrite1Param(Connection conn)
+		/**
+		 * Tests the ClobOutputStream.write(int b) method
+		 **/
+        private static void testClobAsciiWrite1Param(Connection conn)
         {
             try {
-                System.out.println(START + "testClobWrite1Param");
+                System.out.println(START + "testClobAsciiWrite1Param");
                
                 PreparedStatement stmt3 = conn.prepareStatement(
                     "SELECT c FROM testBlobX1 WHERE a = 1");
@@ -414,14 +463,278 @@
                 rs3.close();
                 stmt3.close();
                
-                System.out.println("testClobWrite1Param finished");
+                System.out.println("testClobAsciiWrite1Param finished");
             } catch (SQLException e) {
                 TestUtil.dumpSQLExceptions(e);
             } catch (Throwable e) {
                 if (debug) e.printStackTrace();
             }
         }
-        
+
+		/**
+		 * Tests the ClobWriter.write(char cbuf[], int off, int len) method
+		 **/
+        private static void testClobCharacterWrite3ParamChar(Connection conn)
+        {
+            try {
+                System.out.println(START + "testClobCharacterWrite3ParamChar");
+               
+                PreparedStatement stmt3 = conn.prepareStatement(
+                    "SELECT c FROM testBlobX1 WHERE a = 1");
+                
+                ResultSet rs3 = stmt3.executeQuery();
+                
+                rs3.next();
+
+                Clob clob = rs3.getClob(1);
+                char[] testdata = unicodeTestString.toCharArray();
+				
+
+                if (clob != null) {
+                    Writer clobWriter = clob.setCharacterStream(1L);
+                    clobWriter.write(testdata, 0, testdata.length);
+                    clobWriter.close();
+                    
+                    PreparedStatement stmt4 = conn.prepareStatement(
+                        "UPDATE testBlobX1 SET c = ? WHERE a = 1");
+                    stmt4.setClob(1,  clob);
+                    stmt4.executeUpdate();
+                    stmt4.close();
+                } else {
+                    System.out.println("FAIL -- clob is NULL");
+                }
+
+                rs3.close();
+                rs3 = stmt3.executeQuery();
+                
+                if (rs3.next()) {
+                    long new_length = rs3.getClob(1).length();
+                    if (new_length != testdata.length) {
+                        System.out.println(
+                                "FAIL -- wrong clob length; original: " + 
+                                testdata.length + " clob length: " + new_length);
+                    } else {
+                        // Check contents ...
+                        Reader lStream = rs3.getClob(1).getCharacterStream();
+
+                        if (!compareClobReader2CharArray(testdata, lStream))
+                            System.out.println("FAIL - Clob and buffer contents do not match");
+
+                        lStream.close();
+                        
+                    }
+                } else {
+                    System.out.println("FAIL -- clob not found");
+                }
+                rs3.close();
+                stmt3.close();
+               
+                System.out.println("testClobCharacterWrite3ParamChar finished");
+            } catch (SQLException e) {
+                TestUtil.dumpSQLExceptions(e);
+            } catch (Throwable e) {
+                if (debug) e.printStackTrace();
+            }
+        }
+
+		/**
+		 * Tests the ClobWriter.write(String str, int off, int len) method
+		 **/
+		private static void testClobCharacterWrite3ParamString(Connection conn)
+        {
+            try {
+                System.out.println(START + "testClobCharacterWrite3ParamString");
+               
+                PreparedStatement stmt3 = conn.prepareStatement(
+                    "SELECT c FROM testBlobX1 WHERE a = 1");
+                
+                ResultSet rs3 = stmt3.executeQuery();
+                
+                rs3.next();
+
+                Clob clob = rs3.getClob(1);
+				
+
+                if (clob != null) {
+                    Writer clobWriter = clob.setCharacterStream(1L);
+                    clobWriter.write(unicodeTestString, 0, unicodeTestString.length());
+                    clobWriter.close();
+                    
+                    PreparedStatement stmt4 = conn.prepareStatement(
+                        "UPDATE testBlobX1 SET c = ? WHERE a = 1");
+                    stmt4.setClob(1,  clob);
+                    stmt4.executeUpdate();
+                    stmt4.close();
+                } else {
+                    System.out.println("FAIL -- clob is NULL");
+                }
+
+                rs3.close();
+                rs3 = stmt3.executeQuery();
+                
+                if (rs3.next()) {
+                    long new_length = rs3.getClob(1).length();
+                    if (new_length != unicodeTestString.length()) {
+                        System.out.println(
+                                "FAIL -- wrong clob length; original: " + 
+                                unicodeTestString.length() + " clob length: " + new_length);
+                    } else {
+                        // Check contents ...
+                        Reader lStream = rs3.getClob(1).getCharacterStream();
+
+                        if (!compareClobReader2CharArray(unicodeTestString.toCharArray(),
lStream))
+                            System.out.println("FAIL - Clob and buffer contents do not match");
+
+                        lStream.close();
+                        
+                    }
+                } else {
+                    System.out.println("FAIL -- clob not found");
+                }
+                rs3.close();
+                stmt3.close();
+               
+                System.out.println("testClobCharacterWrite3ParamString finished");
+            } catch (SQLException e) {
+                TestUtil.dumpSQLExceptions(e);
+            } catch (Throwable e) {
+                if (debug) e.printStackTrace();
+            }
+        }
+
+		/**
+		 * Tests the ClobWriter.write(String str) method
+		 **/
+		private static void testClobCharacterWrite1ParamString(Connection conn)
+        {
+            try {
+                System.out.println(START + "testClobCharacterWrite1ParamString");
+               
+                PreparedStatement stmt3 = conn.prepareStatement(
+                    "SELECT c FROM testBlobX1 WHERE a = 1");
+                
+                ResultSet rs3 = stmt3.executeQuery();
+                
+                rs3.next();
+
+                Clob clob = rs3.getClob(1);
+				
+
+                if (clob != null) {
+                    Writer clobWriter = clob.setCharacterStream(1L);
+                    clobWriter.write(unicodeTestString);
+                    clobWriter.close();
+                    
+                    PreparedStatement stmt4 = conn.prepareStatement(
+                        "UPDATE testBlobX1 SET c = ? WHERE a = 1");
+                    stmt4.setClob(1,  clob);
+                    stmt4.executeUpdate();
+                    stmt4.close();
+                } else {
+                    System.out.println("FAIL -- clob is NULL");
+                }
+
+                rs3.close();
+                rs3 = stmt3.executeQuery();
+                
+                if (rs3.next()) {
+                    long new_length = rs3.getClob(1).length();
+                    if (new_length != unicodeTestString.length()) {
+                        System.out.println(
+                                "FAIL -- wrong clob length; original: " + 
+                                unicodeTestString.length() + " clob length: " + new_length);
+                    } else {
+                        // Check contents ...
+                        Reader lStream = rs3.getClob(1).getCharacterStream();
+
+                        if (!compareClobReader2CharArray(unicodeTestString.toCharArray(),
lStream))
+                            System.out.println("FAIL - Clob and buffer contents do not match");
+
+                        lStream.close();
+                        
+                    }
+                } else {
+                    System.out.println("FAIL -- clob not found");
+                }
+                rs3.close();
+                stmt3.close();
+               
+                System.out.println("testClobCharacterWrite1ParamString finished");
+            } catch (SQLException e) {
+                TestUtil.dumpSQLExceptions(e);
+            } catch (Throwable e) {
+                if (debug) e.printStackTrace();
+            }
+        }
+		/**
+		 * Tests the ClobWriter.write(int c) method
+		 **/
+		private static void testClobCharacterWrite1Char(Connection conn)
+        {
+            try {
+                System.out.println(START + "testClobCharacterWrite1Char");
+               
+                PreparedStatement stmt3 = conn.prepareStatement(
+                    "SELECT c FROM testBlobX1 WHERE a = 1");
+                
+                ResultSet rs3 = stmt3.executeQuery();
+                
+                rs3.next();
+
+                Clob clob = rs3.getClob(1);
+				
+				char testchar = 'a';
+
+                if (clob != null) {
+                    Writer clobWriter = clob.setCharacterStream(1L);
+                    clobWriter.write(testchar);
+                    clobWriter.close();
+                    
+                    PreparedStatement stmt4 = conn.prepareStatement(
+                        "UPDATE testBlobX1 SET c = ? WHERE a = 1");
+                    stmt4.setClob(1,  clob);
+                    stmt4.executeUpdate();
+                    stmt4.close();
+                } else {
+                    System.out.println("FAIL -- clob is NULL");
+                }
+
+                rs3.close();
+                rs3 = stmt3.executeQuery();
+                
+                if (rs3.next()) {
+                    long new_length = rs3.getClob(1).length();
+					Clob fish = rs3.getClob(1);
+                    if (new_length != 1) {
+                        System.out.println(
+                                "FAIL -- wrong clob length; original: " + 
+                                1 + " clob length: " + new_length);
+                    } else {
+                        // Check contents ...
+                        Reader lStream = rs3.getClob(1).getCharacterStream();
+						char clobchar = (char) lStream.read();
+						
+                        if (clobchar != testchar)
+                            System.out.println("FAIL - fetched Clob and original contents
do not match");
+
+                        lStream.close();
+                        
+                    }
+                } else {
+                    System.out.println("FAIL -- clob not found");
+                }
+                rs3.close();
+                stmt3.close();
+               
+                System.out.println("testClobCharacterWrite1Char finished");
+            } catch (SQLException e) {
+                TestUtil.dumpSQLExceptions(e);
+            } catch (Throwable e) {
+                if (debug) e.printStackTrace();
+            }
+        }
+
+		
         private static boolean compareLob2File(InputStream fStream, InputStream lStream)
{
             byte[] fByte = new byte[1024];
             byte[] lByte = new byte[1024];
@@ -443,4 +756,34 @@
             } 
             return true;
         }
+
+		private static boolean compareClobReader2CharArray(char[] cArray, Reader charReader) {
+			char[] clobChars = new char[cArray.length];
+
+			int readChars = 0;
+			int totalCharsRead = 0;
+
+			try {
+				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;
+
+			} catch (Throwable e) {
+				if (debug) e.printStackTrace();
+			}
+			return true;
+		}
+
+		
+        private static void cleanUp(Connection conn) throws SQLException {
+            String[] testObjects = {"table testBlobX1"};
+            Statement cleanupStmt = conn.createStatement();
+            TestUtil.cleanUpTest(cleanupStmt, testObjects);
+        }
+
 }



Mime
View raw message