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