|
23 | 23 | + "cleanup; contentsEquals performs a correct byte-level stream comparison " |
24 | 24 | + "including BufferedInputStream wrapping and length mismatches; getTempDir " |
25 | 25 | + "honours the 'net.ladenthin.llama.tmpdir' system-property override; and " |
26 | | - + "getNativeResourcePath produces the expected classpath resource prefix.") |
| 26 | + + "getNativeResourcePath produces the expected classpath resource prefix; and " |
| 27 | + + "resourceMatchesFile compares a classpath resource to an on-disk file byte-for-byte.") |
27 | 28 | public class LlamaLoaderTest { |
28 | 29 |
|
29 | 30 | private static final String TMPDIR_PROP = LlamaSystemProperties.PREFIX + ".tmpdir"; |
| 31 | + |
| 32 | + /** A small file present on the test classpath, used as a byte-comparison fixture. */ |
| 33 | + private static final String EXISTING_TEST_RESOURCE = "/images/test-image.jpg"; |
| 34 | + |
30 | 35 | private String previousTmpDir; |
31 | 36 |
|
32 | 37 | @BeforeEach |
@@ -111,6 +116,38 @@ public void resourceMatchesFileFalseWhenResourceAbsent() throws IOException { |
111 | 116 | } |
112 | 117 | } |
113 | 118 |
|
| 119 | + @Test |
| 120 | + public void resourceMatchesFileTrueWhenBytesIdentical() throws IOException { |
| 121 | + // The fast-path reuse predicate: a present resource and a byte-identical on-disk copy match. |
| 122 | + java.nio.file.Path tmp = java.nio.file.Files.createTempFile("llama-loader-test", ".bin"); |
| 123 | + try { |
| 124 | + try (java.io.InputStream in = LlamaLoader.class.getResourceAsStream(EXISTING_TEST_RESOURCE)) { |
| 125 | + assertNotNull(in, "fixture must be on the test classpath: " + EXISTING_TEST_RESOURCE); |
| 126 | + java.nio.file.Files.copy(in, tmp, java.nio.file.StandardCopyOption.REPLACE_EXISTING); |
| 127 | + } |
| 128 | + assertTrue(LlamaLoader.resourceMatchesFile(EXISTING_TEST_RESOURCE, tmp)); |
| 129 | + } finally { |
| 130 | + java.nio.file.Files.deleteIfExists(tmp); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void resourceMatchesFileFalseWhenContentDiffers() throws IOException { |
| 136 | + // A present resource whose on-disk copy diverges (here: one extra trailing byte) must NOT match, |
| 137 | + // so a stale/partial file is never mistaken for the shipped library on the reuse fast path. |
| 138 | + java.nio.file.Path tmp = java.nio.file.Files.createTempFile("llama-loader-test", ".bin"); |
| 139 | + try { |
| 140 | + try (java.io.InputStream in = LlamaLoader.class.getResourceAsStream(EXISTING_TEST_RESOURCE)) { |
| 141 | + assertNotNull(in, "fixture must be on the test classpath: " + EXISTING_TEST_RESOURCE); |
| 142 | + java.nio.file.Files.copy(in, tmp, java.nio.file.StandardCopyOption.REPLACE_EXISTING); |
| 143 | + } |
| 144 | + java.nio.file.Files.write(tmp, new byte[] {0}, java.nio.file.StandardOpenOption.APPEND); |
| 145 | + assertFalse(LlamaLoader.resourceMatchesFile(EXISTING_TEST_RESOURCE, tmp)); |
| 146 | + } finally { |
| 147 | + java.nio.file.Files.deleteIfExists(tmp); |
| 148 | + } |
| 149 | + } |
| 150 | + |
114 | 151 | @Test |
115 | 152 | public void testContentsEqualsBothEmpty() throws IOException { |
116 | 153 | assertTrue(LlamaLoader.contentsEquals( |
|
0 commit comments