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.comparator;
18  
19  import org.apache.commons.io.testtools.TestUtils;
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  import java.io.BufferedOutputStream;
24  import java.io.File;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.Comparator;
29  import java.util.List;
30  
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.assertTrue;
33  
34  /**
35   * Test case for {@link CompositeFileComparator}.
36   */
37  public class CompositeFileComparatorTest extends ComparatorAbstractTestCase {
38  
39      @Before
40      public void setUp() throws Exception {
41          comparator = new CompositeFileComparator(
42                  new AbstractFileComparator[] {
43                      (AbstractFileComparator) SizeFileComparator.SIZE_COMPARATOR,
44                      (AbstractFileComparator) ExtensionFileComparator.EXTENSION_COMPARATOR});
45          reverse = new ReverseComparator(comparator);
46          final File dir = getTestDirectory();
47          lessFile   = new File(dir, "xyz.txt");
48          equalFile1 = new File(dir, "foo.txt");
49          equalFile2 = new File(dir, "bar.txt");
50          moreFile   = new File(dir, "foo.xyz");
51          if (!lessFile.getParentFile().exists()) {
52              throw new IOException("Cannot create file " + lessFile
53                      + " as the parent directory does not exist");
54          }
55  
56          try (final BufferedOutputStream output3 =
57                  new BufferedOutputStream(new FileOutputStream(lessFile))) {
58              TestUtils.generateTestData(output3, (long) 32);
59          }
60          if (!equalFile1.getParentFile().exists()) {
61              throw new IOException("Cannot create file " + equalFile1
62                      + " as the parent directory does not exist");
63          }
64          try (final BufferedOutputStream output2 =
65                  new BufferedOutputStream(new FileOutputStream(equalFile1))) {
66              TestUtils.generateTestData(output2, (long) 48);
67          }
68          if (!equalFile2.getParentFile().exists()) {
69              throw new IOException("Cannot create file " + equalFile2
70                      + " as the parent directory does not exist");
71          }
72          try (final BufferedOutputStream output1 =
73                  new BufferedOutputStream(new FileOutputStream(equalFile2))) {
74              TestUtils.generateTestData(output1, (long) 48);
75          }
76          if (!moreFile.getParentFile().exists()) {
77              throw new IOException("Cannot create file " + moreFile
78                      + " as the parent directory does not exist");
79          }
80          try (final BufferedOutputStream output =
81                  new BufferedOutputStream(new FileOutputStream(moreFile))) {
82              TestUtils.generateTestData(output, (long) 48);
83          }
84      }
85  
86      /**
87       * Test Constructor with null Iterable
88       */
89      @Test
90      public void constructorIterable_order() {
91          final List<Comparator<File>> list = new ArrayList<>();
92          list.add(SizeFileComparator.SIZE_COMPARATOR);
93          list.add(ExtensionFileComparator.EXTENSION_COMPARATOR);
94          final Comparator<File> c = new CompositeFileComparator(list);
95  
96          assertEquals("equal", 0, c.compare(equalFile1, equalFile2));
97          assertTrue("less",  c.compare(lessFile, moreFile) < 0);
98          assertTrue("more",  c.compare(moreFile, lessFile) > 0);
99      }
100 
101     /**
102      * Test Constructor with null Iterable
103      */
104     @Test
105     public void constructorIterable_Null() {
106         final Comparator<File> c = new CompositeFileComparator((Iterable<Comparator<File>>)null);
107         assertEquals("less,more", 0, c.compare(lessFile, moreFile));
108         assertEquals("more,less", 0, c.compare(moreFile, lessFile));
109         assertEquals("toString", "CompositeFileComparator{}", c.toString());
110     }
111 
112     /**
113      * Test Constructor with null array
114      */
115     @Test
116     public void constructorArray_Null() {
117         final Comparator<File> c = new CompositeFileComparator((Comparator<File>[])null);
118         assertEquals("less,more", 0, c.compare(lessFile, moreFile));
119         assertEquals("more,less", 0, c.compare(moreFile, lessFile));
120         assertEquals("toString", "CompositeFileComparator{}", c.toString());
121     }
122 }