stevel 2004/04/22 07:03:47
Modified: src/testcases/org/apache/tools/ant/util FileUtilsTest.java
src/main/org/apache/tools/ant/util FileUtils.java
src/main/org/apache/tools/ant/taskdefs SignJar.java
src/main/org/apache/tools/ant Project.java
Log:
A new uptodate test that includes granularity. the
intent is to move all date tests to this single place, to eliminate
inconsistencies.
The only place I use it is signjar, which, by virtue of the fact there is no
signjar test, means I know it wont break the tests.
Also, cleaned up the reflection hacks for 1.1.
Revision Changes Path
1.27 +19 -21
ant/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java
Index: FileUtilsTest.java
===================================================================
RCS file:
/home/cvs/ant/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -r1.26 -r1.27
--- FileUtilsTest.java 9 Mar 2004 16:49:07 -0000 1.26
+++ FileUtilsTest.java 22 Apr 2004 14:03:46 -0000 1.27
@@ -50,6 +50,16 @@
}
}
+ /**
+ * test modification.
+ * Since Ant1.7, the method being tested no longer uses
+ * reflection to provide backwards support to Java1.1, so this
+ * test is not so critical. But it does explore file system
+ * behaviour and will help catch any regression in Java itself,
+ * so is worth retaining.
+ * @see FileUtils#setFileLastModified(java.io.File, long)
+ * @throws IOException
+ */
public void testSetLastModified() throws IOException {
removeThis = new File("dummy");
FileOutputStream fos = new FileOutputStream(removeThis);
@@ -64,20 +74,14 @@
* granularity (should be > 2s to account for Windows FAT).
*/
try {
- Thread.currentThread().sleep(5000);
+ Thread.sleep(5000);
} catch (InterruptedException ie) {
fail(ie.getMessage());
}
fu.setFileLastModified(removeThis, -1);
long secondModTime = removeThis.lastModified();
- try {
- Class.forName("java.lang.ThreadLocal");
- assertTrue(secondModTime > modTime);
- } catch (ClassNotFoundException e) {
- // JDK 1.1
- assertEquals(modTime, secondModTime);
- }
+ assertTrue(secondModTime > modTime);
// number of milliseconds in a day
@@ -87,19 +91,13 @@
// it did not work on a computer running JDK 1.4.1_02 + Windows 2000
fu.setFileLastModified(removeThis, secondModTime + millisperday);
long thirdModTime = removeThis.lastModified();
- try {
- Class.forName("java.lang.ThreadLocal");
- /*
- * I would love to compare this with 123456, but depending on
- * the filesystems granularity it can take an arbitrary value.
- *
- * Just assert the time has changed.
- */
- assertTrue(thirdModTime != secondModTime);
- } catch (ClassNotFoundException e) {
- // JDK 1.1
- assertEquals(modTime, thirdModTime);
- }
+ /*
+ * I would love to compare this with 123456, but depending on
+ * the filesystems granularity it can take an arbitrary value.
+ *
+ * Just assert the time has changed.
+ */
+ assertTrue(thirdModTime != secondModTime);
}
public void testResolveFile() {
1.68 +46 -48 ant/src/main/org/apache/tools/ant/util/FileUtils.java
Index: FileUtils.java
===================================================================
RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/util/FileUtils.java,v
retrieving revision 1.67
retrieving revision 1.68
diff -u -r1.67 -r1.68
--- FileUtils.java 21 Apr 2004 07:12:29 -0000 1.67
+++ FileUtils.java 22 Apr 2004 14:03:46 -0000 1.68
@@ -57,9 +57,9 @@
*/
public class FileUtils {
- private static Random rand = new Random(System.currentTimeMillis());
- private static Object lockReflection = new Object();
- private static java.lang.reflect.Method setLastModified = null;
+ //get some non-crypto-grade randomness from various places.
+ private static Random rand = new Random(System.currentTimeMillis()
+ +Runtime.getRuntime().freeMemory());
private boolean onNetWare = Os.isFamily("netware");
@@ -78,6 +78,7 @@
*/
public static final long UNIX_FILE_TIMESTAMP_GRANULARITY = 1000;
+
// stolen from FilePathToURI of the Xerces-J team
static {
for (int i = 0; i <= 0x20; i++) {
@@ -648,65 +649,27 @@
}
if (preserveLastModified) {
- setFileLastModified(destFile, sourceFile.lastModified());
+ destFile.setLastModified(sourceFile.lastModified());
}
}
}
- /**
- * see whether we have a setLastModified method in File and return it.
- *
- * @return a method to setLastModified.
- */
- protected final Method getSetLastModified() {
- if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
- return null;
- }
- synchronized (lockReflection) {
- if (setLastModified == null) {
- try {
- setLastModified =
- java.io.File.class.getMethod("setLastModified",
- new Class[]
{Long.TYPE});
- } catch (NoSuchMethodException nse) {
- throw new BuildException("File.setlastModified not in
JDK > 1.1?",
- nse);
- }
- }
- }
- return setLastModified;
- }
/**
- * Calls File.setLastModified(long time) in a Java 1.1 compatible way.
+ * Calls File.setLastModified(long time). Originally written to
+ * to dynamically bind to that call on Java1.2+.
*
* @param file the file whose modified time is to be set
* @param time the time to which the last modified time is to be set.
- *
+ * if this is -1, the current time is used.
* @throws BuildException if the time cannot be set.
*/
public void setFileLastModified(File file, long time)
throws BuildException {
- if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
- return;
- }
- Long[] times = new Long[1];
if (time < 0) {
- times[0] = new Long(System.currentTimeMillis());
- } else {
- times[0] = new Long(time);
- }
-
- try {
- getSetLastModified().invoke(file, times);
- } catch (java.lang.reflect.InvocationTargetException ite) {
- Throwable nested = ite.getTargetException();
- throw new BuildException("Exception setting the modification
time "
- + "of " + file, nested);
- } catch (Throwable other) {
- throw new BuildException("Exception setting the modification
time "
- + "of " + file, other);
+ time=System.currentTimeMillis();
}
+ file.setLastModified(time);
}
/**
@@ -1357,5 +1320,40 @@
return UNIX_FILE_TIMESTAMP_GRANULARITY;
}
}
+
+ /**
+ * Returns true if the source is older than the dest.
+ * If the dest file does not exist, then the test returns false; it is
+ * implicitly not up do date.
+ * @param source source file (should be the older)
+ * @param dest dest file (should be the newer)
+ * @param granularity: an offset added to the source time.
+ * @return true if the source is older than the dest, taking the
+ * granularity into account
+ * @since Ant1.7
+ */
+ public boolean isUpToDate(File source,File dest,long granularity) {
+ //do a check for the destination file existing
+ if(!dest.exists()) {
+ //if it does not, then the file is not up to date.
+ return false;
+ }
+ long sourceTime=source.lastModified();
+ long destTime=dest.lastModified();
+ return destTime>=sourceTime+granularity;
+ }
+
+ /**
+ * returns true if the source is older than the dest
+ * @param source source file (should be the older)
+ * @param dest dest file (should be the newer)
+ * @return true if the source is older than the dest, taking the
granularity into account
+ * @since Ant1.7
+ */
+ public boolean isUpToDate(File source, File dest) {
+ return isUpToDate(source, dest, getFileTimestampGranularity());
+ }
+
+
}
1.36 +8 -1 ant/src/main/org/apache/tools/ant/taskdefs/SignJar.java
Index: SignJar.java
===================================================================
RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/SignJar.java,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -r1.35 -r1.36
--- SignJar.java 9 Mar 2004 16:48:06 -0000 1.35
+++ SignJar.java 22 Apr 2004 14:03:47 -0000 1.36
@@ -28,6 +28,7 @@
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.util.JavaEnvUtils;
+import org.apache.tools.ant.util.FileUtils;
/**
* Signs jar or zip files with the javasign command line tool. The
@@ -321,7 +322,7 @@
if (jarFile.equals(signedjarFile)) {
return false;
}
- if (signedjarFile.lastModified() > jarFile.lastModified()) {
+ if (FileUtils.newFileUtils().isUpToDate(jarFile,signedjarFile)) {
return true;
}
} else {
@@ -333,6 +334,12 @@
return false;
}
+ /**
+ * test for a file being signed, by looking for a signature in the
META-INF
+ * directory
+ * @param file
+ * @return
+ */
protected boolean isSigned(File file) {
final String SIG_START = "META-INF/";
final String SIG_END = ".SF";
1.166 +1 -6 ant/src/main/org/apache/tools/ant/Project.java
Index: Project.java
===================================================================
RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/Project.java,v
retrieving revision 1.165
retrieving revision 1.166
diff -u -r1.165 -r1.166
--- Project.java 22 Mar 2004 15:54:52 -0000 1.165
+++ Project.java 22 Apr 2004 14:03:47 -0000 1.166
@@ -1531,11 +1531,6 @@
*/
public void setFileLastModified(File file, long time)
throws BuildException {
- if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
- log("Cannot change the modification time of " + file
- + " in JDK 1.1", Project.MSG_WARN);
- return;
- }
fileUtils.setFileLastModified(file, time);
log("Setting modification time for " + file, MSG_VERBOSE);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]