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 static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.File;
24  import java.io.IOException;
25  
26  import org.apache.commons.io.FileUtils;
27  import org.apache.commons.io.IOUtils;
28  import org.apache.commons.vfs2.FileFilter;
29  import org.apache.commons.vfs2.FileFilterSelector;
30  import org.apache.commons.vfs2.FileObject;
31  import org.apache.commons.vfs2.FileSelectInfo;
32  import org.apache.commons.vfs2.FileSelector;
33  import org.apache.commons.vfs2.FileSystemException;
34  import org.junit.jupiter.api.AfterAll;
35  import org.junit.jupiter.api.BeforeAll;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * Test for {@link DirectoryFileFilter} and {@link FileFileFilter}.
40   */
41  // CHECKSTYLE:OFF Test code
42  public class DirectoryAndFileFilterTest extends BaseFilterTest {
43  
44      private static final String FILE = "myfile.txt";
45  
46      private static final String DIR = "mydir";
47  
48      private static File testDir;
49  
50      private static File file;
51  
52      private static FileSelectInfo fileInfo;
53  
54      private static File dir;
55  
56      private static FileSelectInfo dirInfo;
57  
58      private static File notExistingFile;
59  
60      private static FileSelectInfo notExistingFileInfo;
61  
62      private static File zipFile;
63  
64      private static FileObject zipFileObj;
65  
66      @AfterAll
67      public static void afterClass() throws IOException {
68  
69          file = null;
70          fileInfo = null;
71  
72          dir = null;
73          dirInfo = null;
74  
75          notExistingFileInfo = null;
76          notExistingFile = null;
77  
78          IOUtils.close(zipFileObj);
79          FileUtils.deleteQuietly(zipFile);
80          zipFile = null;
81  
82          FileUtils.deleteDirectory(testDir);
83  
84          testDir = null;
85      }
86  
87      @BeforeAll
88      public static void beforeClass() throws IOException {
89  
90          testDir = getTestDir(DirectoryAndFileFilterTest.class.getName());
91          testDir.mkdir();
92  
93          dir = new File(testDir, DIR);
94          dir.mkdir();
95          dirInfo = createFileSelectInfo(dir);
96  
97          file = new File(dir, FILE);
98          FileUtils.touch(file);
99          fileInfo = createFileSelectInfo(file);
100 
101         notExistingFile = new File(testDir, "not-existing-file.txt");
102         notExistingFileInfo = createFileSelectInfo(notExistingFile);
103 
104         zipFile = new File(getTempDir(), DirectoryAndFileFilterTest.class.getName() + ".zip");
105         zipDir(testDir, "", zipFile);
106         zipFileObj = getZipFileObject(zipFile);
107     }
108 
109     @Test
110     public void testAcceptZipFile() throws FileSystemException {
111 
112         FileObject[] files;
113 
114         // FILE Filter
115         files = zipFileObj.findFiles(new FileSelector() {
116             @Override
117             public boolean includeFile(final FileSelectInfo fileInfo) throws Exception {
118                 return FileFileFilter.FILE.accept(fileInfo);
119             }
120 
121             @Override
122             public boolean traverseDescendents(final FileSelectInfo fileInfo) throws Exception {
123                 return true;
124             }
125         });
126         assertContains(files, FILE);
127         assertEquals(1, files.length);
128 
129         // DIRECTORY Filter
130         files = zipFileObj.findFiles(new FileFilterSelector(DirectoryFileFilter.DIRECTORY));
131         assertContains(files, DIR);
132         assertEquals(1, files.length);
133     }
134 
135     @Test
136     public void testDirectoryFileFilter() throws FileSystemException {
137 
138         final FileFilter testee = DirectoryFileFilter.DIRECTORY;
139 
140         assertTrue(testee.accept(dirInfo));
141         assertFalse(testee.accept(fileInfo));
142         assertFalse(testee.accept(notExistingFileInfo));
143     }
144 
145     @Test
146     public void testFileFileFilter() throws FileSystemException {
147 
148         final FileFilter testee = FileFileFilter.FILE;
149 
150         assertTrue(testee.accept(fileInfo));
151         assertFalse(testee.accept(dirInfo));
152         assertFalse(testee.accept(notExistingFileInfo));
153     }
154 
155 }
156 // CHECKSTYLE:ON