You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/03/02 13:53:03 UTC
(commons-io) 02/03: Better tests for RandomAccessFiles.contentEquals()
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git
commit 5aada4c51232d9e93e7f337696220dd8fc40ef44
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sun Mar 2 08:02:08 2025 -0500
Better tests for RandomAccessFiles.contentEquals()
---
.../apache/commons/io/RandomAccessFilesTest.java | 33 +++++++++++++++++-----
1 file changed, 26 insertions(+), 7 deletions(-)
diff --git a/src/test/java/org/apache/commons/io/RandomAccessFilesTest.java b/src/test/java/org/apache/commons/io/RandomAccessFilesTest.java
index 564a27af3..67ace2037 100644
--- a/src/test/java/org/apache/commons/io/RandomAccessFilesTest.java
+++ b/src/test/java/org/apache/commons/io/RandomAccessFilesTest.java
@@ -28,8 +28,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.util.Arrays;
+import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
@@ -42,6 +42,10 @@ public class RandomAccessFilesTest {
private static final Path PATH_RO_0 = Paths.get("src/test/resources/org/apache/commons/io/test-file-empty.bin");
private static final Path PATH_RO_0_BIS = Paths.get("src/test/resources/org/apache/commons/io/test-file-empty2.bin");
+ private static byte reverse(final byte b) {
+ return (byte) (~b & 0xff);
+ }
+
@ParameterizedTest()
@EnumSource(value = RandomAccessFileMode.class)
public void testContentEquals(final RandomAccessFileMode mode) throws IOException {
@@ -83,11 +87,15 @@ public void testContentEquals(final RandomAccessFileMode mode) throws IOExceptio
final Path bigFile2 = Files.createTempFile(getClass().getSimpleName(), "-2.bin");
final Path bigFile3 = Files.createTempFile(getClass().getSimpleName(), "-3.bin");
try {
- final int newLength = 1_000_000;
+ // This length must match any restriction from the Surefire configuration.
+ final int newLength = 5_000_000;
final byte[] bytes1 = new byte[newLength];
final byte[] bytes2 = new byte[newLength];
- Arrays.fill(bytes1, (byte) 1);
- Arrays.fill(bytes2, (byte) 2);
+ // Make sure bytes1 and bytes2 are different despite the shuffle
+ ArrayUtils.shuffle(bytes1);
+ bytes1[0] = 1;
+ ArrayUtils.shuffle(bytes2);
+ bytes2[0] = 2;
Files.write(bigFile1, bytes1);
Files.write(bigFile2, bytes2);
try (RandomAccessFile raf1 = mode.create(bigFile1);
@@ -96,9 +104,20 @@ public void testContentEquals(final RandomAccessFileMode mode) throws IOExceptio
assertFalse(RandomAccessFiles.contentEquals(RandomAccessFiles.reset(raf2), RandomAccessFiles.reset(raf1)));
assertTrue(RandomAccessFiles.contentEquals(RandomAccessFiles.reset(raf1), RandomAccessFiles.reset(raf1)));
}
- // Make the last byte different
- final byte[] bytes3 = bytes1.clone();
- bytes3[bytes3.length - 1] = 9;
+ // Make the LAST byte different.
+ byte[] bytes3 = bytes1.clone();
+ final int last = bytes3.length - 1;
+ bytes3[last] = reverse(bytes3[last]);
+ Files.write(bigFile3, bytes3);
+ try (RandomAccessFile raf1 = mode.create(bigFile1);
+ RandomAccessFile raf3 = mode.create(bigFile3)) {
+ assertFalse(RandomAccessFiles.contentEquals(raf1, raf3));
+ assertFalse(RandomAccessFiles.contentEquals(RandomAccessFiles.reset(raf3), RandomAccessFiles.reset(raf1)));
+ }
+ // Make a byte in the middle different
+ bytes3 = bytes1.clone();
+ final int middle = bytes3.length / 2;
+ bytes3[middle] = reverse(bytes3[middle]);
Files.write(bigFile3, bytes3);
try (RandomAccessFile raf1 = mode.create(bigFile1);
RandomAccessFile raf3 = mode.create(bigFile3)) {