diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java b/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java index 838bbfbbd69b2ff212062d27901655932519bb65..2e982ad485dfbe4fd76a5fdce0fe954d71e00f96 100644 --- a/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java +++ b/common/source/java/ch/systemsx/cisd/common/utilities/FileUtilities.java @@ -422,25 +422,29 @@ public final class FileUtilities /** A <i>Java</i> pattern matching one or more digits. */ private final static Pattern ONE_OR_MORE_DIGITS = Pattern.compile("(\\d+)"); - public final static File createNextNumberedFile(File path, String defaultFileName) + public final static File createNextNumberedFile(File path, Pattern regex) { - return createNextNumberedFile(path, null, defaultFileName); + return createNextNumberedFile(path, regex, null); } /** - * Creates the next numbered file. + * Creates the next numbered file if given <var>path</var> does already exist. * <p> * If the new suggested file already exists, then this method is called recursively. * </p> * - * @param defaultFileName the default name for the new file if the digit pattern could not be found (probably the - * starting file). - * @param regex pattern to find out the counter. If <code>null</code> then <code>"(\\d+)"</code> will be taken. - * The given <var>regex</var> must contain <code>(\\d+)</code> or <code>([0-9]+)</code>. + * @param defaultFileName the default name for the new file if the digit pattern could not be found in its name. If + * empty then "1" will be appended to its name. + * @param regex pattern to find out the counter. If <code>null</code> then {@link #ONE_OR_MORE_DIGITS} will be + * taken. The given <var>regex</var> must contain <code>(\\d+)</code> or <code>([0-9]+)</code>. */ public final static File createNextNumberedFile(File path, Pattern regex, String defaultFileName) { assert path != null; + if (path.exists() == false) + { + return path; + } final Pattern pattern; if (regex == null) { @@ -456,11 +460,15 @@ public final class FileUtilities boolean found = matcher.find(); if (found == false) { + final String fileName; if (StringUtils.isEmpty(defaultFileName) == false) { - return new File(path.getParent(), defaultFileName); + fileName = defaultFileName; + } else + { + fileName = pathName + "1"; } - return path; + return createNextNumberedFile(new File(path.getParent(), fileName), pattern, defaultFileName); } StringBuilder builder = new StringBuilder(); int nextStart = 0; diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesTest.java index 1456444b6dade7acae1f47ba4d62d402d4018315..6e4cdb575b37517721d5f12370c5ef3aade040fb 100644 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesTest.java +++ b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/FileUtilitiesTest.java @@ -32,8 +32,9 @@ import java.util.Arrays; import java.util.List; import java.util.regex.Pattern; -import org.apache.commons.io.FilenameUtils; +import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; import org.testng.annotations.Test; @@ -57,6 +58,12 @@ public class FileUtilitiesTest assert workingDirectory.isDirectory(); } + @BeforeMethod + public void beforeMethod() throws IOException + { + FileUtils.cleanDirectory(workingDirectory); + } + @Test public void testLoadToStringFile() throws Exception { @@ -129,18 +136,28 @@ public class FileUtilitiesTest } @Test - public final void testCreateNextNumberedFile() + public final void testCreateNextNumberedFile() throws IOException { File file = new File(workingDirectory, "abc_[12]"); + // File does not exist assert file.exists() == false; Pattern pattern = Pattern.compile("_\\[(\\d+)\\]"); File newFile = FileUtilities.createNextNumberedFile(file, pattern, null); - assertEquals(FilenameUtils.getName(new File(workingDirectory, "abc_[13]").getPath()), FilenameUtils - .getName(newFile.getPath())); + assertEquals(new File(workingDirectory, "abc_[12]"), newFile); + FileUtils.touch(file); + assert file.exists(); + // File exists + newFile = FileUtilities.createNextNumberedFile(file, pattern, null); + assertEquals(new File(workingDirectory, "abc_[13]"), newFile); + // File not containing a number in it file = new File(workingDirectory, "abc"); + assert file.exists() == false; + newFile = FileUtilities.createNextNumberedFile(file, pattern, null); + assertEquals(new File(workingDirectory, "abc"), newFile); + FileUtils.touch(file); + assert file.exists(); newFile = FileUtilities.createNextNumberedFile(file, pattern, null); - assertEquals(FilenameUtils.getName(new File(workingDirectory, "abc").getPath()), FilenameUtils.getName(newFile - .getPath())); + assertEquals(new File(workingDirectory, "abc1"), newFile); try { FileUtilities.createNextNumberedFile(null, pattern, null); @@ -149,25 +166,41 @@ public class FileUtilitiesTest { // Nothing to do here } + String defaultFileName = "abc_[1]"; try { - FileUtilities.createNextNumberedFile(file, Pattern.compile("dummyPattern"), "abc_[1]"); + FileUtilities.createNextNumberedFile(file, Pattern.compile("dummyPattern"), defaultFileName); fail("Must contain either '(\\d+)' or ([0-9]+)."); } catch (AssertionError e) { // Nothing to do here } - newFile = FileUtilities.createNextNumberedFile(file, pattern, "abc_[1]"); - assertEquals(FilenameUtils.getName(new File(workingDirectory, "abc_[1]").getPath()), FilenameUtils - .getName(newFile.getPath())); + // File already exists but default one does not + assertEquals(file.getName(), "abc"); + assert file.exists(); + newFile = FileUtilities.createNextNumberedFile(file, pattern, defaultFileName); + File defaultFile = new File(workingDirectory, defaultFileName); + assertEquals(defaultFile, newFile); + // File already exists and default one also + FileUtils.touch(defaultFile); + assert defaultFile.exists(); + newFile = FileUtilities.createNextNumberedFile(file, pattern, defaultFileName); + assertEquals(new File(workingDirectory, "abc_[2]"), newFile); + // With no pattern (using the default one) file = new File(workingDirectory, "a0bc1"); + FileUtils.touch(file); + assert file.exists(); newFile = FileUtilities.createNextNumberedFile(file, null); - assertEquals(FilenameUtils.getName(new File(workingDirectory, "a1bc2").getPath()), FilenameUtils - .getName(newFile.getPath())); + assertEquals(new File(workingDirectory, "a1bc2"), newFile); + // More examples file = new File(workingDirectory, "12abc_[12]"); newFile = FileUtilities.createNextNumberedFile(file, pattern, "12abc_[1]"); - assertEquals(FilenameUtils.getName(new File(workingDirectory, "12abc_[13]").getPath()), FilenameUtils - .getName(newFile.getPath())); + assertEquals(new File(workingDirectory, "12abc_[12]"), newFile); + FileUtils.touch(file); + newFile = FileUtilities.createNextNumberedFile(file, pattern, "12abc_[1]"); + assertEquals(new File(workingDirectory, "12abc_[13]"), newFile); + newFile = FileUtilities.createNextNumberedFile(file, Pattern.compile("xxx(\\d+)xxx"), "12abc_[1]"); + assertEquals(new File(workingDirectory, "12abc_[1]"), newFile); } @Test