Browse Source

adding a method to find wrong cased files

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@523681 13f79535-47bb-0310-9956-ffa450edef68
master
Antoine Levy-Lambert 18 years ago
parent
commit
1e4c39fb60
2 changed files with 45 additions and 8 deletions
  1. +31
    -8
      src/main/org/apache/tools/ant/util/FileUtils.java
  2. +14
    -0
      src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java

+ 31
- 8
src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -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).<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


+ 14
- 0
src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java View File

@@ -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();


Loading…
Cancel
Save