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 HiddenFileFilter}.
35   */
36  // CHECKSTYLE:OFF Test code
37  public class HiddenFileFilterTest extends BaseFilterTest {
38  
39      private static File testDir;
40  
41      private static File visibleFile;
42  
43      private static FileSelectInfo visibleFileInfo;
44  
45      private static File hiddenFile;
46  
47      private static FileSelectInfo hiddenFileInfo;
48  
49      private static File notExistingFile;
50  
51      private static FileSelectInfo notExistingFileInfo;
52  
53      private static File zipFile;
54  
55      private static FileObject zipFileObj;
56  
57      @AfterClass
58      public static void afterClass() throws IOException {
59  
60          visibleFile = null;
61          visibleFileInfo = null;
62          hiddenFile = null;
63          hiddenFileInfo = null;
64          notExistingFile = null;
65          notExistingFileInfo = null;
66  
67          zipFileObj.close();
68          FileUtils.deleteQuietly(zipFile);
69          zipFile = null;
70  
71          FileUtils.deleteDirectory(testDir);
72          testDir = null;
73      }
74  
75      @BeforeClass
76      public static void beforeClass() throws IOException {
77          testDir = getTestDir(HiddenFileFilterTest.class.getName());
78          testDir.mkdir();
79  
80          visibleFile = new File(testDir, "visible.txt");
81          FileUtils.touch(visibleFile);
82          visibleFileInfo = createFileSelectInfo(visibleFile);
83  
84          hiddenFile = new File(testDir, "hidden.txt");
85          // TODO xxx In Java 6 there is no way to hide a file
86          // hiddenFile.setVisible(false);
87          hiddenFileInfo = createFileSelectInfo(hiddenFile);
88  
89          notExistingFile = new File(testDir, "not-existing-file.txt");
90          notExistingFileInfo = createFileSelectInfo(notExistingFile);
91  
92          // Zip the test directory
93          zipFile = new File(getTempDir(), HiddenFileFilterTest.class.getName() + ".zip");
94          zipDir(testDir, "", zipFile);
95          zipFileObj = getZipFileObject(zipFile);
96  
97      }
98  
99      @Test
100     public void testAcceptHidden() throws FileSystemException {
101 
102         final FileFilter testee = HiddenFileFilter.HIDDEN;
103 
104         Assert.assertFalse(testee.accept(visibleFileInfo));
105         // TODO xxx In Java 6 there is no way to hide a file
106         // assertThat(testee.accept(hiddenFileInfo));
107         Assert.assertFalse(testee.accept(notExistingFileInfo));
108 
109     }
110 
111     @Test
112     public void testAcceptVisible() throws FileSystemException {
113 
114         final FileFilter testee = HiddenFileFilter.VISIBLE;
115 
116         Assert.assertTrue(testee.accept(visibleFileInfo));
117         // TODO xxx In Java 6 there is no way to hide a file
118         // assertThat(testee.accept(hiddenFileInfo));
119         Assert.assertTrue(testee.accept(notExistingFileInfo));
120 
121     }
122 
123     @Test
124     public void testZipFile() throws FileSystemException {
125 
126         // Same test with ZIP file
127         final FileObject[] files;
128 
129         // TODO xxx In Java 6 there is no way to hide a file
130         // files = zipFileObj.findFiles(new
131         // FileFilterSelector(HiddenFileFilter.HIDDEN));
132         // assertContains(files, hiddenFile.getName());
133         // assertThat(files).hasSize(1);
134 
135         files = zipFileObj.findFiles(new FileFilterSelector(HiddenFileFilter.VISIBLE));
136         assertContains(files, visibleFile.getName());
137         Assert.assertEquals(1, files.length);
138 
139     }
140 
141 }
142 // CHECKSTYLE:ON