Tools { public static File getFileWithIncrementedName(final File existingFile) { Validate.notNull(existingFile, "The existing file to increment the name of can not be null"); if (!existingFile.exists()) { return existingFile; } int counter = 1; File newFile; final String path; if (existingFile.getParent() == null) { path = ""; } else { path = existingFile.getParent() + File.separator; } do { final String existingName = existingFile.getName(); final String newName = String.format("%s%s_%s%s", path, getFileBaseName(existingName), counter, getFileExtension(existingName)); newFile = new File(newName); counter++; } while (newFile.exists()); return newFile; } private  Tools(); static String getTime(); static String getDoubleDigit(final int number); static String dateToString(@Nullable final Date d, final String format); static String decimalFormat(final String format, final double number); static boolean isValidNick(@Nullable final String nick); static String byteToString(final long bytes); static int getBytes(final String text); static void sleep(final long millis); @Nullable static String capitalizeFirstLetter(@Nullable final String word); @Nullable static String shorten(@Nullable final String word, final int length); @Nullable static String getFileExtension(@Nullable final String filename); @Nullable static String getFileBaseName(@Nullable final String filename); static double percent(final double fraction, final double total); static double percentOf(final double percent, final double total); static String postPadString(final String stringToPad, final int paddedLength); static boolean isEmpty(@Nullable final String string); static String appendSlash(final String path); static File getFileWithIncrementedName(final File existingFile); static String emptyIfNull(@Nullable final String string);  }
@Test public void getFileWithIncrementedNameShouldThrowExceptionIfFileIsNull() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("The existing file to increment the name of can not be null"); Tools.getFileWithIncrementedName(null); }
@Test public void getFileWithIncrementedNameShouldReturnSameFileIfNoFileWithThatNameExists() { final File nonExistingFile = new File("monkeys.jpg"); assertFalse(nonExistingFile.exists()); final File file = Tools.getFileWithIncrementedName(nonExistingFile); assertEquals("monkeys.jpg", file.getName()); assertSame(nonExistingFile, file); }
@Test public void getFileWithIncrementedNameShouldReturnFileWithOneAppendedIfFileWithOriginalNameExists() throws IOException { createTemporaryFile("monkeys.jpg"); final File file = Tools.getFileWithIncrementedName(new File("monkeys.jpg")); assertEquals("monkeys_1.jpg", file.getName()); assertNull(file.getParent()); }
@Test public void getFileWithIncrementedNameShouldReturnFileWithTwoAppendedIfFileWithOneAppendedExists() throws IOException { createTemporaryFile("bananas.jpg"); createTemporaryFile("bananas_1.jpg"); final File file = Tools.getFileWithIncrementedName(new File("bananas.jpg")); assertEquals("bananas_2.jpg", file.getName()); }
@Test public void getFileWithIncrementedNameShouldReturnFileWithFiveAppendedIfFileUpToFourAppendedExists() throws IOException { createTemporaryFile("apples.jpg"); createTemporaryFile("apples_1.jpg"); createTemporaryFile("apples_2.jpg"); createTemporaryFile("apples_3.jpg"); createTemporaryFile("apples_4.jpg"); final File file = Tools.getFileWithIncrementedName(new File("apples.jpg")); assertEquals("apples_5.jpg", file.getName()); }
@Test public void getFileWithIncrementedNameShouldUseTheSameParent() throws IOException { final String home = System.getProperty("user.home"); final String homeWithSeparator = home + File.separatorChar; createTemporaryFile(homeWithSeparator + "donkeys.jpg"); createTemporaryFile(homeWithSeparator + "donkeys_1.jpg"); final File file = Tools.getFileWithIncrementedName(new File(homeWithSeparator + "donkeys.jpg")); assertEquals("donkeys_2.jpg", file.getName()); assertEquals(home, file.getParent()); }
@Test public void getFileWithIncrementedNameShouldHandleMissingExtension() throws IOException { createTemporaryFile("STUFF"); createTemporaryFile("STUFF_1"); final File file = Tools.getFileWithIncrementedName(new File("STUFF")); assertEquals("STUFF_2", file.getName()); }
@Test public void getFileWithIncrementedNameShouldHandleSpaces() throws IOException { createTemporaryFile("this is a movie.mov"); createTemporaryFile("this is a movie_1.mov"); final File file = Tools.getFileWithIncrementedName(new File("this is a movie.mov")); assertEquals("this is a movie_2.mov", file.getName()); }
@Test public void getFileWithIncrementedNameShouldHandleDots() throws IOException { createTemporaryFile("this.is.a.song.ogg"); createTemporaryFile("this.is.a.song_1.ogg"); final File file = Tools.getFileWithIncrementedName(new File("this.is.a.song.ogg")); assertEquals("this.is.a.song_2.ogg", file.getName()); }