ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anto...@apache.org
Subject svn commit: r523681 - in /ant/core/trunk/src: main/org/apache/tools/ant/util/FileUtils.java tests/junit/org/apache/tools/ant/util/FileUtilsTest.java
Date Thu, 29 Mar 2007 12:15:26 GMT
Author: antoine
Date: Thu Mar 29 05:15:21 2007
New Revision: 523681

URL: http://svn.apache.org/viewvc?view=rev&rev=523681
Log:
adding a method to find wrong cased files

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/util/FileUtils.java
    ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/util/FileUtils.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/util/FileUtils.java?view=diff&rev=523681&r1=523680&r2=523681
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/FileUtils.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/FileUtils.java Thu Mar 29 05:15:21 2007
@@ -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 @@
         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).<br/>
+     * It will return true only if 3 conditions are met :
+     * <br/>
+     * <ul>
+     *   <li>operating system is case insensitive</li>
+     *   <li>file exists</li>
+     *   <li>actual name from directory reading is different from the supplied argument</li>
+     * </ul>
+     *  <br/>
+     * the purpose is to identify files or directories on Windows environments whose case
is not what is expected.<br/>
+     * Possibly to rename them afterwards to the desired upper/lowercase combination.
+     * <br/>
+     * @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

Modified: ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java?view=diff&rev=523681&r1=523680&r2=523681
==============================================================================
--- ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java (original)
+++ ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java Thu Mar 29
05:15:21 2007
@@ -544,6 +544,20 @@
                 !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();



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message