View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.utils.io;
20  
21  import java.io.File;
22  
23  import org.junit.Test;
24  
25  import static org.junit.Assert.*;
26  
27  /**
28   * Test the {@link SelectorUtils} class.
29   */
30  @SuppressWarnings("deprecation")
31  public class SelectorUtilsTest {
32  
33      @Test(expected = NullPointerException.class)
34      public void testMatchPatternStart() {
35          SelectorUtils.matchPatternStart(null, null);
36      }
37  
38      @Test
39      public void testEmptyStrings() {
40          assertTrue(SelectorUtils.matchPatternStart("", ""));
41      }
42  
43      @Test
44      public void testRegexPrefix() throws Exception {
45          assertEquals(
46                  true,
47                  SelectorUtils.matchPatternStart(
48                          SelectorUtils.REGEX_HANDLER_PREFIX + File.separator + "aaa"
49                                  + SelectorUtils.PATTERN_HANDLER_SUFFIX,
50                          ""));
51      }
52  
53      @Test
54      public void testAntPatternStrings() {
55          assertAntDoesNotMatch("/aaa", "");
56          assertAntDoesNotMatch("\\aaa", "");
57          assertAntMatch("aaa", "");
58          assertAntMatch("/aaa/bbb", "/aaa/bbb");
59          assertAntMatch("/aaa/**", "/aaa/bbb");
60          assertAntDoesNotMatch("/aaa/**", "/ccc/bbb");
61          assertAntMatch("/aaa/**", "\\aaa\\bbb");
62          assertAntDoesNotMatch("/aaa/**", "\\ccc\\bbb");
63          assertAntDoesNotMatch("/aaa/", "\\aaa\\bbb");
64      }
65  
66      private void assertAntDoesNotMatch(String pattern, String target) {
67          assertEquals(false, SelectorUtils.matchPatternStart(wrapWithAntHandler(pattern), target));
68      }
69  
70      private void assertAntMatch(String pattern, String target) {
71          assertEquals(true, SelectorUtils.matchPatternStart(wrapWithAntHandler(pattern), target));
72      }
73  
74      private String wrapWithAntHandler(String val) {
75          return SelectorUtils.ANT_HANDLER_PREFIX + val + SelectorUtils.PATTERN_HANDLER_SUFFIX;
76      }
77  }