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