You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2015/10/12 20:35:27 UTC

svn commit: r1708196 - in /webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io: ByteStreamComparator.java CharacterStreamComparator.java IOTestUtils.java

Author: veithen
Date: Mon Oct 12 18:35:26 2015
New Revision: 1708196

URL: http://svn.apache.org/viewvc?rev=1708196&view=rev
Log:
Improve diagnostic messages.

Modified:
    webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/ByteStreamComparator.java
    webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/CharacterStreamComparator.java
    webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/IOTestUtils.java

Modified: webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/ByteStreamComparator.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/ByteStreamComparator.java?rev=1708196&r1=1708195&r2=1708196&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/ByteStreamComparator.java (original)
+++ webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/ByteStreamComparator.java Mon Oct 12 18:35:26 2015
@@ -31,22 +31,42 @@ import java.io.OutputStream;
  */
 public class ByteStreamComparator extends OutputStream {
     private final InputStream in;
+    private final String name1;
+    private final String name2;
     private final byte[] compareBuffer = new byte[1024];
     private int position;
     
-    public ByteStreamComparator(InputStream in) {
+    /**
+     * Constructor.
+     * 
+     * @param in
+     *            the stream to compare to
+     * @param name1
+     *            the name of the stream passed as argument; used in error messages
+     * @param name2
+     *            a name for the stream represented by the data written to this instance; used in
+     *            error messages
+     */
+    public ByteStreamComparator(InputStream in, String name1, String name2) {
         this.in = in;
+        this.name1 = name1;
+        this.name2 = name2;
+    }
+
+    @Deprecated
+    public ByteStreamComparator(InputStream in) {
+        this(in, "s1", "s2");
     }
 
     public void write(byte[] buffer, int off, int len) throws IOException {
         while (len > 0) {
             int c = in.read(compareBuffer, 0, Math.min(compareBuffer.length, len));
             if (c == -1) {
-                fail("The two streams have different lengths");
+                fail("The two streams have different lengths: len(" + name1 + ") = " + position + " < len(" + name2 + ")");
             }
             for (int i=0; i<c; i++) {
                 if (buffer[off] != compareBuffer[i]) {
-                    fail("Byte mismatch at position " + position);
+                    fail("Byte mismatch: " + name1 + "[" + position + "] = " + compareBuffer[i] + " != " + name2 + "[" + position + "] = " + buffer[off]);
                 }
                 off++;
                 len--;
@@ -60,7 +80,7 @@ public class ByteStreamComparator extend
 
     public void close() throws IOException {
         if (in.read() != -1) {
-            fail("The two streams have different lengths");
+            fail("The two streams have different lengths: len(" + name1 + ") > len(" + name2 + ") = " + position);
         }
     }
 

Modified: webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/CharacterStreamComparator.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/CharacterStreamComparator.java?rev=1708196&r1=1708195&r2=1708196&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/CharacterStreamComparator.java (original)
+++ webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/CharacterStreamComparator.java Mon Oct 12 18:35:26 2015
@@ -31,22 +31,42 @@ import java.io.Writer;
  */
 public class CharacterStreamComparator extends Writer {
     private final Reader in;
+    private final String name1;
+    private final String name2;
     private final char[] compareBuffer = new char[1024];
     private int position;
     
-    public CharacterStreamComparator(Reader in) {
+    /**
+     * Constructor.
+     * 
+     * @param in
+     *            the stream to compare to
+     * @param name1
+     *            the name of the stream passed as argument; used in error messages
+     * @param name2
+     *            a name for the stream represented by the data written to this instance; used in
+     *            error messages
+     */
+    public CharacterStreamComparator(Reader in, String name1, String name2) {
         this.in = in;
+        this.name1 = name1;
+        this.name2 = name2;
+    }
+
+    @Deprecated
+    public CharacterStreamComparator(Reader in) {
+        this(in, "s1", "s2");
     }
 
     public void write(char[] buffer, int off, int len) throws IOException {
         while (len > 0) {
             int c = in.read(compareBuffer, 0, Math.min(compareBuffer.length, len));
             if (c == -1) {
-                fail("The two streams have different lengths");
+                fail("The two streams have different lengths: len(" + name1 + ") = " + position + " < len(" + name2 + ")");
             }
             for (int i=0; i<c; i++) {
                 if (buffer[off] != compareBuffer[i]) {
-                    fail("Character mismatch at position " + position);
+                    fail("Byte mismatch: " + name1 + "[" + position + "] = " + compareBuffer[i] + " != " + name2 + "[" + position + "] = " + buffer[off]);
                 }
                 off++;
                 len--;
@@ -60,7 +80,7 @@ public class CharacterStreamComparator e
 
     public void close() throws IOException {
         if (in.read() != -1) {
-            fail("The two streams have different lengths");
+            fail("The two streams have different lengths: len(" + name1 + ") > len(" + name2 + ") = " + position);
         }
     }
 }

Modified: webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/IOTestUtils.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/IOTestUtils.java?rev=1708196&r1=1708195&r2=1708196&view=diff
==============================================================================
--- webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/IOTestUtils.java (original)
+++ webservices/axiom/trunk/testing/testutils/src/main/java/org/apache/axiom/testutils/io/IOTestUtils.java Mon Oct 12 18:35:26 2015
@@ -30,14 +30,24 @@ import org.apache.commons.io.IOUtils;
 public final class IOTestUtils {
     private IOTestUtils() {}
     
+    @Deprecated
     public static void compareStreams(InputStream s1, InputStream s2) throws IOException {
-        OutputStream comparator = new ByteStreamComparator(s2);
+        compareStreams(s1, "s1", s2, "s2");
+    }
+    
+    public static void compareStreams(InputStream s1, String name1, InputStream s2, String name2) throws IOException {
+        OutputStream comparator = new ByteStreamComparator(s2, name2, name1);
         IOUtils.copy(s1, comparator);
         comparator.close();
     }
     
+    @Deprecated
     public static void compareStreams(Reader s1, Reader s2) throws IOException {
-        Writer comparator = new CharacterStreamComparator(s2);
+        compareStreams(s1, "s1", s2, "s2");
+    }
+    
+    public static void compareStreams(Reader s1, String name1, Reader s2, String name2) throws IOException {
+        Writer comparator = new CharacterStreamComparator(s2, name2, name1);
         IOUtils.copy(s1, comparator);
         comparator.close();
     }