View Javadoc
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.vfs2.filter;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.util.Date;
22  
23  import org.apache.commons.io.FileUtils;
24  import org.apache.commons.vfs2.FileFilterSelector;
25  import org.apache.commons.vfs2.FileObject;
26  import org.apache.commons.vfs2.FileSelectInfo;
27  import org.apache.commons.vfs2.FileSystemException;
28  import org.junit.AfterClass;
29  import org.junit.Assert;
30  import org.junit.BeforeClass;
31  import org.junit.Test;
32  
33  /**
34   * Test for {@link AgeFileFilter}.
35   */
36  // CHECKSTYLE:OFF Test code
37  public class AgeFileFilterTest extends BaseFilterTest {
38  
39      private static final long DAY_MILLIS = 24 * 60 * 60 * 1000;
40  
41      private static final long NOW_MILLIS = System.currentTimeMillis();
42  
43      private static final long TWO_DAYS_AGO_MILLIS = NOW_MILLIS - (2 * DAY_MILLIS);
44  
45      private static final long TWO_DAYS_LATER_MILLIS = NOW_MILLIS + (2 * DAY_MILLIS);
46  
47      private static File testDir;
48  
49      private static File oldFile;
50  
51      private static FileSelectInfo oldFileInfo;
52  
53      private static File newFile;
54  
55      private static FileSelectInfo newFileInfo;
56  
57      private static File currentFile;
58  
59      private static FileSelectInfo currentFileInfo;
60  
61      private static File zipFile;
62  
63      private static FileObject zipFileObj;
64  
65      @AfterClass
66      public static void afterClass() throws IOException {
67          newFileInfo = null;
68          newFile = null;
69  
70          currentFileInfo = null;
71          currentFile = null;
72  
73          oldFileInfo = null;
74          oldFile = null;
75  
76          zipFileObj.close();
77          FileUtils.deleteQuietly(zipFile);
78          zipFile = null;
79  
80          FileUtils.deleteDirectory(testDir);
81          testDir = null;
82      }
83  
84      @BeforeClass
85      public static void beforeClass() throws IOException {
86          testDir = getTestDir(AgeFileFilterTest.class.getName());
87  
88          // Set the file's time stamp two days back
89          oldFile = new File(testDir, "old.txt");
90          FileUtils.touch(oldFile);
91          oldFile.setLastModified(TWO_DAYS_AGO_MILLIS);
92          oldFileInfo = createFileSelectInfo(oldFile);
93  
94          // Reference file
95          currentFile = new File(testDir, "current.txt");
96          FileUtils.touch(currentFile);
97          currentFile.setLastModified(NOW_MILLIS);
98          currentFileInfo = createFileSelectInfo(currentFile);
99  
100         // Set the file's time stamp two days into the future
101         newFile = new File(testDir, "new.txt");
102         FileUtils.touch(newFile);
103         newFile.setLastModified(TWO_DAYS_LATER_MILLIS);
104         newFileInfo = createFileSelectInfo(newFile);
105 
106         // Zip the test directory
107         zipFile = new File(getTempDir(), AgeFileFilterTest.class.getName() + ".zip");
108         zipDir(testDir, "", zipFile);
109         zipFileObj = getZipFileObject(zipFile);
110 
111     }
112 
113     @Test
114     public void testAgeFileFilterDate() throws FileSystemException {
115 
116         final AgeFileFilter testee = new AgeFileFilter(new Date());
117         Assert.assertTrue(testee.accept(oldFileInfo));
118         Assert.assertTrue(testee.accept(currentFileInfo));
119         Assert.assertFalse(testee.accept(newFileInfo));
120 
121     }
122 
123     @Test
124     public void testAgeFileFilterDateBoolean() throws FileSystemException {
125 
126         AgeFileFilter testee;
127 
128         testee = new AgeFileFilter(new Date(), true);
129         Assert.assertTrue(testee.accept(oldFileInfo));
130         Assert.assertTrue(testee.accept(currentFileInfo));
131         Assert.assertFalse(testee.accept(newFileInfo));
132 
133         testee = new AgeFileFilter(new Date(), false);
134         Assert.assertFalse(testee.accept(oldFileInfo));
135         Assert.assertFalse(testee.accept(currentFileInfo));
136         Assert.assertTrue(testee.accept(newFileInfo));
137 
138     }
139 
140     @Test
141     public void testAgeFileFilterFile() throws FileSystemException {
142 
143         final AgeFileFilter testee = new AgeFileFilter(currentFileInfo.getFile());
144         Assert.assertTrue(testee.accept(oldFileInfo));
145         Assert.assertTrue(testee.accept(currentFileInfo));
146         Assert.assertFalse(testee.accept(newFileInfo));
147 
148     }
149 
150     @Test
151     public void testAgeFileFilterFileBoolean() throws FileSystemException {
152 
153         AgeFileFilter testee;
154 
155         testee = new AgeFileFilter(currentFileInfo.getFile(), true);
156         Assert.assertTrue(testee.accept(oldFileInfo));
157         Assert.assertTrue(testee.accept(currentFileInfo));
158         Assert.assertFalse(testee.accept(newFileInfo));
159 
160         testee = new AgeFileFilter(currentFileInfo.getFile(), false);
161         Assert.assertFalse(testee.accept(oldFileInfo));
162         Assert.assertFalse(testee.accept(currentFileInfo));
163         Assert.assertTrue(testee.accept(newFileInfo));
164 
165     }
166 
167     @Test
168     public void testAgeFileFilterLong() throws FileSystemException {
169 
170         final AgeFileFilter testee = new AgeFileFilter(NOW_MILLIS);
171         Assert.assertTrue(testee.accept(oldFileInfo));
172         Assert.assertTrue(testee.accept(currentFileInfo));
173         Assert.assertFalse(testee.accept(newFileInfo));
174 
175     }
176 
177     @Test
178     public void testAgeFileFilterLongBoolean() throws FileSystemException {
179 
180         AgeFileFilter testee;
181 
182         testee = new AgeFileFilter(NOW_MILLIS, true);
183         Assert.assertTrue(testee.accept(oldFileInfo));
184         Assert.assertTrue(testee.accept(currentFileInfo));
185         Assert.assertFalse(testee.accept(newFileInfo));
186 
187         testee = new AgeFileFilter(NOW_MILLIS, false);
188         Assert.assertFalse(testee.accept(oldFileInfo));
189         Assert.assertFalse(testee.accept(currentFileInfo));
190         Assert.assertTrue(testee.accept(newFileInfo));
191 
192         // Same test with ZIP file
193         FileObject[] files;
194 
195         files = zipFileObj.findFiles(new FileFilterSelector(new AgeFileFilter(NOW_MILLIS, true)));
196         assertContains(files, oldFile.getName(), currentFile.getName());
197         Assert.assertEquals(2, files.length);
198 
199         files = zipFileObj.findFiles(new FileFilterSelector(new AgeFileFilter(NOW_MILLIS, false)));
200         assertContains(files, newFile.getName());
201         Assert.assertEquals(1, files.length);
202 
203     }
204 
205 }
206 // CHECKSTYLE:ON