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 14:04:55 UTC

(commons-io) 02/02: Better tests for FileChannels.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 5d3322ba11a0541894ece9e7def5d45634eea1d2
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Sun Mar 2 09:04:48 2025 -0500

    Better tests for FileChannels.contentEquals()
---
 .../commons/io/channels/FileChannelsTest.java      | 37 +++++++++++++++-------
 1 file changed, 25 insertions(+), 12 deletions(-)

diff --git a/src/test/java/org/apache/commons/io/channels/FileChannelsTest.java b/src/test/java/org/apache/commons/io/channels/FileChannelsTest.java
index a8aef2ef3..d8922194b 100644
--- a/src/test/java/org/apache/commons/io/channels/FileChannelsTest.java
+++ b/src/test/java/org/apache/commons/io/channels/FileChannelsTest.java
@@ -93,6 +93,19 @@ public void testContentEquals(final int bufferSize) throws IOException {
             try (FileChannel fc1 = FileChannel.open(bigFile1); FileChannel fc3 = FileChannel.open(bigFile3)) {
                 assertFalse(FileChannels.contentEquals(fc1, fc3, bufferSize));
                 assertFalse(FileChannels.contentEquals(reset(fc3), reset(fc1), bufferSize));
+                // Test just the last byte
+                fc1.position(last);
+                fc3.position(last);
+                assertFalse(FileChannels.contentEquals(fc1, fc3, bufferSize));
+            }
+            // Make the LAST byte equal.
+            bytes3 = bytes1.clone();
+            Files.write(bigFile3, bytes3);
+            try (FileChannel fc1 = FileChannel.open(bigFile1); FileChannel fc3 = FileChannel.open(bigFile3)) {
+                // Test just the last byte
+                fc1.position(last);
+                fc3.position(last);
+                assertTrue(FileChannels.contentEquals(fc1, fc3, bufferSize));
             }
             // Make a byte in the middle different
             bytes3 = bytes1.clone();
@@ -121,16 +134,16 @@ private void testContentEquals(final String content1, final String content2, fin
         FileUtils.writeStringToFile(file2, content2, US_ASCII);
         // File checksums are different
         assertNotEquals(FileUtils.checksumCRC32(file1), FileUtils.checksumCRC32(file2));
-        try (FileInputStream stream1 = new FileInputStream(file1);
-                FileInputStream stream2 = new FileInputStream(file2);
-                FileChannel channel1 = stream1.getChannel();
-                FileChannel channel2 = stream2.getChannel()) {
+        try (FileInputStream in1 = new FileInputStream(file1);
+                FileInputStream in2 = new FileInputStream(file2);
+                FileChannel channel1 = in1.getChannel();
+                FileChannel channel2 = in2.getChannel()) {
             assertFalse(FileChannels.contentEquals(channel1, channel2, bufferSize));
         }
-        try (FileInputStream stream1 = new FileInputStream(file1);
-                FileInputStream stream2 = new FileInputStream(file2);
-                FileChannel channel1 = stream1.getChannel();
-                FileChannel channel2 = stream2.getChannel()) {
+        try (FileInputStream in1 = new FileInputStream(file1);
+                FileInputStream in2 = new FileInputStream(file2);
+                FileChannel channel1 = in1.getChannel();
+                FileChannel channel2 = in2.getChannel()) {
             assertTrue(FileChannels.contentEquals(channel1, channel1, bufferSize));
             assertTrue(FileChannels.contentEquals(channel2, channel2, bufferSize));
         }
@@ -161,10 +174,10 @@ public void testContentEqualsEmpty(final int bufferSize) throws IOException {
         assertFalse(isEmpty(notEmpty));
         // File checksums are different
         assertNotEquals(FileUtils.checksumCRC32(empty), FileUtils.checksumCRC32(notEmpty));
-        try (FileInputStream streamEmpty = new FileInputStream(empty);
-                FileInputStream streamNotEmpty = new FileInputStream(notEmpty);
-                FileChannel channelEmpty = streamEmpty.getChannel();
-                FileChannel channelNotEmpty = streamNotEmpty.getChannel()) {
+        try (FileInputStream inEmpty = new FileInputStream(empty);
+                FileInputStream inNotEmpty = new FileInputStream(notEmpty);
+                FileChannel channelEmpty = inEmpty.getChannel();
+                FileChannel channelNotEmpty = inNotEmpty.getChannel()) {
             assertFalse(FileChannels.contentEquals(channelEmpty, channelNotEmpty, bufferSize));
             assertFalse(FileChannels.contentEquals(null, channelNotEmpty, bufferSize));
             assertFalse(FileChannels.contentEquals(channelNotEmpty, null, bufferSize));