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 java.io.File;
20  import java.util.ArrayList;
21  import java.util.Comparator;
22  import java.util.List;
23  
24  /**
25   * Test case for {@link CompositeFileComparator}.
26   */
27  public class CompositeFileComparatorTest extends ComparatorAbstractTestCase {
28  
29      /**
30       * Construct a new test case with the specified name.
31       *
32       * @param name Name of the test
33       */
34      public CompositeFileComparatorTest(final String name) {
35          super(name);
36      }
37  
38      /** @see junit.framework.TestCase#setUp() */
39      @Override
40      protected void setUp() throws Exception {
41          super.setUp();
42          comparator = new CompositeFileComparator(
43                  new AbstractFileComparator[] {
44                      (AbstractFileComparator) SizeFileComparator.SIZE_COMPARATOR,
45                      (AbstractFileComparator) ExtensionFileComparator.EXTENSION_COMPARATOR});
46          reverse = new ReverseComparator(comparator);
47          final File dir = getTestDirectory();
48          lessFile   = new File(dir, "xyz.txt");
49          equalFile1 = new File(dir, "foo.txt");
50          equalFile2 = new File(dir, "bar.txt");
51          moreFile   = new File(dir, "foo.xyz");
52          createFile(lessFile,   32);
53          createFile(equalFile1, 48);
54          createFile(equalFile2, 48);
55          createFile(moreFile,   48);
56      }
57  
58      /**
59       * Test Constructor with null Iterable
60       */
61      public void testConstructorIterable() {
62          final List<Comparator<File>> list = new ArrayList<Comparator<File>>();
63          list.add(SizeFileComparator.SIZE_COMPARATOR);
64          list.add(ExtensionFileComparator.EXTENSION_COMPARATOR);
65          final Comparator<File> c = new CompositeFileComparator(list);
66  
67          assertEquals("equal", 0, c.compare(equalFile1, equalFile2));
68          assertTrue("less",  c.compare(lessFile, moreFile) < 0);
69          assertTrue("more",  c.compare(moreFile, lessFile) > 0);
70      }
71  
72      /**
73       * Test Constructor with null Iterable
74       */
75      public void testConstructorIterableNull() {
76          final Comparator<File> c = new CompositeFileComparator((Iterable<Comparator<File>>)null);
77          assertEquals("less,more", 0, c.compare(lessFile, moreFile));
78          assertEquals("more,less", 0, c.compare(moreFile, lessFile));
79          assertEquals("toString", "CompositeFileComparator{}", c.toString());
80      }
81  
82      /**
83       * Test Constructor with null array
84       */
85      public void testConstructorArrayNull() {
86          final Comparator<File> c = new CompositeFileComparator((Comparator<File>[])null);
87          assertEquals("less,more", 0, c.compare(lessFile, moreFile));
88          assertEquals("more,less", 0, c.compare(moreFile, lessFile));
89          assertEquals("toString", "CompositeFileComparator{}", c.toString());
90      }
91  }