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