Skip to content

Commit 922d82c

Browse files
Merge pull request #262 from bernardladenthin/claude/eloquent-bell-hgy21r
test(loader): cover LlamaLoader.resourceMatchesFile match + content-mismatch branches
2 parents b5c36e7 + e34a13c commit 922d82c

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

src/test/java/net/ladenthin/llama/loader/LlamaLoaderTest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@
2323
+ "cleanup; contentsEquals performs a correct byte-level stream comparison "
2424
+ "including BufferedInputStream wrapping and length mismatches; getTempDir "
2525
+ "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.")
2728
public class LlamaLoaderTest {
2829

2930
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+
3035
private String previousTmpDir;
3136

3237
@BeforeEach
@@ -111,6 +116,38 @@ public void resourceMatchesFileFalseWhenResourceAbsent() throws IOException {
111116
}
112117
}
113118

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+
114151
@Test
115152
public void testContentsEqualsBothEmpty() throws IOException {
116153
assertTrue(LlamaLoader.contentsEquals(

0 commit comments

Comments
 (0)