1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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  /**
25   * This is used to test FileUtils for correctness.
26   *
27   * @author <a href="mailto:alban.peignier@free.fr">Alban Peignier</a>
28   */
29  public class FileUtilsFileNewerTestCase extends FileBasedTestCase {
30  
31      // Test data
32      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      /** @see junit.framework.TestCase#setUp() */
46      protected void setUp() throws Exception {
47          getTestDirectory().mkdirs();
48          createFile(m_testFile1, FILE1_SIZE);
49          createFile(m_testFile2, FILE2_SIZE);
50      }
51  
52      /** @see junit.framework.TestCase#tearDown() */
53      protected void tearDown() throws Exception {
54          m_testFile1.delete();
55          m_testFile2.delete();
56      }
57  
58      /**
59       * Tests the <code>isFileNewer(File, *)</code> methods which a "normal" file.
60       *
61       * @see FileUtils#isFileNewer(File, long)
62       * @see FileUtils#isFileNewer(File, Date)
63       * @see FileUtils#isFileNewer(File, File)
64       */
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 TWO_SECOND = 2000;
71  
72          testIsFileNewer("two second earlier is not newer" , m_testFile1, fileLastModified + TWO_SECOND, false);
73          testIsFileNewer("same time is not newer" , m_testFile1, fileLastModified, false);
74          testIsFileNewer("two second later is newer" , m_testFile1, fileLastModified - TWO_SECOND, true);
75      }
76  
77      /**
78       * Tests the <code>isFileNewer(File, *)</code> methods which a not existing file.
79       *
80       * @see FileUtils#isFileNewer(File, long)
81       * @see FileUtils#isFileNewer(File, Date)
82       * @see FileUtils#isFileNewer(File, File)
83       */
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, m_testFile2.lastModified(), false);
90      }
91  
92      /**
93       * Tests the <code>isFileNewer(File, *)</code> methods which the specified conditions.
94       * <p/>
95       * Creates :
96       * <ul>
97       * <li>a <code>Date</code> which represents the time reference</li>
98       * <li>a temporary file with the same last modification date than the time reference</li>
99       * </ul>
100      * Then compares (with the needed <code>isFileNewer</code> method) the last modification date of 
101      * the specified file with the specified time reference, the created <code>Date</code> and the temporary 
102      * file.
103      * <br/>
104      * The test is successfull if the three comparaisons return the specified wanted result.
105      *
106      * @param description describes the tested situation
107      * @param file the file of which the last modification date is compared
108      * @param timeMillis the time reference measured in milliseconds since the epoch 
109      *
110      * @see FileUtils#isFileNewer(File, long)
111      * @see FileUtils#isFileNewer(File, Date)
112      * @see FileUtils#isFileNewer(File, File)
113      */
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         assertEquals("The temporary file hasn't the right last modification date", time, temporaryFile.lastModified());
122         assertEquals(description + " - file", wantedResult, FileUtils.isFileNewer(file, temporaryFile));
123     }
124 
125     /**
126      * Tests the <code>isFileNewer(File, long)</code> method without specifying a <code>File</code>.
127      * <br/>
128      * The test is successfull if the method throws an <code>IllegalArgumentException</code>. 
129      */
130     public void testIsFileNewerNoFile() {
131         try {
132             FileUtils.isFileNewer(null,0);
133             fail("File not specified");
134         } catch (IllegalArgumentException e) {}
135     }
136 
137     /**
138      * Tests the <code>isFileNewer(File, Date)</code> method without specifying a <code>Date</code>.
139      * <br/>
140      * The test is successfull if the method throws an <code>IllegalArgumentException</code>. 
141      */
142     public void testIsFileNewerNoDate() {
143         try {
144             FileUtils.isFileNewer(m_testFile1, (Date) null);
145             fail("Date not specified");
146         } catch (IllegalArgumentException e) {}
147     }
148 
149     /**
150      * Tests the <code>isFileNewer(File, File)</code> method without specifying a reference <code>File</code>.
151      * <br/>
152      * The test is successfull if the method throws an <code>IllegalArgumentException</code>. 
153      */
154     public void testIsFileNewerNoFileReference() {
155         try {
156             FileUtils.isFileNewer(m_testFile1, (File) null);
157             fail("Reference file not specified");
158         } catch (IllegalArgumentException e) {}
159     }
160 }