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


The following commit(s) were added to refs/heads/master by this push:
     new 283e53e5 Add test for FileChannels.contentEquals() #509
283e53e5 is described below

commit 283e53e54b9ab23c888f9e502c7e504bc6c58653
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Nov 8 08:32:22 2023 -0500

    Add test for FileChannels.contentEquals() #509
    
    - Fix FileChannels.contentEquals()
---
 src/changes/changes.xml                            |   2 +
 .../apache/commons/io/channels/FileChannels.java   |   4 +-
 .../commons/io/channels/FileChannelsTest.java      | 112 +++++++++++++++++++++
 3 files changed, 116 insertions(+), 2 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ad0ee4c9..1d5e1bf8 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -50,6 +50,8 @@ The <action> type attribute can be add,update,fix,remove.
     <release version="2.15.1" date="20YY-MM-DD" description="Java 8 is 
required.">
       <!-- FIX -->
       <action dev="sebb" type="fix" due-to="Gregor Dschung">Fix wrong issue id 
in changelog #503.</action>
+      <action dev="ggregory" type="fix" due-to="Stephan Markwalder, Gary 
Gregory">Add test for FileChannels.contentEquals() #509.</action>
+      <action dev="ggregory" type="fix" due-to="Gary Gregory">Fix 
FileChannels.contentEquals().</action>
     </release>
     <release version="2.15.0" date="2023-10-21" description="Java 8 is 
required.">
       <!-- FIX -->
diff --git a/src/main/java/org/apache/commons/io/channels/FileChannels.java 
b/src/main/java/org/apache/commons/io/channels/FileChannels.java
index 468c85b4..94116b14 100644
--- a/src/main/java/org/apache/commons/io/channels/FileChannels.java
+++ b/src/main/java/org/apache/commons/io/channels/FileChannels.java
@@ -60,6 +60,8 @@ public final class FileChannels {
         while (true) {
             final int read1 = channel1.read(byteBuffer1);
             final int read2 = channel2.read(byteBuffer2);
+            byteBuffer1.clear();
+            byteBuffer2.clear();
             if (read1 == IOUtils.EOF && read2 == IOUtils.EOF) {
                 return byteBuffer1.equals(byteBuffer2);
             }
@@ -69,8 +71,6 @@ public final class FileChannels {
             if (!byteBuffer1.equals(byteBuffer2)) {
                 return false;
             }
-            byteBuffer1.clear();
-            byteBuffer2.clear();
         }
     }
 
diff --git a/src/test/java/org/apache/commons/io/channels/FileChannelsTest.java 
b/src/test/java/org/apache/commons/io/channels/FileChannelsTest.java
new file mode 100644
index 00000000..7a3b260a
--- /dev/null
+++ b/src/test/java/org/apache/commons/io/channels/FileChannelsTest.java
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.channels;
+
+import static java.nio.charset.StandardCharsets.US_ASCII;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.channels.FileChannel;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.file.AbstractTempDirTest;
+import org.apache.commons.lang3.StringUtils;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link FileChannels}.
+ */
+public class FileChannelsTest extends AbstractTempDirTest {
+
+    private static final int BUFFER_SIZE = 1024;
+    private static final String CONTENT = StringUtils.repeat("x", BUFFER_SIZE);
+
+    private boolean isEmpty(final File empty) {
+        return empty.length() == 0;
+    }
+
+    private void testContentEquals(final String content1, final String 
content2) throws IOException {
+        assertTrue(FileChannels.contentEquals(null, null, BUFFER_SIZE));
+
+        // Prepare test files with same size but different content
+        // (first 3 bytes are different, followed by a large amount of equal 
content)
+        final File file1 = new File(tempDirFile, "test1.txt");
+        final File file2 = new File(tempDirFile, "test2.txt");
+        FileUtils.writeStringToFile(file1, content1, US_ASCII);
+        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()) {
+            assertFalse(FileChannels.contentEquals(channel1, channel2, 
BUFFER_SIZE));
+        }
+        try (FileInputStream stream1 = new FileInputStream(file1);
+                FileInputStream stream2 = new FileInputStream(file2);
+                FileChannel channel1 = stream1.getChannel();
+                FileChannel channel2 = stream2.getChannel()) {
+            assertTrue(FileChannels.contentEquals(channel1, channel1, 
BUFFER_SIZE));
+            assertTrue(FileChannels.contentEquals(channel2, channel2, 
BUFFER_SIZE));
+        }
+    }
+
+    @Test
+    public void testContentEqualsDifferentPostfix() throws IOException {
+        testContentEquals(CONTENT + "ABC", CONTENT + "XYZ");
+    }
+
+    @Test
+    public void testContentEqualsDifferentPrefix() throws IOException {
+        testContentEquals("ABC" + CONTENT, "XYZ" + CONTENT);
+    }
+
+    @Test
+    public void testContentEqualsEmpty() throws IOException {
+        assertTrue(FileChannels.contentEquals(null, null, BUFFER_SIZE));
+
+        final File empty = new File(tempDirFile, "empty.txt");
+        final File notEmpty = new File(tempDirFile, "not-empty.txt");
+        FileUtils.writeStringToFile(empty, StringUtils.EMPTY, US_ASCII);
+        FileUtils.writeStringToFile(notEmpty, "X", US_ASCII);
+        assertTrue(isEmpty(empty));
+        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()) {
+            assertFalse(FileChannels.contentEquals(channelEmpty, 
channelNotEmpty, BUFFER_SIZE));
+            assertFalse(FileChannels.contentEquals(null, channelNotEmpty, 
BUFFER_SIZE));
+            assertFalse(FileChannels.contentEquals(channelNotEmpty, null, 
BUFFER_SIZE));
+            assertTrue(FileChannels.contentEquals(channelEmpty, channelEmpty, 
BUFFER_SIZE));
+            assertTrue(FileChannels.contentEquals(null, channelEmpty, 
BUFFER_SIZE));
+            assertTrue(FileChannels.contentEquals(channelEmpty, null, 
BUFFER_SIZE));
+            assertTrue(FileChannels.contentEquals(channelNotEmpty, 
channelNotEmpty, BUFFER_SIZE));
+        }
+    }
+
+}

Reply via email to