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 static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.io.File;
23  import java.util.Comparator;
24  
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Tests {@link NameFileComparator}.
30   */
31  public class NameFileComparatorTest extends ComparatorAbstractTest {
32  
33      @BeforeEach
34      public void setUp() {
35          comparator = (AbstractFileComparator) NameFileComparator.NAME_INSENSITIVE_COMPARATOR;
36          reverse = NameFileComparator.NAME_REVERSE;
37          equalFile1 = new File("a/foo.txt");
38          equalFile2 = new File("b/foo.txt");
39          lessFile   = new File("c/ABC.txt");
40          moreFile   = new File("d/XYZ.txt");
41      }
42  
43      /** Test case sensitivity */
44      @Test
45      public void testCaseSensitivity() {
46          final File file3 = new File("a/FOO.txt");
47          final Comparator<File> sensitive = new NameFileComparator(null); /* test null as well */
48          assertEquals(0, sensitive.compare(equalFile1, equalFile2), "sensitive file1 & file2 = 0");
49          assertTrue(sensitive.compare(equalFile1, file3) > 0, "sensitive file1 & file3 > 0");
50          assertTrue(sensitive.compare(equalFile1, lessFile) > 0, "sensitive file1 & less  > 0");
51  
52          final Comparator<File> insensitive = NameFileComparator.NAME_INSENSITIVE_COMPARATOR;
53          assertEquals(0, insensitive.compare(equalFile1, equalFile2), "insensitive file1 & file2 = 0");
54          assertEquals(0, insensitive.compare(equalFile1, file3), "insensitive file1 & file3 = 0");
55          assertTrue(insensitive.compare(equalFile1, lessFile) > 0, "insensitive file1 & file4 > 0");
56          assertTrue(insensitive.compare(file3, lessFile) > 0, "insensitive file3 & less  > 0");
57      }
58  }