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