PosixFileNameComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { String o1WithoutDot = o1; String o2WithoutDot = o2; if (o1.indexOf(AeshConstants.DOT) == 0) { o1WithoutDot = o1.substring(1); } if (o2.indexOf(AeshConstants.DOT) == 0) { o2WithoutDot = o2.substring(1); } if (o1WithoutDot.compareTo(o2WithoutDot) == 0) { return o2.compareTo(o1); } else if (o1WithoutDot.compareToIgnoreCase(o2WithoutDot) == 0) { return o2WithoutDot.compareTo(o1WithoutDot); } else { return o1WithoutDot.compareToIgnoreCase(o2WithoutDot); } }  @Override int compare(String o1, String o2);  }
@Test public void testSame() { String s1 = "abcde"; String s2 = "abcde"; assertTrue(s1 + " should equals " + s2, posixComparator.compare(s1, s2) == 0); }
@Test public void testDifferentNames() { String s1 = "Abcde"; String s2 = "Bbcde"; assertTrue(s1 + " should be before " + s2, posixComparator.compare(s1, s2) < 0); String s3 = "abcde"; String s4 = "Bbcde"; assertTrue(s3 + " should be before " + s4, posixComparator.compare(s3, s4) < 0); String s5 = "bbcde"; String s6 = "Abcde"; assertTrue(s5 + " should be after " + s6, posixComparator.compare(s5, s6) > 0); }
@Test public void testIgnoreCasesDifferentLength() { String s1 = "abcde"; String s2 = "Abc"; assertTrue(s1 + " should be after " + s2, posixComparator.compare(s1, s2) > 0); String s3 = "Abcde"; String s4 = "abc"; assertTrue(s3 + " should be after " + s4, posixComparator.compare(s3, s4) > 0); }
@Test public void testIgnoreDotsDifferent() { String s1 = ".Abcde"; String s2 = "Bbcde"; assertTrue(s1 + " should be before " + s2, posixComparator.compare(s1, s2) < 0); String s3 = "Abcde"; String s4 = ".Bbcde"; assertTrue(s3 + " should be before " + s4, posixComparator.compare(s3, s4) < 0); }
@Test public void testLowerCaseBeforeUpperCases() { String s1 = "abcde"; String s2 = "Abcde"; assertTrue(s1 + " should be before " + s2, posixComparator.compare(s1, s2) < 0); String s3 = "AbCde"; String s4 = "Abcde"; assertTrue(s3 + " should be after " + s4, posixComparator.compare(s3, s4) > 0); }
@Test public void testIgnoreDotsSameName() { String s1 = ".abcde"; String s2 = "abcde"; assertTrue(s1 + " should be after " + s2, posixComparator.compare(s1, s2) > 0); String s3 = "abcde"; String s4 = ".abcde"; assertTrue(s3 + " should be before " + s4, posixComparator.compare(s3, s4) < 0); }