hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e..@apache.org
Subject svn commit: r1126287 - in /hadoop/common/trunk: CHANGES.txt src/java/org/apache/hadoop/io/MD5Hash.java src/test/core/org/apache/hadoop/io/TestMD5Hash.java
Date Mon, 23 May 2011 01:27:43 GMT
Author: eli
Date: Mon May 23 01:27:43 2011
New Revision: 1126287

URL: http://svn.apache.org/viewvc?rev=1126287&view=rev
Log:
HADOOP-7318. MD5Hash factory should reset the digester it returns. Contributed by Todd Lipcon

Modified:
    hadoop/common/trunk/CHANGES.txt
    hadoop/common/trunk/src/java/org/apache/hadoop/io/MD5Hash.java
    hadoop/common/trunk/src/test/core/org/apache/hadoop/io/TestMD5Hash.java

Modified: hadoop/common/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/CHANGES.txt?rev=1126287&r1=1126286&r2=1126287&view=diff
==============================================================================
--- hadoop/common/trunk/CHANGES.txt (original)
+++ hadoop/common/trunk/CHANGES.txt Mon May 23 01:27:43 2011
@@ -717,6 +717,9 @@ Release 0.22.0 - Unreleased
     HADOOP-7305. Eclipse project classpath should include tools.jar from JDK.
     (Niels Basjes via todd)
 
+    HADOOP-7318. MD5Hash factory should reset the digester it returns.
+    (todd via eli)
+
 Release 0.21.1 - Unreleased
 
   IMPROVEMENTS

Modified: hadoop/common/trunk/src/java/org/apache/hadoop/io/MD5Hash.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/java/org/apache/hadoop/io/MD5Hash.java?rev=1126287&r1=1126286&r2=1126287&view=diff
==============================================================================
--- hadoop/common/trunk/src/java/org/apache/hadoop/io/MD5Hash.java (original)
+++ hadoop/common/trunk/src/java/org/apache/hadoop/io/MD5Hash.java Mon May 23 01:27:43 2011
@@ -98,7 +98,9 @@ public class MD5Hash implements Writable
    * Create a thread local MD5 digester
    */
   public static MessageDigest getDigester() {
-    return DIGESTER_FACTORY.get();
+    MessageDigest digester = DIGESTER_FACTORY.get();
+    digester.reset();
+    return digester;
   }
 
   /** Construct a hash value for the content from the InputStream. */

Modified: hadoop/common/trunk/src/test/core/org/apache/hadoop/io/TestMD5Hash.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/src/test/core/org/apache/hadoop/io/TestMD5Hash.java?rev=1126287&r1=1126286&r2=1126287&view=diff
==============================================================================
--- hadoop/common/trunk/src/test/core/org/apache/hadoop/io/TestMD5Hash.java (original)
+++ hadoop/common/trunk/src/test/core/org/apache/hadoop/io/TestMD5Hash.java Mon May 23 01:27:43
2011
@@ -20,6 +20,9 @@ package org.apache.hadoop.io;
 
 import org.apache.hadoop.io.TestWritable;
 import junit.framework.TestCase;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
 import java.security.MessageDigest;
 import java.util.Random;
 
@@ -109,7 +112,38 @@ public class TestMD5Hash extends TestCas
     t2.start();
     t1.join();
     t2.join();
-    
   }
-	
+
+  public void testFactoryReturnsClearedHashes() throws IOException {
+    // A stream that will throw an IOE after reading some bytes
+    ByteArrayInputStream failingStream = new ByteArrayInputStream(
+        "xxxx".getBytes()) {
+      @Override
+      public synchronized int read(byte[] b) throws IOException {
+        int ret = super.read(b);
+        if (ret <= 0) {
+          throw new IOException("Injected fault");
+        }
+        return ret;
+      }
+    };
+    final String TEST_STRING = "hello";
+
+    // Calculate the correct digest for the test string
+    MD5Hash expectedHash = MD5Hash.digest(TEST_STRING);
+
+    // Hashing again should give the same result
+    assertEquals(expectedHash, MD5Hash.digest(TEST_STRING));
+
+    // Try to hash a stream which will fail halfway through
+    try {
+      MD5Hash.digest(failingStream);
+      fail("didnt throw!");
+    } catch (Exception e) {
+      // expected
+    }
+
+    // Make sure we get the same result
+    assertEquals(expectedHash, MD5Hash.digest(TEST_STRING));
+  }
 }



Mime
View raw message