Skip to content

Commit 5aada4c

Browse files
committed
Better tests for RandomAccessFiles.contentEquals()
1 parent 6b889d8 commit 5aada4c

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

src/test/java/org/apache/commons/io/RandomAccessFilesTest.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import java.nio.file.Files;
2929
import java.nio.file.Path;
3030
import java.nio.file.Paths;
31-
import java.util.Arrays;
3231

32+
import org.apache.commons.lang3.ArrayUtils;
3333
import org.junit.jupiter.params.ParameterizedTest;
3434
import org.junit.jupiter.params.provider.EnumSource;
3535

@@ -42,6 +42,10 @@ public class RandomAccessFilesTest {
4242
private static final Path PATH_RO_0 = Paths.get("src/test/resources/org/apache/commons/io/test-file-empty.bin");
4343
private static final Path PATH_RO_0_BIS = Paths.get("src/test/resources/org/apache/commons/io/test-file-empty2.bin");
4444

45+
private static byte reverse(final byte b) {
46+
return (byte) (~b & 0xff);
47+
}
48+
4549
@ParameterizedTest()
4650
@EnumSource(value = RandomAccessFileMode.class)
4751
public void testContentEquals(final RandomAccessFileMode mode) throws IOException {
@@ -83,11 +87,15 @@ public void testContentEquals(final RandomAccessFileMode mode) throws IOExceptio
8387
final Path bigFile2 = Files.createTempFile(getClass().getSimpleName(), "-2.bin");
8488
final Path bigFile3 = Files.createTempFile(getClass().getSimpleName(), "-3.bin");
8589
try {
86-
final int newLength = 1_000_000;
90+
// This length must match any restriction from the Surefire configuration.
91+
final int newLength = 5_000_000;
8792
final byte[] bytes1 = new byte[newLength];
8893
final byte[] bytes2 = new byte[newLength];
89-
Arrays.fill(bytes1, (byte) 1);
90-
Arrays.fill(bytes2, (byte) 2);
94+
// Make sure bytes1 and bytes2 are different despite the shuffle
95+
ArrayUtils.shuffle(bytes1);
96+
bytes1[0] = 1;
97+
ArrayUtils.shuffle(bytes2);
98+
bytes2[0] = 2;
9199
Files.write(bigFile1, bytes1);
92100
Files.write(bigFile2, bytes2);
93101
try (RandomAccessFile raf1 = mode.create(bigFile1);
@@ -96,9 +104,20 @@ public void testContentEquals(final RandomAccessFileMode mode) throws IOExceptio
96104
assertFalse(RandomAccessFiles.contentEquals(RandomAccessFiles.reset(raf2), RandomAccessFiles.reset(raf1)));
97105
assertTrue(RandomAccessFiles.contentEquals(RandomAccessFiles.reset(raf1), RandomAccessFiles.reset(raf1)));
98106
}
99-
// Make the last byte different
100-
final byte[] bytes3 = bytes1.clone();
101-
bytes3[bytes3.length - 1] = 9;
107+
// Make the LAST byte different.
108+
byte[] bytes3 = bytes1.clone();
109+
final int last = bytes3.length - 1;
110+
bytes3[last] = reverse(bytes3[last]);
111+
Files.write(bigFile3, bytes3);
112+
try (RandomAccessFile raf1 = mode.create(bigFile1);
113+
RandomAccessFile raf3 = mode.create(bigFile3)) {
114+
assertFalse(RandomAccessFiles.contentEquals(raf1, raf3));
115+
assertFalse(RandomAccessFiles.contentEquals(RandomAccessFiles.reset(raf3), RandomAccessFiles.reset(raf1)));
116+
}
117+
// Make a byte in the middle different
118+
bytes3 = bytes1.clone();
119+
final int middle = bytes3.length / 2;
120+
bytes3[middle] = reverse(bytes3[middle]);
102121
Files.write(bigFile3, bytes3);
103122
try (RandomAccessFile raf1 = mode.create(bigFile1);
104123
RandomAccessFile raf3 = mode.create(bigFile3)) {

0 commit comments

Comments
 (0)