diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index 08aac4d98..0de963cda 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -18,14 +18,7 @@ package org.apache.tools.ant.util; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.Reader; -import java.io.UnsupportedEncodingException; -import java.io.Writer; +import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.text.DecimalFormat; @@ -1239,6 +1232,36 @@ public class FileUtils { return UNIX_FILE_TIMESTAMP_GRANULARITY; } + /** + * test whether a file or directory exists, with an error in the upper/lower case spelling of the name. + * Using this method is only interesting on case insensitive file systems (Windows).
+ * It will return true only if 3 conditions are met : + *
+ * + *
+ * the purpose is to identify files or directories on Windows environments whose case is not what is expected.
+ * Possibly to rename them afterwards to the desired upper/lowercase combination. + *
+ * @param localFile file to test + * @return true if the file exists and the case of the actual file is not the case of the parameter + */ + public boolean hasErrorInCase(File localFile) { + if (!localFile.exists()) { + return false; + } + final String localFileName = localFile.getName(); + FilenameFilter ff = new FilenameFilter () { + public boolean accept(File dir, String name) { + return name.equalsIgnoreCase(localFileName) && (!name.equals(localFileName)); + } + }; + String[] names = localFile.getParentFile().list(ff); + return names!=null && names.length==1; + } /** * Returns true if the source is older than the dest. * If the dest file does not exist, then the test returns false; it is diff --git a/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java index 6e5ad3b1a..38f7becd2 100644 --- a/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java @@ -544,6 +544,20 @@ public class FileUtilsTest extends TestCase { !FILE_UTILS.isUpToDate(firstTime,-1L)); } + public void testHasErrorInCase() { + File tempFolder = new File(System.getProperty("java.io.tmpdir")); + File wellcased = FILE_UTILS.createTempFile("alpha", "beta", tempFolder); + String s = wellcased.getName().toUpperCase(); + File wrongcased = new File(tempFolder, s); + if (Os.isFamily("dos")) { + assertTrue(FILE_UTILS.hasErrorInCase(wrongcased)); + assertFalse(FILE_UTILS.hasErrorInCase(wellcased)); + } else { + assertFalse(FILE_UTILS.hasErrorInCase(wrongcased)); + assertFalse(FILE_UTILS.hasErrorInCase(wellcased)); + } + + } public void testGetDefaultEncoding() { // This just tests that the function does not blow up FILE_UTILS.getDefaultEncoding();