This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git
The following commit(s) were added to refs/heads/master by this push:
new 7786c28c Use NIO in tests to read test fixtures
7786c28c is described below
commit 7786c28c39b2d3ddb255fc3e8d20b125aff338a5
Author: Gary D. Gregory <[email protected]>
AuthorDate: Sat Mar 15 15:33:17 2025 -0400
Use NIO in tests to read test fixtures
---
.../commons/codec/digest/DigestUtilsTest.java | 19 ++++----
.../apache/commons/codec/digest/XXHash32Test.java | 52 ++++++++++------------
2 files changed, 33 insertions(+), 38 deletions(-)
diff --git a/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java
b/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java
index a2ac153e..e0b33209 100644
--- a/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java
+++ b/src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java
@@ -27,9 +27,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
@@ -109,15 +108,16 @@ public class DigestUtilsTest {
}
@Test
- public void testDigestAs() throws IOException {
+ public void testDigestFileAs() throws IOException {
final String expected = "d41d8cd98f00b204e9800998ecf8427e";
final String pathName =
"src/test/resources/org/apache/commons/codec/empty.bin";
final String algo = MessageDigestAlgorithms.MD5;
- assertEquals(expected, new DigestUtils(algo).digestAsHex(new
File(pathName)));
- try (FileInputStream inputStream = new FileInputStream(pathName)) {
+ final Path path = Paths.get(pathName);
+ assertEquals(expected, new
DigestUtils(algo).digestAsHex(path.toFile()));
+ try (InputStream inputStream = Files.newInputStream(path)) {
assertEquals(expected, new
DigestUtils(algo).digestAsHex(inputStream));
}
- final byte[] allBytes = Files.readAllBytes(Paths.get(pathName));
+ final byte[] allBytes = Files.readAllBytes(path);
assertEquals(expected, new DigestUtils(algo).digestAsHex(allBytes));
assertEquals(expected, new
DigestUtils(algo).digestAsHex(ByteBuffer.wrap(allBytes)));
}
@@ -311,11 +311,12 @@ public class DigestUtilsTest {
final String pathName =
"src/test/resources/org/apache/commons/codec/empty.bin";
final String algo = MessageDigestAlgorithms.SHA_224;
final DigestUtils digestUtils = new DigestUtils(algo);
- assertEquals(expected, digestUtils.digestAsHex(new File(pathName)));
- try (FileInputStream inputStream = new FileInputStream(pathName)) {
+ final Path path = Paths.get(pathName);
+ assertEquals(expected, digestUtils.digestAsHex(path.toFile()));
+ try (InputStream inputStream = Files.newInputStream(path)) {
assertEquals(expected, digestUtils.digestAsHex(inputStream));
}
- final byte[] allBytes = Files.readAllBytes(Paths.get(pathName));
+ final byte[] allBytes = Files.readAllBytes(path);
assertEquals(expected, digestUtils.digestAsHex(allBytes));
assertEquals(expected,
digestUtils.digestAsHex(ByteBuffer.wrap(allBytes)));
}
diff --git a/src/test/java/org/apache/commons/codec/digest/XXHash32Test.java
b/src/test/java/org/apache/commons/codec/digest/XXHash32Test.java
index 0d0bda92..e62c13f1 100644
--- a/src/test/java/org/apache/commons/codec/digest/XXHash32Test.java
+++ b/src/test/java/org/apache/commons/codec/digest/XXHash32Test.java
@@ -19,14 +19,14 @@ package org.apache.commons.codec.digest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.net.URI;
import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
@@ -58,54 +58,48 @@ public class XXHash32Test {
return output.toByteArray();
}
- private File file;
+ private Path file;
private String expectedChecksum;
- public void initData(final String path, final String c) throws IOException
{
+ public void initData(final String path, final String c) throws Exception {
final URL url = XXHash32Test.class.getClassLoader().getResource(path);
if (url == null) {
throw new FileNotFoundException("couldn't find " + path);
}
- URI uri = null;
- try {
- uri = url.toURI();
- } catch (final java.net.URISyntaxException ex) {
- throw new IOException(ex);
- }
- file = new File(uri);
+ file = Paths.get(url.toURI());
expectedChecksum = c;
}
@ParameterizedTest
@MethodSource("data")
- public void verifyChecksum(final String path, final String c) throws
IOException {
+ public void verifyChecksum(final String path, final String c) throws
Exception {
initData(path, c);
- final XXHash32 h = new XXHash32();
- try (FileInputStream s = new FileInputStream(file)) {
- final byte[] b = toByteArray(s);
- h.update(b, 0, b.length);
+ final XXHash32 hasher = new XXHash32();
+ try (InputStream in = Files.newInputStream(file)) {
+ final byte[] bytes = toByteArray(in);
+ hasher.update(bytes, 0, bytes.length);
}
- assertEquals(expectedChecksum, Long.toHexString(h.getValue()),
"checksum for " + file.getName());
+ assertEquals(expectedChecksum, Long.toHexString(hasher.getValue()),
"checksum for " + file);
}
@ParameterizedTest
@MethodSource("data")
- public void verifyIncrementalChecksum(final String path, final String c)
throws IOException {
+ public void verifyIncrementalChecksum(final String path, final String c)
throws Exception {
initData(path, c);
- final XXHash32 h = new XXHash32();
- try (FileInputStream s = new FileInputStream(file)) {
- final byte[] b = toByteArray(s);
+ final XXHash32 hasher = new XXHash32();
+ try (InputStream in = Files.newInputStream(file)) {
+ final byte[] bytes = toByteArray(in);
// Hit the case where the hash should be reset
- h.update(b[0]);
- h.reset();
+ hasher.update(bytes[0]);
+ hasher.reset();
// Pass in chunks
- h.update(b[0]);
- h.update(b, 1, b.length - 2);
- h.update(b, b.length - 1, 1);
+ hasher.update(bytes[0]);
+ hasher.update(bytes, 1, bytes.length - 2);
+ hasher.update(bytes, bytes.length - 1, 1);
// Check the hash ignores negative length
- h.update(b, 0, -1);
+ hasher.update(bytes, 0, -1);
}
- assertEquals(expectedChecksum, Long.toHexString(h.getValue()),
"checksum for " + file.getName());
+ assertEquals(expectedChecksum, Long.toHexString(hasher.getValue()),
"checksum for " + file);
}
}