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 CanWriteFileFilter}.
34   */
35  // CHECKSTYLE:OFF Test code
36  public class CanWriteFileFilterTest 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(CanWriteFileFilterTest.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(), CanWriteFileFilterTest.class.getName() + ".zip");
102         zipDir(testDir, "", zipFile);
103         zipFileObj = getZipFileObject(zipFile);
104 
105     }
106 
107     @Test
108     public void testAcceptCannotWrite() throws FileSystemException {
109 
110         Assert.assertFalse(CanWriteFileFilter.CANNOT_WRITE.accept(writableFileInfo));
111         Assert.assertTrue(CanWriteFileFilter.CANNOT_WRITE.accept(readOnlyFileInfo));
112         Assert.assertFalse(CanWriteFileFilter.CANNOT_WRITE.accept(notExistingFileInfo));
113 
114     }
115 
116     @Test
117     public void testAcceptCanWrite() throws FileSystemException {
118 
119         Assert.assertTrue(CanWriteFileFilter.CAN_WRITE.accept(writableFileInfo));
120         Assert.assertFalse(CanWriteFileFilter.CAN_WRITE.accept(readOnlyFileInfo));
121         Assert.assertTrue(CanWriteFileFilter.CAN_WRITE.accept(notExistingFileInfo));
122 
123     }
124 
125     @Test
126     public void testAcceptZipFile() throws FileSystemException {
127 
128         FileObject[] files;
129 
130         // CAN_WRITE Filter
131         files = zipFileObj.findFiles(new FileFilterSelector(CanWriteFileFilter.CAN_WRITE));
132         Assert.assertTrue(files == null || files.length == 0);
133 
134         // CANNOT_WRITE Filter
135         files = zipFileObj.findFiles(new FileFilterSelector(CanWriteFileFilter.CANNOT_WRITE));
136         assertContains(files, READONLY, WRITABLE);
137         Assert.assertEquals(2, files.length);
138 
139     }
140 
141 }
142 // CHECKSTYLE:ON