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