You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by ka...@apache.org on 2006/07/07 11:14:55 UTC

svn commit: r419848 - /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java

Author: kahatlen
Date: Fri Jul  7 02:14:54 2006
New Revision: 419848

URL: http://svn.apache.org/viewvc?rev=419848&view=rev
Log:
DERBY-1474: Add assertEquals overloads for Blob and Clob

Follow-up patch contributed by Kristian Waagan.

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java?rev=419848&r1=419847&r2=419848&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java Fri Jul  7 02:14:54 2006
@@ -126,43 +126,40 @@
      * @param b1 first <code>Blob</code>.
      * @param b2 second <code>Blob</code>.
      * @throws AssertionFailedError if blobs are not equal.
+     * @throws IOException if reading or closing a stream fails
+     * @throws SQLException if obtaining a stream fails
      */
-    public static void assertEquals(Blob b1, Blob b2) {
+    public static void assertEquals(Blob b1, Blob b2)
+            throws IOException, SQLException {
         if (b1 == null || b2 == null) {
             assertNull("Blob b2 is null, b1 is not", b1);
             assertNull("Blob b1 is null, b2 is not", b2);
             return;
         }
-        InputStream is1 = null, is2 = null;
-        try {
-            assertEquals("Blobs have different lengths",
-                         b1.length(), b2.length());
-            is1 = b1.getBinaryStream();
-            is2 = b2.getBinaryStream();
-            if (is1 == null || is2 == null) {
-                assertNull("Blob b2 has null-stream, blob b1 doesn't", is1);
-                assertNull("Blob b1 has null-stream, blob b2 doesn't", is2);
-                return;
-            }
-        } catch (SQLException sqle) {
-            fail("SQLException while asserting Blob equality: " +
-                    sqle.getMessage());
+        assertEquals("Blobs have different lengths",
+                     b1.length(), b2.length());
+        InputStream is1 = b1.getBinaryStream();
+        InputStream is2 = b2.getBinaryStream();
+        if (is1 == null || is2 == null) {
+            assertNull("Blob b2 has null-stream, blob b1 doesn't", is1);
+            assertNull("Blob b1 has null-stream, blob b2 doesn't", is2);
+            return;
         }
-        try {
-            long index = 1;
-            int by1 = is1.read();
-            int by2 = is2.read();
-            do {
+        long index = 1;
+        int by1 = is1.read();
+        int by2 = is2.read();
+        do {
+            // Avoid string concatenation for every byte in the stream.
+            if (by1 != by2) {
                 assertEquals("Blobs differ at index " + index,
                         by1, by2);
-                index++;
-                by1 = is1.read();
-                by2 = is2.read();
-            } while ( by1 != -1 || by2 != -1);
-        } catch (IOException ioe) {
-            fail("IOException while asserting Blob equality: " +
-                    ioe.getMessage());
-        }
+            }
+            index++;
+            by1 = is1.read();
+            by2 = is2.read();
+        } while ( by1 != -1 || by2 != -1);
+        is1.close();
+        is2.close();
     }
 
     /**
@@ -173,43 +170,40 @@
      * @param c1 first <code>Clob</code>.
      * @param c2 second <code>Clob</code>.
      * @throws AssertionFailedError if clobs are not equal.
+     * @throws IOException if reading or closing a stream fails
+     * @throws SQLException if obtaining a stream fails
      */
-    public static void assertEquals(Clob c1, Clob c2) {
+    public static void assertEquals(Clob c1, Clob c2)
+            throws IOException, SQLException {
         if (c1 == null || c2 == null) {
             assertNull("Clob c2 is null, c1 is not", c1);
             assertNull("Clob c1 is null, c2 is not", c2);
             return;
         }
-        Reader r1 = null, r2 = null;
-        try {
-            assertEquals("Clobs have different lengths",
-                         c1.length(), c2.length());
-            r1 = c1.getCharacterStream();
-            r2 = c2.getCharacterStream();
-            if (r1 == null || r2 == null) {
-                assertNull("Clob c2 has null-stream, clob c1 doesn't", r1);
-                assertNull("Clob c1 has null-stream, clob c2 doesn't", r2);
-                return;
-            }
-        } catch (SQLException sqle) {
-            fail("SQLException while asserting Clob equality: " +
-                    sqle.getMessage());
+        assertEquals("Clobs have different lengths",
+                     c1.length(), c2.length());
+        Reader r1 = c1.getCharacterStream();
+        Reader r2 = c2.getCharacterStream();
+        if (r1 == null || r2 == null) {
+            assertNull("Clob c2 has null-stream, clob c1 doesn't", r1);
+            assertNull("Clob c1 has null-stream, clob c2 doesn't", r2);
+            return;
         }
-        try {
-            long index = 1;
-            int ch1 = r1.read();
-            int ch2 = r2.read();
-            do {
+        long index = 1;
+        int ch1 = r1.read();
+        int ch2 = r2.read();
+        do {
+            // Avoid string concatenation for every char in the stream.
+            if (ch1 != ch2) {
                 assertEquals("Clobs differ at index " + index,
                         ch1, ch2);
-                index++;
-                ch1 = r1.read();
-                ch2 = r2.read();
-            } while (ch1 != -1 || ch2 != -1);
-        } catch (IOException ioe) {
-            fail("IOException while asserting Clob equality: " +
-                    ioe.getMessage());
-        }
+            }
+            index++;
+            ch1 = r1.read();
+            ch2 = r2.read();
+        } while (ch1 != -1 || ch2 != -1);
+        r1.close();
+        r2.close();
     }
 
     /**