Skip to content

Commit e34a13c

Browse files
committed
test(loader): cover resourceMatchesFile match + content-mismatch branches
PR bernardladenthin#260 added LlamaLoader.resourceMatchesFile (the fast-path reuse predicate for race-safe native-lib extraction) but unit-tested only the absent-resource -> false branch. Add its two core branches: - a present resource vs a byte-identical on-disk copy -> true (the reuse predicate itself), and - a present resource whose on-disk copy diverges (one extra trailing byte) -> false, so a stale/partial file is never mistaken for the shipped library on the reuse fast path. Uses the committed /images/test-image.jpg test-classpath fixture; pure-Java, no model or native library required. LlamaLoaderTest 25/25 green; Spotless clean. The private extractFile/moveIntoPlace atomic-move + reuse flow stays covered end-to-end by NativeLibraryLoadSmokeTest in the CI Java Tests jobs (needs the built native lib). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qg1mYW7hHjtVvAfMeMEmPq
1 parent b5c36e7 commit e34a13c

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)