|
19 | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; |
20 | 20 |
|
21 | 21 | import java.io.ByteArrayOutputStream; |
22 | | -import java.io.File; |
23 | | -import java.io.FileInputStream; |
24 | 22 | import java.io.FileNotFoundException; |
25 | 23 | import java.io.IOException; |
26 | 24 | import java.io.InputStream; |
27 | 25 | import java.io.OutputStream; |
28 | | -import java.net.URI; |
29 | 26 | import java.net.URL; |
| 27 | +import java.nio.file.Files; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.nio.file.Paths; |
30 | 30 | import java.util.stream.Stream; |
31 | 31 |
|
32 | 32 | import org.apache.commons.io.IOUtils; |
@@ -58,54 +58,48 @@ private static byte[] toByteArray(final InputStream input) throws IOException { |
58 | 58 | return output.toByteArray(); |
59 | 59 | } |
60 | 60 |
|
61 | | - private File file; |
| 61 | + private Path file; |
62 | 62 |
|
63 | 63 | private String expectedChecksum; |
64 | 64 |
|
65 | | - public void initData(final String path, final String c) throws IOException { |
| 65 | + public void initData(final String path, final String c) throws Exception { |
66 | 66 | final URL url = XXHash32Test.class.getClassLoader().getResource(path); |
67 | 67 | if (url == null) { |
68 | 68 | throw new FileNotFoundException("couldn't find " + path); |
69 | 69 | } |
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()); |
77 | 71 | expectedChecksum = c; |
78 | 72 | } |
79 | 73 |
|
80 | 74 | @ParameterizedTest |
81 | 75 | @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 { |
83 | 77 | 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); |
88 | 82 | } |
89 | | - assertEquals(expectedChecksum, Long.toHexString(h.getValue()), "checksum for " + file.getName()); |
| 83 | + assertEquals(expectedChecksum, Long.toHexString(hasher.getValue()), "checksum for " + file); |
90 | 84 | } |
91 | 85 |
|
92 | 86 | @ParameterizedTest |
93 | 87 | @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 { |
95 | 89 | 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); |
99 | 93 | // 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(); |
102 | 96 | // 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); |
106 | 100 | // Check the hash ignores negative length |
107 | | - h.update(b, 0, -1); |
| 101 | + hasher.update(bytes, 0, -1); |
108 | 102 | } |
109 | | - assertEquals(expectedChecksum, Long.toHexString(h.getValue()), "checksum for " + file.getName()); |
| 103 | + assertEquals(expectedChecksum, Long.toHexString(hasher.getValue()), "checksum for " + file); |
110 | 104 | } |
111 | 105 | } |
0 commit comments