HADOOP-7318. MD5Hash factory should reset the digester it returns. Contributed by Todd Lipcon

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1126287 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Eli Collins 2011-05-23 01:27:43 +00:00
parent 088693f0a0
commit 18ac8283b6
3 changed files with 42 additions and 3 deletions

View File

@ -717,6 +717,9 @@ Release 0.22.0 - Unreleased
HADOOP-7305. Eclipse project classpath should include tools.jar from JDK. HADOOP-7305. Eclipse project classpath should include tools.jar from JDK.
(Niels Basjes via todd) (Niels Basjes via todd)
HADOOP-7318. MD5Hash factory should reset the digester it returns.
(todd via eli)
Release 0.21.1 - Unreleased Release 0.21.1 - Unreleased
IMPROVEMENTS IMPROVEMENTS

View File

@ -98,7 +98,9 @@ public class MD5Hash implements WritableComparable<MD5Hash> {
* Create a thread local MD5 digester * Create a thread local MD5 digester
*/ */
public static MessageDigest getDigester() { 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. */ /** Construct a hash value for the content from the InputStream. */

View File

@ -20,6 +20,9 @@ package org.apache.hadoop.io;
import org.apache.hadoop.io.TestWritable; import org.apache.hadoop.io.TestWritable;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.Random; import java.util.Random;
@ -109,7 +112,38 @@ public class TestMD5Hash extends TestCase {
t2.start(); t2.start();
t1.join(); t1.join();
t2.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));
}
} }