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.io.filefilter;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.io.ObjectOutputStream;
27  import java.io.Serializable;
28  import java.nio.file.FileVisitResult;
29  import java.nio.file.Path;
30  import java.nio.file.Paths;
31  import java.util.function.Function;
32  import java.util.regex.Pattern;
33  
34  import org.apache.commons.io.IOCase;
35  import org.apache.commons.io.output.ByteArrayOutputStream;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * Tests {@link RegexFileFilter}.
40   */
41  public class RegexFileFilterTest {
42  
43      public void assertFiltering(final IOFileFilter filter, final File file, final boolean expected) {
44          // Note. This only tests the (File, String) version if the parent of
45          // the File passed in is not null
46          assertEquals(expected, filter.accept(file), "Filter(File) " + filter.getClass().getName() + " not " + expected + " for " + file);
47  
48          if (file != null && file.getParentFile() != null) {
49              assertEquals(expected, filter.accept(file.getParentFile(), file.getName()),
50                      "Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for " + file);
51              assertEquals(expected, filter.matches(file.toPath()), "Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for " + file);
52          } else if (file == null) {
53              assertEquals(expected, filter.accept(file), "Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for null");
54              assertEquals(expected, filter.matches(null), "Filter(File, String) " + filter.getClass().getName() + " not " + expected + " for null");
55          }
56          // Just don't blow up
57          assertNotNull(filter.toString());
58      }
59  
60      public void assertFiltering(final IOFileFilter filter, final Path path, final boolean expected) {
61          // Note. This only tests the (Path, Path) version if the parent of
62          // the Path passed in is not null
63          final FileVisitResult expectedFileVisitResult = AbstractFileFilter.toDefaultFileVisitResult(expected);
64          assertEquals(expectedFileVisitResult, filter.accept(path, null),
65                  "Filter(Path) " + filter.getClass().getName() + " not " + expectedFileVisitResult + " for " + path);
66          assertEquals(expectedFileVisitResult != FileVisitResult.TERMINATE, filter.matches(path),
67                  "Filter(Path) " + filter.getClass().getName() + " not " + expectedFileVisitResult + " for " + path);
68  
69          if (path != null && path.getParent() != null) {
70              assertEquals(expectedFileVisitResult, filter.accept(path, null),
71                      "Filter(Path, Path) " + filter.getClass().getName() + " not " + expectedFileVisitResult + " for " + path);
72          } else if (path == null) {
73              assertEquals(expectedFileVisitResult, filter.accept(path, null),
74                      "Filter(Path, Path) " + filter.getClass().getName() + " not " + expectedFileVisitResult + " for null");
75          }
76          // Just don't blow up
77          assertNotNull(filter.toString());
78      }
79  
80      private RegexFileFilter assertSerializable(final RegexFileFilter serializable) throws IOException {
81          try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
82              try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
83                  oos.writeObject(serializable);
84              }
85              baos.flush();
86              assertTrue(baos.toByteArray().length > 0);
87          }
88          return serializable;
89      }
90  
91      @Test
92      public void testRegex() throws IOException {
93          RegexFileFilter filter = new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$");
94          assertSerializable(filter);
95          assertFiltering(filter, new File("Test.java"), true);
96          assertFiltering(filter, new File("test-10.java"), true);
97          assertFiltering(filter, new File("test-.java"), false);
98          //
99          assertFiltering(filter, new File("Test.java").toPath(), true);
100         assertFiltering(filter, new File("test-10.java").toPath(), true);
101         assertFiltering(filter, new File("test-.java").toPath(), false);
102 
103         filter = new RegexFileFilter("^[Tt]est.java$");
104         assertSerializable(filter);
105         assertFiltering(filter, new File("Test.java"), true);
106         assertFiltering(filter, new File("test.java"), true);
107         assertFiltering(filter, new File("tEST.java"), false);
108         //
109         assertFiltering(filter, new File("Test.java").toPath(), true);
110         assertFiltering(filter, new File("test.java").toPath(), true);
111         assertFiltering(filter, new File("tEST.java").toPath(), false);
112 
113         filter = new RegexFileFilter(Pattern.compile("^test.java$", Pattern.CASE_INSENSITIVE));
114         assertSerializable(filter);
115         assertFiltering(filter, new File("Test.java"), true);
116         assertFiltering(filter, new File("test.java"), true);
117         assertFiltering(filter, new File("tEST.java"), true);
118         //
119         assertFiltering(filter, new File("Test.java").toPath(), true);
120         assertFiltering(filter, new File("test.java").toPath(), true);
121         assertFiltering(filter, new File("tEST.java").toPath(), true);
122 
123         filter = new RegexFileFilter("^test.java$", Pattern.CASE_INSENSITIVE);
124         assertSerializable(filter);
125         assertFiltering(filter, new File("Test.java"), true);
126         assertFiltering(filter, new File("test.java"), true);
127         assertFiltering(filter, new File("tEST.java"), true);
128         //
129         assertFiltering(filter, new File("Test.java").toPath(), true);
130         assertFiltering(filter, new File("test.java").toPath(), true);
131         assertFiltering(filter, new File("tEST.java").toPath(), true);
132 
133         filter = new RegexFileFilter("^test.java$", IOCase.INSENSITIVE);
134         assertSerializable(filter);
135         assertFiltering(filter, new File("Test.java"), true);
136         assertFiltering(filter, new File("test.java"), true);
137         assertFiltering(filter, new File("tEST.java"), true);
138         //
139         assertFiltering(filter, new File("Test.java").toPath(), true);
140         assertFiltering(filter, new File("test.java").toPath(), true);
141         assertFiltering(filter, new File("tEST.java").toPath(), true);
142     }
143 
144     @Test
145     public void testRegexEdgeCases() {
146         assertThrows(NullPointerException.class, () -> assertSerializable(new RegexFileFilter((String) null)));
147         assertThrows(NullPointerException.class, () -> assertSerializable(new RegexFileFilter(null, Pattern.CASE_INSENSITIVE)));
148         assertThrows(NullPointerException.class, () -> assertSerializable(new RegexFileFilter(null, IOCase.INSENSITIVE)));
149         assertThrows(NullPointerException.class, () -> assertSerializable(new RegexFileFilter((java.util.regex.Pattern) null)));
150     }
151 
152     /**
153      * Tests https://issues.apache.org/jira/browse/IO-733.
154      *
155      * @throws IOException
156      */
157     @SuppressWarnings("unchecked")
158     @Test
159     public void testRegexFileNameOnly() throws IOException {
160         final Path path = Paths.get("folder", "Foo.java");
161         final String patternStr = "Foo.*";
162         assertFiltering(assertSerializable(new RegexFileFilter(patternStr)), path, true);
163         assertFiltering(assertSerializable(new RegexFileFilter(Pattern.compile(patternStr), (Function<Path, String> & Serializable) Path::toString)), path,
164                 false);
165         //
166         assertFiltering(new RegexFileFilter(Pattern.compile(patternStr), (Function<Path, String> & Serializable) null), path, false);
167         assertFiltering(new RegexFileFilter(Pattern.compile(patternStr), (Function<Path, String> & Serializable) p -> null), path, false);
168         //
169         assertFiltering(assertSerializable(new RegexFileFilter(Pattern.compile(patternStr), (Function<Path, String> & Serializable) null)), path, false);
170         assertFiltering(assertSerializable(new RegexFileFilter(Pattern.compile(patternStr), (Function<Path, String> & Serializable) p -> null)), path, false);
171     }
172 
173 }