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.Assert.fail;
20  
21  import java.io.File;
22  import java.util.regex.Pattern;
23  
24  import org.apache.commons.vfs2.FileFilter;
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  /**
29   * Test for {@link RegexFileFilter}.
30   */
31  // CHECKSTYLE:OFF Test code
32  public class RegexFileFilterTestCase extends BaseFilterTest {
33  
34      @Test
35      public void testPatternNullArgConstruction() {
36          try {
37              new RegexFileFilter((Pattern) null);
38              fail();
39          } catch (final IllegalArgumentException ex) {
40              Assert.assertEquals(RegexFileFilter.PATTERN_IS_MISSING, ex.getMessage());
41          }
42      }
43  
44      @Test
45      public void testRegex() throws Exception {
46  
47          FileFilter filter;
48  
49          filter = new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$");
50          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("Test.java"))));
51          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test-10.java"))));
52          Assert.assertFalse(filter.accept(createFileSelectInfo(new File("test-.java"))));
53  
54          filter = new RegexFileFilter("^[Tt]est.java$");
55          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("Test.java"))));
56          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test.java"))));
57          Assert.assertFalse(filter.accept(createFileSelectInfo(new File("tEST.java"))));
58  
59          filter = new RegexFileFilter(Pattern.compile("^test.java$", Pattern.CASE_INSENSITIVE));
60          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("Test.java"))));
61          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test.java"))));
62          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("tEST.java"))));
63  
64          filter = new RegexFileFilter("^test.java$", Pattern.CASE_INSENSITIVE);
65          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("Test.java"))));
66          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test.java"))));
67          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("tEST.java"))));
68  
69          filter = new RegexFileFilter("^test.java$", IOCase.INSENSITIVE);
70          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("Test.java"))));
71          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("test.java"))));
72          Assert.assertTrue(filter.accept(createFileSelectInfo(new File("tEST.java"))));
73  
74      }
75  
76      @Test
77      public void testStringIOCaseNullArgConstruction() {
78          try {
79              new RegexFileFilter((String) null, IOCase.INSENSITIVE);
80              fail();
81          } catch (final IllegalArgumentException ex) {
82              Assert.assertEquals(RegexFileFilter.PATTERN_IS_MISSING, ex.getMessage());
83          }
84      }
85  
86      @Test
87      public void testStringNullArgConstruction() {
88          try {
89              new RegexFileFilter((String) null);
90              fail();
91          } catch (final IllegalArgumentException ex) {
92              Assert.assertEquals(RegexFileFilter.PATTERN_IS_MISSING, ex.getMessage());
93          }
94      }
95  
96      @Test
97      public void testStringPatternNullArgConstruction() {
98          try {
99              new RegexFileFilter((String) null, Pattern.CASE_INSENSITIVE);
100             fail();
101         } catch (final IllegalArgumentException ex) {
102             Assert.assertEquals(RegexFileFilter.PATTERN_IS_MISSING, ex.getMessage());
103         }
104     }
105 
106 }
107 // CHECKSTYLE:ON