FileSortUtils { public void performSortOperation(int option, List<File> pdf) { switch (option) { case DATE_INDEX: sortFilesByDateNewestToOldest(pdf); break; case NAME_INDEX: sortByNameAlphabetical(pdf); break; case SIZE_INCREASING_ORDER_INDEX: sortFilesBySizeIncreasingOrder(pdf); break; case SIZE_DECREASING_ORDER_INDEX: sortFilesBySizeDecreasingOrder(pdf); break; } } private  FileSortUtils(); void performSortOperation(int option, List<File> pdf); static FileSortUtils getInstance(); final int NAME_INDEX; final int DATE_INDEX; final int SIZE_INCREASING_ORDER_INDEX; final int SIZE_DECREASING_ORDER_INDEX; }
@Test public void shouldReturnArraySortedNewestToOldest() { long base = mFiles.get(0).lastModified(); long[] edittimes = {base - 10000, base - 10, base + 10000, base + 10, base + 100, base + 5}; for (int i = 0; i < edittimes.length; i++) when(mFiles.get(i).lastModified()).thenReturn(edittimes[i]); File[] expected = new File[]{mFiles.get(2), mFiles.get(4), mFiles.get(3), mFiles.get(5), mFiles.get(1), mFiles.get(0)}; mInstance.performSortOperation(mInstance.DATE_INDEX, mFiles); Assert.assertEquals(asList(expected), mFiles); }
@Test public void shouldReturnArraySortedAlphabetically() throws IOException { List<String> paths = getPaths(); mFiles = new ArrayList<>(paths.size()); for (String item : paths) { File f = new File(item); f.createNewFile(); mFiles.add(f); } File[] expected = new File[]{mFiles.get(5), mFiles.get(3), mFiles.get(4), mFiles.get(0), mFiles.get(1), mFiles.get(2)}; mInstance.performSortOperation(mInstance.NAME_INDEX, mFiles); Assert.assertEquals(asList(expected), mFiles); }
@Test public void shouldReturnArraySortedByAscendingSize() { long[] sizes = {10000, 1000, 100, 50, 2000, 2500}; for (int i = 0; i < sizes.length; i++) when(mFiles.get(i).length()).thenReturn(sizes[i]); File[] expected = new File[]{mFiles.get(3), mFiles.get(2), mFiles.get(1), mFiles.get(4), mFiles.get(5), mFiles.get(0)}; mInstance.performSortOperation(mInstance.SIZE_INCREASING_ORDER_INDEX, mFiles); Assert.assertEquals(asList(expected), mFiles); }
@Test public void shouldReturnArraySortedByDescendingSize() { long[] sizes = {10000, 1000, 100, 50, 2000, 2500}; for (int i = 0; i < sizes.length; i++) when(mFiles.get(i).length()).thenReturn(sizes[i]); File[] expected = new File[]{mFiles.get(0), mFiles.get(5), mFiles.get(4), mFiles.get(1), mFiles.get(2), mFiles.get(3)}; mInstance.performSortOperation(mInstance.SIZE_DECREASING_ORDER_INDEX, mFiles); Assert.assertEquals(asList(expected), mFiles); }
@Test public void shouldThrowOnInvalidSortOption() { List<Integer> invalidOptions = asList(-1, 14, 65535, 8); for (Integer item : invalidOptions) { try { mInstance.performSortOperation(item, mFiles); } catch (IllegalArgumentException ex) { Assert.assertTrue(ex.getMessage().startsWith("Invalid sort option")); } } }