View Javadoc
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 }