Skip to content

Commit 7786c28

Browse files
committed
Use NIO in tests to read test fixtures
1 parent e5d7a79 commit 7786c28

File tree

2 files changed

+33
-38
lines changed

2 files changed

+33
-38
lines changed

src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
2727
import static org.junit.jupiter.api.Assumptions.assumeTrue;
2828

2929
import java.io.ByteArrayInputStream;
30-
import java.io.File;
31-
import java.io.FileInputStream;
3230
import java.io.IOException;
31+
import java.io.InputStream;
3332
import java.io.OutputStream;
3433
import java.io.RandomAccessFile;
3534
import java.nio.ByteBuffer;
@@ -109,15 +108,16 @@ public void tearDown() throws IOException {
109108
}
110109

111110
@Test
112-
public void testDigestAs() throws IOException {
111+
public void testDigestFileAs() throws IOException {
113112
final String expected = "d41d8cd98f00b204e9800998ecf8427e";
114113
final String pathName = "src/test/resources/org/apache/commons/codec/empty.bin";
115114
final String algo = MessageDigestAlgorithms.MD5;
116-
assertEquals(expected, new DigestUtils(algo).digestAsHex(new File(pathName)));
117-
try (FileInputStream inputStream = new FileInputStream(pathName)) {
115+
final Path path = Paths.get(pathName);
116+
assertEquals(expected, new DigestUtils(algo).digestAsHex(path.toFile()));
117+
try (InputStream inputStream = Files.newInputStream(path)) {
118118
assertEquals(expected, new DigestUtils(algo).digestAsHex(inputStream));
119119
}
120-
final byte[] allBytes = Files.readAllBytes(Paths.get(pathName));
120+
final byte[] allBytes = Files.readAllBytes(path);
121121
assertEquals(expected, new DigestUtils(algo).digestAsHex(allBytes));
122122
assertEquals(expected, new DigestUtils(algo).digestAsHex(ByteBuffer.wrap(allBytes)));
123123
}
@@ -311,11 +311,12 @@ public void testSha224_FileAsHex() throws IOException {
311311
final String pathName = "src/test/resources/org/apache/commons/codec/empty.bin";
312312
final String algo = MessageDigestAlgorithms.SHA_224;
313313
final DigestUtils digestUtils = new DigestUtils(algo);
314-
assertEquals(expected, digestUtils.digestAsHex(new File(pathName)));
315-
try (FileInputStream inputStream = new FileInputStream(pathName)) {
314+
final Path path = Paths.get(pathName);
315+
assertEquals(expected, digestUtils.digestAsHex(path.toFile()));
316+
try (InputStream inputStream = Files.newInputStream(path)) {
316317
assertEquals(expected, digestUtils.digestAsHex(inputStream));
317318
}
318-
final byte[] allBytes = Files.readAllBytes(Paths.get(pathName));
319+
final byte[] allBytes = Files.readAllBytes(path);
319320
assertEquals(expected, digestUtils.digestAsHex(allBytes));
320321
assertEquals(expected, digestUtils.digestAsHex(ByteBuffer.wrap(allBytes)));
321322
}

src/test/java/org/apache/commons/codec/digest/XXHash32Test.java

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020

2121
import java.io.ByteArrayOutputStream;
22-
import java.io.File;
23-
import java.io.FileInputStream;
2422
import java.io.FileNotFoundException;
2523
import java.io.IOException;
2624
import java.io.InputStream;
2725
import java.io.OutputStream;
28-
import java.net.URI;
2926
import java.net.URL;
27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
29+
import java.nio.file.Paths;
3030
import java.util.stream.Stream;
3131

3232
import org.apache.commons.io.IOUtils;
@@ -58,54 +58,48 @@ private static byte[] toByteArray(final InputStream input) throws IOException {
5858
return output.toByteArray();
5959
}
6060

61-
private File file;
61+
private Path file;
6262

6363
private String expectedChecksum;
6464

65-
public void initData(final String path, final String c) throws IOException {
65+
public void initData(final String path, final String c) throws Exception {
6666
final URL url = XXHash32Test.class.getClassLoader().getResource(path);
6767
if (url == null) {
6868
throw new FileNotFoundException("couldn't find " + path);
6969
}
70-
URI uri = null;
71-
try {
72-
uri = url.toURI();
73-
} catch (final java.net.URISyntaxException ex) {
74-
throw new IOException(ex);
75-
}
76-
file = new File(uri);
70+
file = Paths.get(url.toURI());
7771
expectedChecksum = c;
7872
}
7973

8074
@ParameterizedTest
8175
@MethodSource("data")
82-
public void verifyChecksum(final String path, final String c) throws IOException {
76+
public void verifyChecksum(final String path, final String c) throws Exception {
8377
initData(path, c);
84-
final XXHash32 h = new XXHash32();
85-
try (FileInputStream s = new FileInputStream(file)) {
86-
final byte[] b = toByteArray(s);
87-
h.update(b, 0, b.length);
78+
final XXHash32 hasher = new XXHash32();
79+
try (InputStream in = Files.newInputStream(file)) {
80+
final byte[] bytes = toByteArray(in);
81+
hasher.update(bytes, 0, bytes.length);
8882
}
89-
assertEquals(expectedChecksum, Long.toHexString(h.getValue()), "checksum for " + file.getName());
83+
assertEquals(expectedChecksum, Long.toHexString(hasher.getValue()), "checksum for " + file);
9084
}
9185

9286
@ParameterizedTest
9387
@MethodSource("data")
94-
public void verifyIncrementalChecksum(final String path, final String c) throws IOException {
88+
public void verifyIncrementalChecksum(final String path, final String c) throws Exception {
9589
initData(path, c);
96-
final XXHash32 h = new XXHash32();
97-
try (FileInputStream s = new FileInputStream(file)) {
98-
final byte[] b = toByteArray(s);
90+
final XXHash32 hasher = new XXHash32();
91+
try (InputStream in = Files.newInputStream(file)) {
92+
final byte[] bytes = toByteArray(in);
9993
// Hit the case where the hash should be reset
100-
h.update(b[0]);
101-
h.reset();
94+
hasher.update(bytes[0]);
95+
hasher.reset();
10296
// Pass in chunks
103-
h.update(b[0]);
104-
h.update(b, 1, b.length - 2);
105-
h.update(b, b.length - 1, 1);
97+
hasher.update(bytes[0]);
98+
hasher.update(bytes, 1, bytes.length - 2);
99+
hasher.update(bytes, bytes.length - 1, 1);
106100
// Check the hash ignores negative length
107-
h.update(b, 0, -1);
101+
hasher.update(bytes, 0, -1);
108102
}
109-
assertEquals(expectedChecksum, Long.toHexString(h.getValue()), "checksum for " + file.getName());
103+
assertEquals(expectedChecksum, Long.toHexString(hasher.getValue()), "checksum for " + file);
110104
}
111105
}

0 commit comments

Comments
 (0)