db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmars...@apache.org
Subject svn commit: r409688 - in /db/derby/code/trunk/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 16:08:49 GMT
Author: kmarsden
Date: Fri May 26 09:08:48 2006
New Revision: 409688

URL: http://svn.apache.org/viewvc?rev=409688&view=rev
Log:
DERBY-1245 Add o.a.derby.client.am.ClobWriter test coverage
DERBY-1354 Writer.write(int c) to writer from Clob.setCharacterStream(long pos) appends integer
value, not character

Contributed by Anders Morken


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

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobWriter.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobWriter.java?rev=409688&r1=409687&r2=409688&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobWriter.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/ClobWriter.java Fri May 26
09:08:48 2006
@@ -43,7 +43,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/trunk/java/testing/org/apache/derbyTesting/functionTests/master/lobStreams.out
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/lobStreams.out?rev=409688&r1=409687&r2=409688&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/lobStreams.out
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/lobStreams.out
Fri May 26 09:08:48 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/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/lobStreams.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/lobStreams.java?rev=409688&r1=409687&r2=409688&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/lobStreams.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/lobStreams.java
Fri May 26 09:08:48 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;
@@ -45,6 +48,10 @@
 
         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
         {
@@ -91,18 +98,25 @@
                 resetBlobClob(conn);
                 testBlobWrite1Param(conn);
                 resetBlobClob(conn);
-                testClobWrite3Param(conn);
+                testClobAsciiWrite3Param(conn);
                 resetBlobClob(conn);
-                testClobWrite1Param(conn);
+                testClobAsciiWrite1Param(conn);
+                resetBlobClob(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());
@@ -156,6 +170,9 @@
             
         }
 
+		/**
+		 * Tests the BlobOutputStream.write(byte  b[], int off, int len) method
+		 **/
         private static void testBlobWrite3Param(Connection conn)
         {
             try {
@@ -229,6 +246,9 @@
             }
         }
 
+		/**
+		 * Tests the BlobOutputStream.write(int b) method
+		 **/
         private static void testBlobWrite1Param(Connection conn)
         {
             try {
@@ -301,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");
@@ -365,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) {
@@ -373,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");
@@ -437,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];
@@ -467,6 +757,29 @@
             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();



Mime
View raw message