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.assertNotNull; 21 import static org.junit.jupiter.api.Assertions.assertNull; 22 import static org.junit.jupiter.api.Assertions.assertSame; 23 import static org.junit.jupiter.api.Assertions.assertTrue; 24 25 import java.io.File; 26 import java.util.ArrayList; 27 import java.util.Comparator; 28 import java.util.List; 29 30 import org.junit.jupiter.api.Test; 31 import org.junit.jupiter.api.io.TempDir; 32 33 /** 34 * Base Test case for Comparator implementations. 35 */ 36 public abstract class ComparatorAbstractTest { 37 38 @TempDir 39 public File dir; 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 /** 60 * Test the comparator. 61 */ 62 @Test testComparator()63 public void testComparator() { 64 assertEquals(0, comparator.compare(equalFile1, equalFile2), "equal"); 65 assertTrue(comparator.compare(lessFile, moreFile) < 0, "less"); 66 assertTrue(comparator.compare(moreFile, lessFile) > 0, "more"); 67 } 68 69 /** 70 * Test the comparator reversed. 71 */ 72 @Test testReverseComparator()73 public void testReverseComparator() { 74 assertEquals(0, reverse.compare(equalFile1, equalFile2), "equal"); 75 assertTrue(reverse.compare(moreFile, lessFile) < 0, "less"); 76 assertTrue(reverse.compare(lessFile, moreFile) > 0, "more"); 77 } 78 79 /** 80 * Test the comparator array sort. 81 */ 82 @Test testSortArray()83 public void testSortArray() { 84 final File[] files = new File[3]; 85 files[0] = equalFile1; 86 files[1] = moreFile; 87 files[2] = lessFile; 88 comparator.sort(files); 89 assertSame(lessFile, files[0], "equal"); 90 assertSame(equalFile1, files[1], "less"); 91 assertSame(moreFile, files[2], "more"); 92 } 93 94 /** 95 * Test comparator array sort is null safe. 96 */ 97 @Test testSortArrayNull()98 public void testSortArrayNull() { 99 assertNull(comparator.sort((File[])null)); 100 } 101 102 /** 103 * Test the comparator array sort. 104 */ 105 @Test testSortList()106 public void testSortList() { 107 final List<File> files = new ArrayList<>(); 108 files.add(equalFile1); 109 files.add(moreFile); 110 files.add(lessFile); 111 comparator.sort(files); 112 assertSame(lessFile, files.get(0), "equal"); 113 assertSame(equalFile1, files.get(1), "less"); 114 assertSame(moreFile, files.get(2), "more"); 115 } 116 117 /** 118 * Test comparator list sort is null safe. 119 */ 120 @Test testSortListNull()121 public void testSortListNull() { 122 assertNull(comparator.sort((List<File>)null)); 123 } 124 125 /** 126 * Test comparator toString. 127 */ 128 @Test testToString()129 public void testToString() { 130 assertNotNull(comparator.toString(), "comparator"); 131 assertTrue(reverse.toString().startsWith("ReverseFileComparator["), "reverse"); 132 } 133 } 134