• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 package org.apache.commons.io.file;
19 
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23 
24 import java.io.File;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.io.TempDir;
32 
33 public class PathUtilsContentEqualsTest {
34 
35     @TempDir
36     public File temporaryFolder;
37 
getName()38     private String getName() {
39         return this.getClass().getSimpleName();
40     }
41 
42     @Test
testDirectoryAndFileContentEquals()43     public void testDirectoryAndFileContentEquals() throws Exception {
44         // Non-existent files
45         final Path path1 = new File(temporaryFolder, getName()).toPath();
46         final Path path2 = new File(temporaryFolder, getName() + "2").toPath();
47         assertTrue(PathUtils.directoryAndFileContentEquals(null, null));
48         assertFalse(PathUtils.directoryAndFileContentEquals(null, path1));
49         assertFalse(PathUtils.directoryAndFileContentEquals(path1, null));
50         // both don't exist
51         assertTrue(PathUtils.directoryAndFileContentEquals(path1, path1));
52         assertTrue(PathUtils.directoryAndFileContentEquals(path1, path2));
53         assertTrue(PathUtils.directoryAndFileContentEquals(path2, path2));
54         assertTrue(PathUtils.directoryAndFileContentEquals(path2, path1));
55         // Tree equals true tests
56         {
57             // Trees of files only that contain the same files.
58             final Path dir1 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-files-only/directory-files-only1");
59             final Path dir2 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-files-only/directory-files-only2");
60             assertTrue(PathUtils.directoryAndFileContentEquals(dir1, dir2));
61             assertTrue(PathUtils.directoryAndFileContentEquals(dir2, dir2));
62             assertTrue(PathUtils.directoryAndFileContentEquals(dir1, dir1));
63             assertTrue(PathUtils.directoryAndFileContentEquals(dir2, dir2));
64         }
65         {
66             // Trees of directories containing other directories.
67             final Path dir1 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-then-files/dir1");
68             final Path dir2 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-then-files/dir2");
69             assertTrue(PathUtils.directoryAndFileContentEquals(dir1, dir2));
70             assertTrue(PathUtils.directoryAndFileContentEquals(dir2, dir2));
71             assertTrue(PathUtils.directoryAndFileContentEquals(dir1, dir1));
72             assertTrue(PathUtils.directoryAndFileContentEquals(dir2, dir2));
73         }
74         {
75             // Trees of directories containing other directories and files.
76             final Path dir1 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-and-files/dirs-and-files1");
77             final Path dir2 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-and-files/dirs-and-files1");
78             assertTrue(PathUtils.directoryAndFileContentEquals(dir1, dir2));
79             assertTrue(PathUtils.directoryAndFileContentEquals(dir2, dir2));
80             assertTrue(PathUtils.directoryAndFileContentEquals(dir1, dir1));
81             assertTrue(PathUtils.directoryAndFileContentEquals(dir2, dir2));
82         }
83         // Tree equals false tests
84         {
85             final Path dir1 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-and-files/dirs-and-files1/directory-files-only1");
86             final Path dir2 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-and-files/dirs-and-files1/");
87             assertFalse(PathUtils.directoryAndFileContentEquals(dir1, dir2));
88             assertFalse(PathUtils.directoryAndFileContentEquals(dir2, dir1));
89         }
90         {
91             final Path dir1 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-and-files");
92             final Path dir2 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-then-files");
93             assertFalse(PathUtils.directoryAndFileContentEquals(dir1, dir2));
94             assertFalse(PathUtils.directoryAndFileContentEquals(dir2, dir1));
95         }
96     }
97 
98     @Test
testDirectoryContentEquals()99     public void testDirectoryContentEquals() throws Exception {
100         // Non-existent files
101         final Path path1 = new File(temporaryFolder, getName()).toPath();
102         final Path path2 = new File(temporaryFolder, getName() + "2").toPath();
103         assertTrue(PathUtils.directoryContentEquals(null, null));
104         assertFalse(PathUtils.directoryContentEquals(null, path1));
105         assertFalse(PathUtils.directoryContentEquals(path1, null));
106         // both don't exist
107         assertTrue(PathUtils.directoryContentEquals(path1, path1));
108         assertTrue(PathUtils.directoryContentEquals(path1, path2));
109         assertTrue(PathUtils.directoryContentEquals(path2, path2));
110         assertTrue(PathUtils.directoryContentEquals(path2, path1));
111         // Tree equals true tests
112         {
113             // Trees of files only that contain the same files.
114             final Path dir1 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-files-only/directory-files-only1");
115             final Path dir2 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-files-only/directory-files-only2");
116             assertTrue(PathUtils.directoryContentEquals(dir1, dir2));
117             assertTrue(PathUtils.directoryContentEquals(dir2, dir2));
118             assertTrue(PathUtils.directoryContentEquals(dir1, dir1));
119             assertTrue(PathUtils.directoryContentEquals(dir2, dir2));
120         }
121         {
122             // Trees of directories containing other directories.
123             final Path dir1 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-then-files/dir1");
124             final Path dir2 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-then-files/dir2");
125             assertTrue(PathUtils.directoryContentEquals(dir1, dir2));
126             assertTrue(PathUtils.directoryContentEquals(dir2, dir2));
127             assertTrue(PathUtils.directoryContentEquals(dir1, dir1));
128             assertTrue(PathUtils.directoryContentEquals(dir2, dir2));
129         }
130         {
131             // Trees of directories containing other directories and files.
132             final Path dir1 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-and-files/dirs-and-files1");
133             final Path dir2 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-and-files/dirs-and-files1");
134             assertTrue(PathUtils.directoryContentEquals(dir1, dir2));
135             assertTrue(PathUtils.directoryContentEquals(dir2, dir2));
136             assertTrue(PathUtils.directoryContentEquals(dir1, dir1));
137             assertTrue(PathUtils.directoryContentEquals(dir2, dir2));
138         }
139         // Tree equals false tests
140         {
141             final Path dir1 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-and-files/dirs-and-files1/directory-files-only1");
142             final Path dir2 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-and-files/dirs-and-files1/");
143             assertFalse(PathUtils.directoryContentEquals(dir1, dir2));
144             assertFalse(PathUtils.directoryContentEquals(dir2, dir1));
145         }
146         {
147             final Path dir1 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-and-files");
148             final Path dir2 = Paths.get("src/test/resources/dir-equals-tests/dir-equals-dirs-then-files");
149             assertFalse(PathUtils.directoryContentEquals(dir1, dir2));
150             assertFalse(PathUtils.directoryContentEquals(dir2, dir1));
151         }
152     }
153 
154     @Test
testFileContentEquals()155     public void testFileContentEquals() throws Exception {
156         // Non-existent files
157         final Path path1 = new File(temporaryFolder, getName()).toPath();
158         final Path path2 = new File(temporaryFolder, getName() + "2").toPath();
159         assertTrue(PathUtils.fileContentEquals(null, null));
160         assertFalse(PathUtils.fileContentEquals(null, path1));
161         assertFalse(PathUtils.fileContentEquals(path1, null));
162         // both don't exist
163         assertTrue(PathUtils.fileContentEquals(path1, path1));
164         assertTrue(PathUtils.fileContentEquals(path1, path2));
165         assertTrue(PathUtils.fileContentEquals(path2, path2));
166         assertTrue(PathUtils.fileContentEquals(path2, path1));
167 
168         // Directories
169         assertThrows(IOException.class, () -> PathUtils.fileContentEquals(temporaryFolder.toPath(), temporaryFolder.toPath()));
170 
171         // Different files
172         final Path objFile1 = Paths.get(temporaryFolder.getAbsolutePath(), getName() + ".object");
173         PathUtils.copyFile(getClass().getResource("/java/lang/Object.class"), objFile1);
174 
175         final Path objFile1b = Paths.get(temporaryFolder.getAbsolutePath(), getName() + ".object2");
176         PathUtils.copyFile(getClass().getResource("/java/lang/Object.class"), objFile1b);
177 
178         final Path objFile2 = Paths.get(temporaryFolder.getAbsolutePath(), getName() + ".collection");
179         PathUtils.copyFile(getClass().getResource("/java/util/Collection.class"), objFile2);
180 
181         assertFalse(PathUtils.fileContentEquals(objFile1, objFile2));
182         assertFalse(PathUtils.fileContentEquals(objFile1b, objFile2));
183         assertTrue(PathUtils.fileContentEquals(objFile1, objFile1b));
184 
185         assertTrue(PathUtils.fileContentEquals(objFile1, objFile1));
186         assertTrue(PathUtils.fileContentEquals(objFile1b, objFile1b));
187         assertTrue(PathUtils.fileContentEquals(objFile2, objFile2));
188 
189         // Equal files
190         Files.createFile(path1);
191         Files.createFile(path2);
192         assertTrue(PathUtils.fileContentEquals(path1, path1));
193         assertTrue(PathUtils.fileContentEquals(path1, path2));
194     }
195 
196 }
197