From e34a13c32bc8acc7e2d657e3658f5d689e4d6b1d Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 21 Jun 2026 17:11:28 +0000 Subject: [PATCH] test(loader): cover resourceMatchesFile match + content-mismatch branches PR #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 Claude-Session: https://claude.ai/code/session_01Qg1mYW7hHjtVvAfMeMEmPq --- .../llama/loader/LlamaLoaderTest.java | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/test/java/net/ladenthin/llama/loader/LlamaLoaderTest.java b/src/test/java/net/ladenthin/llama/loader/LlamaLoaderTest.java index 8cc5dbe6..72d424ed 100644 --- a/src/test/java/net/ladenthin/llama/loader/LlamaLoaderTest.java +++ b/src/test/java/net/ladenthin/llama/loader/LlamaLoaderTest.java @@ -23,10 +23,15 @@ + "cleanup; contentsEquals performs a correct byte-level stream comparison " + "including BufferedInputStream wrapping and length mismatches; getTempDir " + "honours the 'net.ladenthin.llama.tmpdir' system-property override; and " - + "getNativeResourcePath produces the expected classpath resource prefix.") + + "getNativeResourcePath produces the expected classpath resource prefix; and " + + "resourceMatchesFile compares a classpath resource to an on-disk file byte-for-byte.") public class LlamaLoaderTest { private static final String TMPDIR_PROP = LlamaSystemProperties.PREFIX + ".tmpdir"; + + /** A small file present on the test classpath, used as a byte-comparison fixture. */ + private static final String EXISTING_TEST_RESOURCE = "/images/test-image.jpg"; + private String previousTmpDir; @BeforeEach @@ -111,6 +116,38 @@ public void resourceMatchesFileFalseWhenResourceAbsent() throws IOException { } } + @Test + public void resourceMatchesFileTrueWhenBytesIdentical() throws IOException { + // The fast-path reuse predicate: a present resource and a byte-identical on-disk copy match. + java.nio.file.Path tmp = java.nio.file.Files.createTempFile("llama-loader-test", ".bin"); + try { + try (java.io.InputStream in = LlamaLoader.class.getResourceAsStream(EXISTING_TEST_RESOURCE)) { + assertNotNull(in, "fixture must be on the test classpath: " + EXISTING_TEST_RESOURCE); + java.nio.file.Files.copy(in, tmp, java.nio.file.StandardCopyOption.REPLACE_EXISTING); + } + assertTrue(LlamaLoader.resourceMatchesFile(EXISTING_TEST_RESOURCE, tmp)); + } finally { + java.nio.file.Files.deleteIfExists(tmp); + } + } + + @Test + public void resourceMatchesFileFalseWhenContentDiffers() throws IOException { + // A present resource whose on-disk copy diverges (here: one extra trailing byte) must NOT match, + // so a stale/partial file is never mistaken for the shipped library on the reuse fast path. + java.nio.file.Path tmp = java.nio.file.Files.createTempFile("llama-loader-test", ".bin"); + try { + try (java.io.InputStream in = LlamaLoader.class.getResourceAsStream(EXISTING_TEST_RESOURCE)) { + assertNotNull(in, "fixture must be on the test classpath: " + EXISTING_TEST_RESOURCE); + java.nio.file.Files.copy(in, tmp, java.nio.file.StandardCopyOption.REPLACE_EXISTING); + } + java.nio.file.Files.write(tmp, new byte[] {0}, java.nio.file.StandardOpenOption.APPEND); + assertFalse(LlamaLoader.resourceMatchesFile(EXISTING_TEST_RESOURCE, tmp)); + } finally { + java.nio.file.Files.deleteIfExists(tmp); + } + } + @Test public void testContentsEqualsBothEmpty() throws IOException { assertTrue(LlamaLoader.contentsEquals(