1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io.comparator;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertTrue;
21
22 import java.io.BufferedOutputStream;
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.file.Files;
26 import java.util.ArrayList;
27 import java.util.Comparator;
28 import java.util.List;
29
30 import org.apache.commons.io.test.TestUtils;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33
34
35
36
37 public class CompositeFileComparatorTest extends ComparatorAbstractTest {
38
39 @BeforeEach
40 public void setUp() throws Exception {
41 comparator = new CompositeFileComparator(SizeFileComparator.SIZE_COMPARATOR, ExtensionFileComparator.EXTENSION_COMPARATOR);
42 reverse = new ReverseFileComparator(comparator);
43 lessFile = new File(dir, "xyz.txt");
44 equalFile1 = new File(dir, "foo.txt");
45 equalFile2 = new File(dir, "bar.txt");
46 moreFile = new File(dir, "foo.xyz");
47 if (!lessFile.getParentFile().exists()) {
48 throw new IOException("Cannot create file " + lessFile
49 + " as the parent directory does not exist");
50 }
51
52 try (BufferedOutputStream output3 =
53 new BufferedOutputStream(Files.newOutputStream(lessFile.toPath()))) {
54 TestUtils.generateTestData(output3, 32);
55 }
56 if (!equalFile1.getParentFile().exists()) {
57 throw new IOException("Cannot create file " + equalFile1
58 + " as the parent directory does not exist");
59 }
60 try (BufferedOutputStream output2 =
61 new BufferedOutputStream(Files.newOutputStream(equalFile1.toPath()))) {
62 TestUtils.generateTestData(output2, 48);
63 }
64 if (!equalFile2.getParentFile().exists()) {
65 throw new IOException("Cannot create file " + equalFile2
66 + " as the parent directory does not exist");
67 }
68 try (BufferedOutputStream output1 =
69 new BufferedOutputStream(Files.newOutputStream(equalFile2.toPath()))) {
70 TestUtils.generateTestData(output1, 48);
71 }
72 if (!moreFile.getParentFile().exists()) {
73 throw new IOException("Cannot create file " + moreFile
74 + " as the parent directory does not exist");
75 }
76 try (BufferedOutputStream output =
77 new BufferedOutputStream(Files.newOutputStream(moreFile.toPath()))) {
78 TestUtils.generateTestData(output, 48);
79 }
80 }
81
82
83
84
85 @Test
86 public void testConstructorArray_Null() {
87 final Comparator<File> c = new CompositeFileComparator((Comparator<File>[]) null);
88 assertEquals(0, c.compare(lessFile, moreFile), "less,more");
89 assertEquals(0, c.compare(moreFile, lessFile), "more,less");
90 assertEquals("CompositeFileComparator{}", c.toString(), "toString");
91 }
92
93
94
95
96 @Test
97 public void testConstructorIterable_Null() {
98 final Comparator<File> c = new CompositeFileComparator((Iterable<Comparator<File>>) null);
99 assertEquals(0, c.compare(lessFile, moreFile), "less,more");
100 assertEquals(0, c.compare(moreFile, lessFile), "more,less");
101 assertEquals("CompositeFileComparator{}", c.toString(), "toString");
102 }
103
104
105
106
107 @Test
108 public void testConstructorIterable_order() {
109 final List<Comparator<File>> list = new ArrayList<>();
110 list.add(SizeFileComparator.SIZE_COMPARATOR);
111 list.add(ExtensionFileComparator.EXTENSION_COMPARATOR);
112 final Comparator<File> c = new CompositeFileComparator(list);
113
114 assertEquals(0, c.compare(equalFile1, equalFile2), "equal");
115 assertTrue(c.compare(lessFile, moreFile) < 0, "less");
116 assertTrue(c.compare(moreFile, lessFile) > 0, "more");
117 }
118 }