1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
50
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
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
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 }