Skip to content

Commit 283e53e

Browse files
committed
Add test for FileChannels.contentEquals() #509
- Fix FileChannels.contentEquals()
1 parent 0079663 commit 283e53e

3 files changed

Lines changed: 116 additions & 2 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ The <action> type attribute can be add,update,fix,remove.
5050
<release version="2.15.1" date="20YY-MM-DD" description="Java 8 is required.">
5151
<!-- FIX -->
5252
<action dev="sebb" type="fix" due-to="Gregor Dschung">Fix wrong issue id in changelog #503.</action>
53+
<action dev="ggregory" type="fix" due-to="Stephan Markwalder, Gary Gregory">Add test for FileChannels.contentEquals() #509.</action>
54+
<action dev="ggregory" type="fix" due-to="Gary Gregory">Fix FileChannels.contentEquals().</action>
5355
</release>
5456
<release version="2.15.0" date="2023-10-21" description="Java 8 is required.">
5557
<!-- FIX -->

src/main/java/org/apache/commons/io/channels/FileChannels.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public static boolean contentEquals(final FileChannel channel1, final FileChanne
6060
while (true) {
6161
final int read1 = channel1.read(byteBuffer1);
6262
final int read2 = channel2.read(byteBuffer2);
63+
byteBuffer1.clear();
64+
byteBuffer2.clear();
6365
if (read1 == IOUtils.EOF && read2 == IOUtils.EOF) {
6466
return byteBuffer1.equals(byteBuffer2);
6567
}
@@ -69,8 +71,6 @@ public static boolean contentEquals(final FileChannel channel1, final FileChanne
6971
if (!byteBuffer1.equals(byteBuffer2)) {
7072
return false;
7173
}
72-
byteBuffer1.clear();
73-
byteBuffer2.clear();
7474
}
7575
}
7676

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.channels;
18+
19+
import static java.nio.charset.StandardCharsets.US_ASCII;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
23+
24+
import java.io.File;
25+
import java.io.FileInputStream;
26+
import java.io.IOException;
27+
import java.nio.channels.FileChannel;
28+
29+
import org.apache.commons.io.FileUtils;
30+
import org.apache.commons.io.file.AbstractTempDirTest;
31+
import org.apache.commons.lang3.StringUtils;
32+
import org.junit.jupiter.api.Test;
33+
34+
/**
35+
* Tests {@link FileChannels}.
36+
*/
37+
public class FileChannelsTest extends AbstractTempDirTest {
38+
39+
private static final int BUFFER_SIZE = 1024;
40+
private static final String CONTENT = StringUtils.repeat("x", BUFFER_SIZE);
41+
42+
private boolean isEmpty(final File empty) {
43+
return empty.length() == 0;
44+
}
45+
46+
private void testContentEquals(final String content1, final String content2) throws IOException {
47+
assertTrue(FileChannels.contentEquals(null, null, BUFFER_SIZE));
48+
49+
// Prepare test files with same size but different content
50+
// (first 3 bytes are different, followed by a large amount of equal content)
51+
final File file1 = new File(tempDirFile, "test1.txt");
52+
final File file2 = new File(tempDirFile, "test2.txt");
53+
FileUtils.writeStringToFile(file1, content1, US_ASCII);
54+
FileUtils.writeStringToFile(file2, content2, US_ASCII);
55+
56+
// File checksums are different
57+
assertNotEquals(FileUtils.checksumCRC32(file1), FileUtils.checksumCRC32(file2));
58+
59+
try (FileInputStream stream1 = new FileInputStream(file1);
60+
FileInputStream stream2 = new FileInputStream(file2);
61+
FileChannel channel1 = stream1.getChannel();
62+
FileChannel channel2 = stream2.getChannel()) {
63+
assertFalse(FileChannels.contentEquals(channel1, channel2, BUFFER_SIZE));
64+
}
65+
try (FileInputStream stream1 = new FileInputStream(file1);
66+
FileInputStream stream2 = new FileInputStream(file2);
67+
FileChannel channel1 = stream1.getChannel();
68+
FileChannel channel2 = stream2.getChannel()) {
69+
assertTrue(FileChannels.contentEquals(channel1, channel1, BUFFER_SIZE));
70+
assertTrue(FileChannels.contentEquals(channel2, channel2, BUFFER_SIZE));
71+
}
72+
}
73+
74+
@Test
75+
public void testContentEqualsDifferentPostfix() throws IOException {
76+
testContentEquals(CONTENT + "ABC", CONTENT + "XYZ");
77+
}
78+
79+
@Test
80+
public void testContentEqualsDifferentPrefix() throws IOException {
81+
testContentEquals("ABC" + CONTENT, "XYZ" + CONTENT);
82+
}
83+
84+
@Test
85+
public void testContentEqualsEmpty() throws IOException {
86+
assertTrue(FileChannels.contentEquals(null, null, BUFFER_SIZE));
87+
88+
final File empty = new File(tempDirFile, "empty.txt");
89+
final File notEmpty = new File(tempDirFile, "not-empty.txt");
90+
FileUtils.writeStringToFile(empty, StringUtils.EMPTY, US_ASCII);
91+
FileUtils.writeStringToFile(notEmpty, "X", US_ASCII);
92+
assertTrue(isEmpty(empty));
93+
assertFalse(isEmpty(notEmpty));
94+
95+
// File checksums are different
96+
assertNotEquals(FileUtils.checksumCRC32(empty), FileUtils.checksumCRC32(notEmpty));
97+
98+
try (FileInputStream streamEmpty = new FileInputStream(empty);
99+
FileInputStream streamNotEmpty = new FileInputStream(notEmpty);
100+
FileChannel channelEmpty = streamEmpty.getChannel();
101+
FileChannel channelNotEmpty = streamNotEmpty.getChannel()) {
102+
assertFalse(FileChannels.contentEquals(channelEmpty, channelNotEmpty, BUFFER_SIZE));
103+
assertFalse(FileChannels.contentEquals(null, channelNotEmpty, BUFFER_SIZE));
104+
assertFalse(FileChannels.contentEquals(channelNotEmpty, null, BUFFER_SIZE));
105+
assertTrue(FileChannels.contentEquals(channelEmpty, channelEmpty, BUFFER_SIZE));
106+
assertTrue(FileChannels.contentEquals(null, channelEmpty, BUFFER_SIZE));
107+
assertTrue(FileChannels.contentEquals(channelEmpty, null, BUFFER_SIZE));
108+
assertTrue(FileChannels.contentEquals(channelNotEmpty, channelNotEmpty, BUFFER_SIZE));
109+
}
110+
}
111+
112+
}

0 commit comments

Comments
 (0)