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  import java.nio.file.Files;
22  
23  import org.apache.commons.io.FileUtils;
24  import org.apache.commons.vfs2.FileFilter;
25  import org.apache.commons.vfs2.FileFilterSelector;
26  import org.apache.commons.vfs2.FileObject;
27  import org.apache.commons.vfs2.FileSelectInfo;
28  import org.apache.commons.vfs2.FileSystemException;
29  import org.junit.AfterClass;
30  import org.junit.Assert;
31  import org.junit.Assume;
32  import org.junit.BeforeClass;
33  import org.junit.Test;
34  
35  /**
36   * Test for {@link SymbolicLinkFileFilter}.
37   * <p>
38   * On Windows, in order for this test to pass, you MUST run the VM with admin rights.
39   * </p>
40   * <p>
41   * To enable this test set the system property "SymbolicLinkFileFilterTest.Enable" to "true".
42   *
43   * <p>
44   * To run only this test with Maven:
45   * </p>
46   *
47   * <pre>
48   * mvn test -Dtest=SymbolicLinkFileFilterTest -pl commons-vfs2 -DSymbolicLinkFileFilterTest.Enable=true
49   * </pre>
50   */
51  // CHECKSTYLE:OFF Test code
52  public class SymbolicLinkFileFilterTest extends BaseFilterTest {
53  
54      private static File testDir;
55  
56      private static File targetFile;
57  
58      private static FileSelectInfo targetFileInfo;
59  
60      private static File linkFile;
61  
62      private static FileSelectInfo linkFileInfo;
63  
64      private static File notExistingFile;
65  
66      private static FileSelectInfo notExistingFileInfo;
67  
68      private static File zipFile;
69  
70      private static FileObject zipFileObject;
71  
72      @AfterClass
73      public static void afterClass() throws IOException {
74          targetFile = null;
75          targetFileInfo = null;
76          linkFile = null;
77          linkFileInfo = null;
78          notExistingFile = null;
79          notExistingFileInfo = null;
80          if (zipFileObject != null) {
81              zipFileObject.close();
82          }
83          if (zipFile != null) {
84              FileUtils.deleteQuietly(zipFile);
85              zipFile = null;
86          }
87          if (testDir != null) {
88              FileUtils.deleteDirectory(testDir);
89              testDir = null;
90          }
91      }
92  
93      @BeforeClass
94      public static void beforeClass() throws IOException {
95          Assume.assumeTrue(Boolean.getBoolean(SymbolicLinkFileFilterTest.class.getSimpleName() + ".Enable"));
96  
97          testDir = getTestDir(SymbolicLinkFileFilterTest.class.getName());
98          testDir.mkdir();
99  
100         linkFile = new File(testDir, "visible.txt");
101         linkFileInfo = createFileSelectInfo(linkFile);
102 
103         targetFile = new File(testDir, "symbolic.txt");
104         Files.deleteIfExists(targetFile.toPath());
105         FileUtils.touch(targetFile);
106         Files.createSymbolicLink(linkFile.toPath(), targetFile.toPath());
107         targetFileInfo = createFileSelectInfo(targetFile);
108 
109         notExistingFile = new File(testDir, "not-existing-file.txt");
110         notExistingFileInfo = createFileSelectInfo(notExistingFile);
111 
112         // Zip the test directory
113         zipFile = new File(getTempDir(), SymbolicLinkFileFilterTest.class.getName() + ".zip");
114         zipDir(testDir, "", zipFile);
115         zipFileObject = getZipFileObject(zipFile);
116     }
117 
118     @Test
119     public void testAcceptActual() throws FileSystemException {
120         final FileFilter testee = SymbolicLinkFileFilter.ACTUAL;
121         Assert.assertTrue(targetFileInfo.getBaseFolder().exists());
122         Assert.assertTrue(targetFileInfo.getFile().exists());
123         Assert.assertTrue(targetFileInfo.toString(), testee.accept(targetFileInfo));
124         Assert.assertTrue(notExistingFileInfo.toString(), testee.accept(notExistingFileInfo));
125     }
126 
127     @Test
128     public void testAcceptSymbolic() throws FileSystemException {
129         final FileFilter testee = SymbolicLinkFileFilter.SYMBOLIC;
130         Assert.assertTrue(linkFileInfo.toString(), testee.accept(linkFileInfo));
131         Assert.assertFalse(notExistingFileInfo.toString(), testee.accept(notExistingFileInfo));
132     }
133 
134     @Test
135     public void testZipFile() throws FileSystemException {
136         final FileObject[] files = zipFileObject.findFiles(new FileFilterSelector(SymbolicLinkFileFilter.SYMBOLIC));
137         Assert.assertEquals(0, files.length);
138     }
139 
140 }
141 // CHECKSTYLE:ON