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  import org.apache.commons.io.FileUtils;
25  import org.apache.commons.io.testtools.FileBasedTestCase;
26  
27  /**
28   * Base Test case for Comparator implementations.
29   */
30  public abstract class ComparatorAbstractTestCase extends FileBasedTestCase {
31  
32      /** comparator instance */
33      protected AbstractFileComparator comparator;
34  
35      /** reverse comparator instance */
36      protected Comparator<File> reverse;
37  
38      /** File which compares equal to  "equalFile2" */
39      protected File equalFile1;
40  
41      /** File which compares equal to  "equalFile1" */
42      protected File equalFile2;
43  
44      /** File which is less than the "moreFile" */
45      protected File lessFile;
46  
47      /** File which is more than the "lessFile" */
48      protected File moreFile;
49  
50      /**
51       * Construct a new test case with the specified name
52       * @param name Name of the test
53       */
54      public ComparatorAbstractTestCase(final String name) {
55          super(name);
56      }
57  
58      /** @see junit.framework.TestCase#setUp() */
59      @Override
60      protected void setUp() throws Exception {
61          comparator = (AbstractFileComparator) DefaultFileComparator.DEFAULT_COMPARATOR;
62          reverse = DefaultFileComparator.DEFAULT_REVERSE;
63      }
64  
65      /** @see junit.framework.TestCase#tearDown() */
66      @Override
67      protected void tearDown() throws Exception {
68          comparator = null;
69          reverse = null;
70          equalFile1 = null;
71          equalFile2 = null;
72          lessFile = null;
73          moreFile = null;
74          FileUtils.deleteDirectory(getTestDirectory());
75      }
76  
77      /**
78       * Test the comparator.
79       */
80      public void testComparator() {
81          assertEquals("equal", 0, comparator.compare(equalFile1, equalFile2));
82          assertTrue("less",  comparator.compare(lessFile, moreFile) < 0);
83          assertTrue("more",  comparator.compare(moreFile, lessFile) > 0);
84      }
85  
86      /**
87       * Test the comparator reversed.
88       */
89      public void testReverseComparator() {
90          assertEquals("equal", 0, reverse.compare(equalFile1, equalFile2));
91          assertTrue("less",  reverse.compare(moreFile, lessFile) < 0);
92          assertTrue("more",  reverse.compare(lessFile, moreFile) > 0);
93      }
94  
95      /**
96       * Test comparator array sort is null safe.
97       */
98      public void testSortArrayNull() {
99          assertNull(comparator.sort((File[])null));
100     }
101 
102     /**
103      * Test the comparator array sort.
104      */
105     public void testSortArray() {
106         final File[] files = new File[3];
107         files[0] = equalFile1;
108         files[1] = moreFile;
109         files[2] = lessFile;
110         comparator.sort(files);
111         assertSame("equal", lessFile, files[0]);
112         assertSame("less",  equalFile1, files[1]);
113         assertSame("more",  moreFile, files[2]);
114     }
115 
116     /**
117      * Test the comparator array sort.
118      */
119     public void testSortList() {
120         final List<File> files = new ArrayList<File>();
121         files.add(equalFile1);
122         files.add(moreFile);
123         files.add(lessFile);
124         comparator.sort(files);
125         assertSame("equal", lessFile, files.get(0));
126         assertSame("less",  equalFile1, files.get(1));
127         assertSame("more",  moreFile, files.get(2));
128     }
129 
130     /**
131      * Test comparator list sort is null safe.
132      */
133     public void testSortListNull() {
134         assertNull(comparator.sort((List<File>)null));
135     }
136 
137     /**
138      * Test comparator toString.
139      */
140     public void testToString() {
141         assertNotNull("comparator", comparator.toString());
142         assertTrue("reverse", reverse.toString().startsWith("ReverseComparator["));
143     }
144 }