Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/test/java/net/ladenthin/llama/loader/LlamaLoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Loading