You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@mrunit.apache.org by Eugene Dzhurinsky <jd...@gmail.com> on 2013/09/16 23:03:13 UTC

Dump content of binary data in Pair to file on test failure.

Hello!

I was looking for a way to not produce huge output with content of binary file
if expected output does not match generated one in test.

I realized that there is no such support (or I didn't find it), so I patched
Pair and allowed it to create binary file in temp folder instead of printing
the binary stream.

Perhaps somebody will find it useful as well. I tried to retain current
behavior of Pair output, so I added check for presence of system property,
which sets up threshold for content size to be stored in file.

Please take a look at patch below.

--
Eugene Dzhurinsky

-- >8 --

Subject: [PATCH] Add system property, which controls how to present content of  binary files:

-DPAIR_MAX_BYTES_REPORT=100

makes Pair output content of value to temporary file, and return name of
the file if content length is greater than 100 bytes.

By default content is returned as byte array.
---
 .../java/org/apache/hadoop/mrunit/types/Pair.java  | 48 +++++++++++++++++++++-
 1 file changed, 46 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/hadoop/mrunit/types/Pair.java b/src/main/java/org/apache/hadoop/mrunit/types/Pair.java
index 5ce462b..daa2458 100644
--- a/src/main/java/org/apache/hadoop/mrunit/types/Pair.java
+++ b/src/main/java/org/apache/hadoop/mrunit/types/Pair.java
@@ -17,8 +17,14 @@
  */
 package org.apache.hadoop.mrunit.types;
 
+import org.apache.hadoop.io.BytesWritable;
+import org.apache.hadoop.io.IOUtils;
+
 import static org.apache.hadoop.mrunit.internal.util.ArgumentChecker.returnNonNull;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.util.Comparator;
 
 /**
@@ -30,9 +36,14 @@ import java.util.Comparator;
 @SuppressWarnings("unchecked")
 public class Pair<S, T> implements Comparable<Pair<S, T>> {
 
+  private static final int threshold = Integer.parseInt(System.getProperty("PAIR_MAX_BYTES_REPORT", String.valueOf(Integer.MAX_VALUE)));
+
   private final S first;
   private final T second;
 
+  private volatile File logFile = null;
+
+
   public Pair(final S car, final T cdr) {
     first = returnNonNull(car);
     second = returnNonNull(cdr);
@@ -91,6 +102,39 @@ public class Pair<S, T> implements Comparable<Pair<S, T>> {
 
   @Override
   public String toString() {
-    return "(" + first + ", " + second + ")";
+    if (second != null && BytesWritable.class.equals(second.getClass())) {
+      BytesWritable wrt = (BytesWritable) second;
+      if (wrt.getLength() > threshold) {
+        return "(" + first + ", result file: '" + write2FileQuiet(wrt.getBytes()) + "')";
+      } else {
+        return "(" + first + ", " + second + ")";
+      }
+    } else {
+      return "(" + first + ", " + second + ")";
+    }
   }
-}
+
+  private File write2FileQuiet(byte[] data) {
+    if (logFile != null) {
+      return logFile;
+    }
+    FileOutputStream out = null;
+    try {
+      logFile = File.createTempFile("mrunit", ".out");
+      out = new FileOutputStream(logFile);
+      out.write(data);
+      out.flush();
+    } catch (IOException e) {
+      e.printStackTrace();
+    } finally {
+      if (out != null)
+        try {
+          out.close();
+        } catch (IOException e) {
+          e.printStackTrace();
+        }
+    }
+    return logFile;
+  }
+
+}
\ No newline at end of file
-- 
1.8.4