View Javadoc
1   package org.codehaus.plexus.util;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  
21  import org.junit.jupiter.api.Test;
22  
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  /**
27   * <p>SelectorUtilsTest class.</p>
28   *
29   * @author herve
30   * @since 3.4.0
31   */
32  public class SelectorUtilsTest {
33      /**
34       * <p>testMatchPath_DefaultFileSeparator.</p>
35       */
36      @Test
37      public void testMatchPath_DefaultFileSeparator() {
38          String separator = File.separator;
39  
40          // Pattern and target start with file separator
41          assertTrue(
42                  SelectorUtils.matchPath(separator + "*" + separator + "a.txt", separator + "b" + separator + "a.txt"));
43          // Pattern starts with file separator, target doesn't
44          assertFalse(SelectorUtils.matchPath(separator + "*" + separator + "a.txt", "b" + separator + "a.txt"));
45          // Pattern doesn't start with file separator, target does
46          assertFalse(SelectorUtils.matchPath("*" + separator + "a.txt", separator + "b" + separator + "a.txt"));
47          // Pattern and target don't start with file separator
48          assertTrue(SelectorUtils.matchPath("*" + separator + "a.txt", "b" + separator + "a.txt"));
49      }
50  
51      /**
52       * <p>testMatchPath_UnixFileSeparator.</p>
53       */
54      @Test
55      public void testMatchPath_UnixFileSeparator() {
56          String separator = "/";
57  
58          // Pattern and target start with file separator
59          assertTrue(SelectorUtils.matchPath(
60                  separator + "*" + separator + "a.txt", separator + "b" + separator + "a.txt", separator, false));
61          // Pattern starts with file separator, target doesn't
62          assertFalse(SelectorUtils.matchPath(
63                  separator + "*" + separator + "a.txt", "b" + separator + "a.txt", separator, false));
64          // Pattern doesn't start with file separator, target does
65          assertFalse(SelectorUtils.matchPath(
66                  "*" + separator + "a.txt", separator + "b" + separator + "a.txt", separator, false));
67          // Pattern and target don't start with file separator
68          assertTrue(SelectorUtils.matchPath("*" + separator + "a.txt", "b" + separator + "a.txt", separator, false));
69      }
70  
71      /**
72       * <p>testMatchPath_WindowsFileSeparator.</p>
73       */
74      @Test
75      public void testMatchPath_WindowsFileSeparator() {
76          String separator = "\\";
77  
78          // Pattern and target start with file separator
79          assertTrue(SelectorUtils.matchPath(
80                  separator + "*" + separator + "a.txt", separator + "b" + separator + "a.txt", separator, false));
81          // Pattern starts with file separator, target doesn't
82          assertFalse(SelectorUtils.matchPath(
83                  separator + "*" + separator + "a.txt", "b" + separator + "a.txt", separator, false));
84          // Pattern doesn't start with file separator, target does
85          assertFalse(SelectorUtils.matchPath(
86                  "*" + separator + "a.txt", separator + "b" + separator + "a.txt", separator, false));
87          // Pattern and target don't start with file separator
88          assertTrue(SelectorUtils.matchPath("*" + separator + "a.txt", "b" + separator + "a.txt", separator, false));
89      }
90  
91      @org.junit.jupiter.api.Test
92      public void testPatternMatchSingleWildcardPosix() {
93          assertFalse(SelectorUtils.matchPath("/com/test/*", "/com/test/test/hallo"));
94      }
95  
96      @Test
97      public void testPatternMatchDoubleWildcardCaseInsensitivePosix() {
98          assertTrue(SelectorUtils.matchPath("/com/test/**", "/com/test/test/hallo"));
99      }
100 
101     @Test
102     public void testPatternMatchDoubleWildcardPosix() {
103         assertTrue(SelectorUtils.matchPath("/com/test/**", "/com/test/test/hallo"));
104     }
105 
106     @org.junit.jupiter.api.Test
107     public void testPatternMatchSingleWildcardWindows() {
108         assertFalse(SelectorUtils.matchPath("D:\\com\\test\\*", "D:\\com\\test\\test\\hallo"));
109 
110         assertFalse(SelectorUtils.matchPath("D:/com/test/*", "D:/com/test/test/hallo"));
111     }
112 
113     @Test
114     public void testPatternMatchDoubleWildcardWindows() {
115         assertTrue(SelectorUtils.matchPath("D:\\com\\test\\**", "D:\\com\\test\\test\\hallo"));
116 
117         assertTrue(SelectorUtils.matchPath("D:\\com\\test\\**", "D:/com/test/test/hallo"));
118     }
119 }