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