FileUtils { public static File[] listFilesSorted(File directory) throws AdeInternalException { final File[] fileArray = directory.listFiles(new LogFileFilter()); if (fileArray == null) { return new File[0]; } final Comparator<? super File> fileComparator = new Comparator<File>() { @Override public int compare(File file1, File file2) { return file1.getName().compareToIgnoreCase(file2.getName()); } }; Arrays.sort(fileArray, fileComparator); return fileArray; } private  FileUtils(); static void createDir(File target); static void createDirs(File target); static File[] listFilesSorted(File directory); static PrintWriter openPrintWriterToFile(File file, boolean verbose); static void assertExists(File... files);  }
@Test public void testListFilesSortedReturnsFileNamesSorted() throws Exception { tempFolder.newFile("a"); tempFolder.newFile("b"); tempFolder.newFile("c"); File[] files = FileUtils.listFilesSorted(tempFolder.getRoot()); assertArrayEquals("Assert filename in alpha order", new String[] { "a", "b", "c" }, new String[] { files[0].getName(), files[1].getName(), files[2].getName() }); }
@Test public void testListFilesSortedIgnoresDotFiles() throws Exception { tempFolder.newFile("a"); tempFolder.newFile(".b"); File[] files = FileUtils.listFilesSorted(tempFolder.getRoot()); assertEquals("Validate only 1 file returned", 1, files.length); assertEquals("Validate correct file returned", "a", files[0].getName()); }
@Test public void testListFilesSortedIgnoresDirs() throws Exception { tempFolder.newFile("a"); tempFolder.newFolder("b"); tempFolder.newFile("c"); File[] files = FileUtils.listFilesSorted(tempFolder.getRoot()); assertEquals("Validate 2 files returned", 2, files.length); assertArrayEquals("Validate correct files returned", new String[] { "a", "c" }, new String[] { files[0].getName(), files[1].getName() }); }
@Test public void testListFilesSortedReturnsEmptyArrayForNonDirectoryInput() throws Exception { File[] files = FileUtils.listFilesSorted(tempFolder.newFile()); assertNotNull("Assert returned not null", files); assertEquals("Assert array is empty", 0, files.length); }