1 16 17 package org.apache.commons.io; 18 19 import java.io.File ; 20 import java.util.Date ; 21 22 import org.apache.commons.io.testtools.*; 23 24 29 public class FileUtilsFileNewerTestCase extends FileBasedTestCase { 30 31 private static final int FILE1_SIZE = 1; 33 private static final int FILE2_SIZE = 1024 * 4 + 1; 34 35 private File m_testFile1; 36 private File m_testFile2; 37 38 public FileUtilsFileNewerTestCase(String name) { 39 super(name); 40 41 m_testFile1 = new File (getTestDirectory(), "file1-test.txt"); 42 m_testFile2 = new File (getTestDirectory(), "file2-test.txt"); 43 } 44 45 46 protected void setUp() throws Exception { 47 getTestDirectory().mkdirs(); 48 createFile(m_testFile1, FILE1_SIZE); 49 createFile(m_testFile2, FILE2_SIZE); 50 } 51 52 53 protected void tearDown() throws Exception { 54 m_testFile1.delete(); 55 m_testFile2.delete(); 56 } 57 58 65 public void testIsFileNewer() { 66 if (!m_testFile1.exists()) 67 throw new IllegalStateException ("The m_testFile1 should exist"); 68 69 long fileLastModified = m_testFile1.lastModified(); 70 final long ONE_SECOND = 1000; 71 72 testIsFileNewer("one second earlier is not newer" , m_testFile1, fileLastModified + ONE_SECOND, false); 73 testIsFileNewer("same time is not newer" , m_testFile1, fileLastModified, false); 74 testIsFileNewer("one second later is newer" , m_testFile1, fileLastModified - ONE_SECOND, true); 75 } 76 77 84 public void testIsFileNewerImaginaryFile() { 85 File imaginaryFile = new File (getTestDirectory(), "imaginaryFile"); 86 if (imaginaryFile.exists()) 87 throw new IllegalStateException ("The imaginary File exists"); 88 89 testIsFileNewer("imaginary file can be newer" , imaginaryFile, 0, false); 90 } 91 92 114 protected void testIsFileNewer(String description, File file, long time, boolean wantedResult) { 115 assertEquals(description + " - time", wantedResult, FileUtils.isFileNewer(file, time)); 116 assertEquals(description + " - date", wantedResult, FileUtils.isFileNewer(file, new Date (time))); 117 118 File temporaryFile = m_testFile2; 119 120 temporaryFile.setLastModified(time); 121 if (temporaryFile.lastModified() != time) 122 throw new IllegalStateException ("The temporary file hasn't the right last modification date"); 123 assertEquals(description + " - file", wantedResult, FileUtils.isFileNewer(file, temporaryFile)); 124 } 125 126 131 public void testIsFileNewerNoFile() { 132 try { 133 FileUtils.isFileNewer(null,0); 134 fail("File not specified"); 135 } catch (IllegalArgumentException e) {} 136 } 137 138 143 public void testIsFileNewerNoDate() { 144 try { 145 FileUtils.isFileNewer(m_testFile1, (Date ) null); 146 fail("Date not specified"); 147 } catch (IllegalArgumentException e) {} 148 } 149 150 155 public void testIsFileNewerNoFileReference() { 156 try { 157 FileUtils.isFileNewer(m_testFile1, (File ) null); 158 fail("Reference file not specified"); 159 } catch (IllegalArgumentException e) {} 160 } 161 } 162 | Popular Tags |