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.FileFilterSelector;
29  import org.apache.commons.vfs2.FileObject;
30  import org.apache.commons.vfs2.FileSelectInfo;
31  import org.apache.commons.vfs2.FileSystemException;
32  import org.junit.jupiter.api.AfterAll;
33  import org.junit.jupiter.api.BeforeAll;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Test for {@link CanReadFileFilter}.
38   */
39  // CHECKSTYLE:OFF Test code
40  public class CanReadFileFilterTest extends BaseFilterTest {
41  
42      private static final String WRITABLE = "writable.txt";
43  
44      private static final String READONLY = "readonly.txt";
45  
46      private static File testDir;
47  
48      private static File writableFile;
49  
50      private static FileSelectInfo writableFileInfo;
51  
52      private static File readOnlyFile;
53  
54      private static FileSelectInfo readOnlyFileInfo;
55  
56      private static File notExistingFile;
57  
58      private static FileSelectInfo notExistingFileInfo;
59  
60      private static File zipFile;
61  
62      private static FileObject zipFileObj;
63  
64      @AfterAll
65      public static void afterClass() throws IOException {
66  
67          writableFileInfo = null;
68          delete(writableFile);
69          writableFile = null;
70  
71          readOnlyFileInfo = null;
72          delete(readOnlyFile);
73          readOnlyFile = 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          testDir = null;
84      }
85  
86      @BeforeAll
87      public static void beforeClass() throws IOException {
88  
89          testDir = getTestDir(CanReadFileFilterTest.class.getName());
90  
91          writableFile = new File(testDir, WRITABLE);
92          writableFileInfo = createFileSelectInfo(writableFile);
93          FileUtils.touch(writableFile);
94  
95          readOnlyFile = new File(testDir, READONLY);
96          readOnlyFileInfo = createFileSelectInfo(readOnlyFile);
97          FileUtils.touch(readOnlyFile);
98          readOnlyFile.setReadable(true);
99          readOnlyFile.setWritable(false);
100 
101         notExistingFile = new File(testDir, "not-existing-file.txt");
102         notExistingFileInfo = createFileSelectInfo(notExistingFile);
103 
104         zipFile = new File(getTempDir(), CanReadFileFilterTest.class.getName() + ".zip");
105         zipDir(testDir, "", zipFile);
106         zipFileObj = getZipFileObject(zipFile);
107     }
108 
109     @Test
110     public void testAcceptCannotRead() throws FileSystemException {
111 
112         assertFalse(CanReadFileFilter.CANNOT_READ.accept(writableFileInfo));
113         assertFalse(CanReadFileFilter.CANNOT_READ.accept(readOnlyFileInfo));
114         assertTrue(CanReadFileFilter.CANNOT_READ.accept(notExistingFileInfo));
115     }
116 
117     @Test
118     public void testAcceptCanRead() throws FileSystemException {
119 
120         assertTrue(CanReadFileFilter.CAN_READ.accept(writableFileInfo));
121         assertTrue(CanReadFileFilter.CAN_READ.accept(readOnlyFileInfo));
122         assertFalse(CanReadFileFilter.CAN_READ.accept(notExistingFileInfo));
123     }
124 
125     @Test
126     public void testAcceptReadOnly() throws FileSystemException {
127 
128         assertFalse(CanReadFileFilter.READ_ONLY.accept(writableFileInfo));
129         assertTrue(CanReadFileFilter.READ_ONLY.accept(readOnlyFileInfo));
130         assertFalse(CanReadFileFilter.READ_ONLY.accept(notExistingFileInfo));
131     }
132 
133     @Test
134     public void testAcceptZipFile() throws FileSystemException {
135 
136         FileObject[] files;
137 
138         // CAN_READ Filter
139         files = zipFileObj.findFiles(new FileFilterSelector(CanReadFileFilter.CAN_READ));
140         assertContains(files, READONLY, WRITABLE);
141         assertEquals(2, files.length);
142 
143         // CANNOT_READ Filter
144         files = zipFileObj.findFiles(new FileFilterSelector(CanReadFileFilter.CANNOT_READ));
145         assertTrue(files == null || files.length == 0);
146 
147         // READ_ONLY Filter
148         files = zipFileObj.findFiles(new FileFilterSelector(CanReadFileFilter.READ_ONLY));
149         assertContains(files, READONLY, WRITABLE);
150         assertEquals(2, files.length);
151     }
152 
153 }
154 // CHECKSTYLE:ON