commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r425111 - in /jakarta/commons/proper/io/trunk/src: java/org/apache/commons/io/FileUtils.java test/org/apache/commons/io/FileUtilsTestCase.java
Date Mon, 24 Jul 2006 16:23:46 GMT
Author: niallp
Date: Mon Jul 24 09:23:45 2006
New Revision: 425111

URL: http://svn.apache.org/viewvc?rev=425111&view=rev
Log:
IO-88 Add isFileOlder() methods to FileUtils (counterparts to existing isFileNewer() methods)

Modified:
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?rev=425111&r1=425110&r2=425111&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Mon Jul
24 09:23:45 2006
@@ -1291,4 +1291,70 @@
         return file.lastModified() > timeMillis;
     }
 
+
+    //-----------------------------------------------------------------------
+    /**
+     * Tests if the specified <code>File</code> is older than the reference
+     * <code>File</code>.
+     *
+     * @param file  the <code>File</code> of which the modification date must
+     * be compared, not null
+     * @param reference  the <code>File</code> of which the modification date
+     * is used, not null
+     * @return true if the <code>File</code> exists and has been modified before
+     * the reference <code>File</code>
+     * @throws IllegalArgumentException if the file is null
+     * @throws IllegalArgumentException if the reference file is null or doesn't exist
+     */
+     public static boolean isFileOlder(File file, File reference) {
+        if (reference == null) {
+            throw new IllegalArgumentException("No specified reference file");
+        }
+        if (!reference.exists()) {
+            throw new IllegalArgumentException("The reference file '"
+                    + file + "' doesn't exist");
+        }
+        return isFileOlder(file, reference.lastModified());
+    }
+
+    /**
+     * Tests if the specified <code>File</code> is older than the specified
+     * <code>Date</code>.
+     * 
+     * @param file  the <code>File</code> of which the modification date
+     * must be compared, not null
+     * @param date  the date reference, not null
+     * @return true if the <code>File</code> exists and has been modified
+     * before the given <code>Date</code>.
+     * @throws IllegalArgumentException if the file is null
+     * @throws IllegalArgumentException if the date is null
+     */
+    public static boolean isFileOlder(File file, Date date) {
+        if (date == null) {
+            throw new IllegalArgumentException("No specified date");
+        }
+        return isFileOlder(file, date.getTime());
+    }
+
+    /**
+     * Tests if the specified <code>File</code> is older than the specified
+     * time reference.
+     *
+     * @param file  the <code>File</code> of which the modification date must
+     * be compared, not null
+     * @param timeMillis  the time reference measured in milliseconds since the
+     * epoch (00:00:00 GMT, January 1, 1970)
+     * @return true if the <code>File</code> exists and has been modified before
+     * the given time reference.
+     * @throws IllegalArgumentException if the file is null
+     */
+     public static boolean isFileOlder(File file, long timeMillis) {
+        if (file == null) {
+            throw new IllegalArgumentException("No specified file");
+        }
+        if (!file.exists()) {
+            return false;
+        }
+        return file.lastModified() < timeMillis;
+    }
 }

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?rev=425111&r1=425110&r2=425111&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
(original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
Mon Jul 24 09:23:45 2006
@@ -23,6 +23,7 @@
 import java.net.URL;
 import java.util.Arrays;
 import java.util.Collection;
+import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -353,11 +354,110 @@
             FileUtils.sizeOfDirectory(file));
     }
 
-    // isFileNewer
+    // isFileNewer / isFileOlder
+    public void testIsFileNewerOlder() throws Exception {
+        File reference   = new File(getTestDirectory(), "FileUtils-reference.txt");
+        File oldFile     = new File(getTestDirectory(), "FileUtils-old.txt");
+        File newFile     = new File(getTestDirectory(), "FileUtils-new.txt");
+        File invalidFile = new File(getTestDirectory(), "FileUtils-invalid-file.txt");
+
+        // Create Files
+        createFile(oldFile, 0);
+        spin(oldFile.lastModified());
+        long now = System.currentTimeMillis();
+        Date date = new Date();
+        createFile(reference, 0);
+        spin(reference.lastModified());
+        createFile(newFile, 0);
+
+        // Test isFileNewer()
+        assertFalse("Old File - Newer - File", FileUtils.isFileNewer(oldFile, reference));
+        assertFalse("Old File - Newer - Date", FileUtils.isFileNewer(oldFile, date));
+        assertFalse("Old File - Newer - Mili", FileUtils.isFileNewer(oldFile, now));
+        assertTrue("New File - Newer - File", FileUtils.isFileNewer(newFile, reference));
+        assertTrue("New File - Newer - Date", FileUtils.isFileNewer(newFile, date));
+        assertTrue("New File - Newer - Mili", FileUtils.isFileNewer(newFile, now));
+        assertFalse("Invalid - Newer - File", FileUtils.isFileNewer(invalidFile, reference));
+        
+        // Test isFileOlder()
+        assertTrue("Old File - Older - File", FileUtils.isFileOlder(oldFile, reference));
+        assertTrue("Old File - Older - Date", FileUtils.isFileOlder(oldFile, date));
+        assertTrue("Old File - Older - Mili", FileUtils.isFileOlder(oldFile, now));
+        assertFalse("New File - Older - File", FileUtils.isFileOlder(newFile, reference));
+        assertFalse("New File - Older - Date", FileUtils.isFileOlder(newFile, date));
+        assertFalse("New File - Older - Mili", FileUtils.isFileOlder(newFile, now));
+        assertFalse("Invalid - Older - File", FileUtils.isFileOlder(invalidFile, reference));
+        
+        
+        // ----- Test isFileNewer() exceptions -----
+        // Null File
+        try {
+            FileUtils.isFileNewer(null, now);
+            fail("Newer Null, expected IllegalArgumentExcepion");
+        } catch (IllegalArgumentException expected) {
+            // expected result
+        }
+        
+        // Null reference File
+        try {
+            FileUtils.isFileNewer(oldFile, (File)null);
+            fail("Newer Null reference, expected IllegalArgumentExcepion");
+        } catch (IllegalArgumentException expected) {
+            // expected result
+        }
+        
+        // Invalid reference File
+        try {
+            FileUtils.isFileNewer(oldFile, invalidFile);
+            fail("Newer invalid reference, expected IllegalArgumentExcepion");
+        } catch (IllegalArgumentException expected) {
+            // expected result
+        }
+        
+        // Null reference Date
+        try {
+            FileUtils.isFileNewer(oldFile, (Date)null);
+            fail("Newer Null date, expected IllegalArgumentExcepion");
+        } catch (IllegalArgumentException expected) {
+            // expected result
+        }
+
+
+        // ----- Test isFileOlder() exceptions -----
+        // Null File
+        try {
+            FileUtils.isFileOlder(null, now);
+            fail("Older Null, expected IllegalArgumentExcepion");
+        } catch (IllegalArgumentException expected) {
+            // expected result
+        }
+        
+        // Null reference File
+        try {
+            FileUtils.isFileOlder(oldFile, (File)null);
+            fail("Older Null reference, expected IllegalArgumentExcepion");
+        } catch (IllegalArgumentException expected) {
+            // expected result
+        }
+        
+        // Invalid reference File
+        try {
+            FileUtils.isFileOlder(oldFile, invalidFile);
+            fail("Older invalid reference, expected IllegalArgumentExcepion");
+        } catch (IllegalArgumentException expected) {
+            // expected result
+        }
+        
+        // Null reference Date
+        try {
+            FileUtils.isFileOlder(oldFile, (Date)null);
+            fail("Older Null date, expected IllegalArgumentExcepion");
+        } catch (IllegalArgumentException expected) {
+            // expected result
+        }
+
+    }
 
-//    // TODO Finish test
-//    public void XtestIsFileNewer() {}
-//
 //    // TODO Remove after debugging
 //    private void log(Object obj) {
 //        System.out.println(
@@ -828,6 +928,13 @@
             IOUtils.LINE_SEPARATOR + "some text" + IOUtils.LINE_SEPARATOR;
         String actual = FileUtils.readFileToString(file, "US-ASCII");
         assertEquals(expected, actual);
+    }
+
+    private void spin(long now) throws InterruptedException {
+        final long end = now + 1000;
+        while (System.currentTimeMillis() <= end) {
+            Thread.sleep(Math.max(1, end - System.currentTimeMillis()));
+        }
     }
 
 }



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


Mime
View raw message