1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
26
27
28
29 public class FileUtilsFileNewerTestCase extends FileBasedTestCase {
30
31
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
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
59
60
61
62
63
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
79
80
81
82
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
127
128
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
139
140
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
151
152
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 }