View Javadoc
1   package org.codehaus.plexus.util;
2   
3   import java.io.File;
4   
5   import junit.framework.TestCase;
6   
7   /*
8    * Copyright The Codehaus Foundation.
9    *
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   */
22  
23  public class SelectorUtilsTest
24      extends TestCase
25  {
26      public void testMatchPath_DefaultFileSeparator()
27      {
28          String separator = File.separator;
29  
30          // Pattern and target start with file separator
31          assertTrue( SelectorUtils.matchPath( separator + "*" + separator + "a.txt",
32                                               separator + "b" + separator + "a.txt" ) );
33          // Pattern starts with file separator, target doesn't
34          assertFalse( SelectorUtils.matchPath( separator + "*" + separator + "a.txt", "b" + separator + "a.txt" ) );
35          // Pattern doesn't start with file separator, target does
36          assertFalse( SelectorUtils.matchPath( "*" + separator + "a.txt", separator + "b" + separator + "a.txt" ) );
37          // Pattern and target don't start with file separator
38          assertTrue( SelectorUtils.matchPath( "*" + separator + "a.txt", "b" + separator + "a.txt" ) );
39      }
40  
41      public void testMatchPath_UnixFileSeparator()
42      {
43          String separator = "/";
44  
45          // Pattern and target start with file separator
46          assertTrue( SelectorUtils.matchPath( separator + "*" + separator + "a.txt",
47                                               separator + "b" + separator + "a.txt", separator, false ) );
48          // Pattern starts with file separator, target doesn't
49          assertFalse( SelectorUtils.matchPath( separator + "*" + separator + "a.txt", "b" + separator + "a.txt",
50                                                separator, false ) );
51          // Pattern doesn't start with file separator, target does
52          assertFalse( SelectorUtils.matchPath( "*" + separator + "a.txt", separator + "b" + separator + "a.txt",
53                                                separator, false ) );
54          // Pattern and target don't start with file separator
55          assertTrue( SelectorUtils.matchPath( "*" + separator + "a.txt", "b" + separator + "a.txt", separator, false ) );
56      }
57  
58      public void testMatchPath_WindowsFileSeparator()
59      {
60          String separator = "\\";
61  
62          // Pattern and target start with file separator
63          assertTrue( SelectorUtils.matchPath( separator + "*" + separator + "a.txt",
64                                               separator + "b" + separator + "a.txt", separator, false ) );
65          // Pattern starts with file separator, target doesn't
66          assertFalse( SelectorUtils.matchPath( separator + "*" + separator + "a.txt", "b" + separator + "a.txt",
67                                                separator, false ) );
68          // Pattern doesn't start with file separator, target does
69          assertFalse( SelectorUtils.matchPath( "*" + separator + "a.txt", separator + "b" + separator + "a.txt",
70                                                separator, false ) );
71          // Pattern and target don't start with file separator
72          assertTrue( SelectorUtils.matchPath( "*" + separator + "a.txt", "b" + separator + "a.txt", separator, false ) );
73      }
74  }