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  
22  import org.apache.commons.io.FileUtils;
23  import org.apache.commons.vfs2.FileFilter;
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 EmptyFileFilter}.
35   */
36  // CHECKSTYLE:OFF Test code
37  public class EmptyFileFilterTest extends BaseFilterTest {
38  
39      private static File testDir;
40  
41      private static File notEmptyFile;
42  
43      private static FileSelectInfo notEmptyFileInfo;
44  
45      private static File emptyFile;
46  
47      private static FileSelectInfo emptyFileInfo;
48  
49      private static File notEmptyDir;
50  
51      private static FileSelectInfo notEmptyDirInfo;
52  
53      private static File emptyDir;
54  
55      private static FileSelectInfo emptyDirInfo;
56  
57      private static File notExistingFile;
58  
59      private static FileSelectInfo notExistingFileInfo;
60  
61      private static File zipFile;
62  
63      private static FileObject zipFileObj;
64  
65      @AfterClass
66      public static void afterClass() throws IOException {
67  
68          notEmptyFile = null;
69          notEmptyFileInfo = null;
70          emptyFile = null;
71          emptyFileInfo = null;
72          notEmptyDir = null;
73          notEmptyDirInfo = null;
74          emptyDir = null;
75          emptyDirInfo = null;
76          notExistingFile = null;
77          notExistingFileInfo = null;
78  
79          zipFileObj.close();
80          FileUtils.deleteQuietly(zipFile);
81          zipFile = null;
82  
83          FileUtils.deleteDirectory(testDir);
84          testDir = null;
85      }
86  
87      @BeforeClass
88      public static void beforeClass() throws IOException {
89          testDir = getTestDir(EmptyFileFilterTest.class.getName());
90          testDir.mkdir();
91  
92          notEmptyFile = new File(testDir, "full.txt");
93          FileUtils.write(notEmptyFile, "whatever");
94          notEmptyFileInfo = createFileSelectInfo(notEmptyFile);
95  
96          emptyFile = new File(testDir, "empty.txt");
97          FileUtils.touch(emptyFile);
98          emptyFileInfo = createFileSelectInfo(emptyFile);
99  
100         notEmptyDir = new File(testDir, "full-dir");
101         notEmptyDir.mkdir();
102         notEmptyDirInfo = createFileSelectInfo(notEmptyDir);
103         FileUtils.touch(new File(notEmptyDir, "foobar.txt"));
104 
105         emptyDir = new File(testDir, "empty-dir");
106         emptyDir.mkdir();
107         emptyDirInfo = createFileSelectInfo(emptyDir);
108 
109         notExistingFile = new File(testDir, "not-existing-file.txt");
110         notExistingFileInfo = createFileSelectInfo(notExistingFile);
111 
112         // Zip the test directory
113         zipFile = new File(getTempDir(), EmptyFileFilterTest.class.getName() + ".zip");
114         zipDir(testDir, "", zipFile);
115         zipFileObj = getZipFileObject(zipFile);
116 
117     }
118 
119     @Test
120     public void testAcceptEmpty() throws FileSystemException {
121 
122         final FileFilter testee = EmptyFileFilter.EMPTY;
123 
124         Assert.assertFalse(testee.accept(notEmptyFileInfo));
125         Assert.assertTrue(testee.accept(emptyFileInfo));
126         Assert.assertFalse(testee.accept(notEmptyDirInfo));
127         Assert.assertTrue(testee.accept(emptyDirInfo));
128         Assert.assertTrue(testee.accept(notExistingFileInfo));
129 
130     }
131 
132     @Test
133     public void testAcceptNotEmpty() throws FileSystemException {
134 
135         final FileFilter testee = EmptyFileFilter.NOT_EMPTY;
136 
137         Assert.assertTrue(testee.accept(notEmptyFileInfo));
138         Assert.assertFalse(testee.accept(emptyFileInfo));
139         Assert.assertTrue(testee.accept(notEmptyDirInfo));
140         Assert.assertFalse(testee.accept(emptyDirInfo));
141         Assert.assertFalse(testee.accept(notExistingFileInfo));
142 
143     }
144 
145     @Test
146     public void testZipFile() throws FileSystemException {
147 
148         // Same test with ZIP file
149         FileObject[] files;
150 
151         files = zipFileObj.findFiles(new FileFilterSelector(EmptyFileFilter.EMPTY));
152         assertContains(files, emptyFile.getName());
153         Assert.assertEquals(1, files.length);
154 
155         files = zipFileObj.findFiles(new FileFilterSelector(EmptyFileFilter.NOT_EMPTY));
156         assertContains(files, notEmptyFile.getName(), notEmptyDir.getName());
157         Assert.assertEquals(2, files.length);
158 
159     }
160 
161 }
162 // CHECKSTYLE:ON