View Javadoc
1   package org.apache.maven.plugins.checkstyle;
2   
3   import java.util.List;
4   
5   import junit.framework.TestCase;
6   
7   /*
8    * Licensed to the Apache Software Foundation (ASF) under one
9    * or more contributor license agreements.  See the NOTICE file
10   * distributed with this work for additional information
11   * regarding copyright ownership.  The ASF licenses this file
12   * to you under the Apache License, Version 2.0 (the
13   * "License"); you may not use this file except in compliance
14   * with the License.  You may obtain a copy of the License at
15   *
16   *  http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing,
19   * software distributed under the License is distributed on an
20   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21   * KIND, either express or implied.  See the License for the
22   * specific language governing permissions and limitations
23   * under the License.
24   */
25  
26  public class RuleUtilTest
27      extends TestCase
28  {
29      private static final String CHECKSTYLE_PACKAGE = "com.puppycrawl.tools.checkstyle.checks";
30  
31      public void testGetName()
32      {
33          assertEquals( "FinalParameters", RuleUtil.getName( CHECKSTYLE_PACKAGE + ".FinalParameters" ) );
34          assertEquals( "FinalParameters", RuleUtil.getName( CHECKSTYLE_PACKAGE + ".FinalParametersCheck" ) );
35          assertNull( RuleUtil.getName( (String) null ) );
36      }
37  
38      public void testGetCategory()
39      {
40          assertEquals( "misc", RuleUtil.getCategory( CHECKSTYLE_PACKAGE + ".FinalParametersCheck" ) );
41          assertEquals( "test", RuleUtil.getCategory( CHECKSTYLE_PACKAGE + ".test.FinalParametersCheck" ) );
42          assertEquals( "extension", RuleUtil.getCategory( "test.FinalParametersCheck" ) );
43          assertEquals( "extension", RuleUtil.getCategory( "copyright" ) );
44          assertNull( RuleUtil.getCategory( (String) null ) );
45      }
46  
47      public void testMatcher()
48      {
49          String[] specs = ( "misc, test, extension, Header, " + CHECKSTYLE_PACKAGE + ".test2" ).split( "," );
50          String[] eventSrcNames =
51              new String[] { CHECKSTYLE_PACKAGE + ".FinalParametersCheck",
52                  CHECKSTYLE_PACKAGE + ".test.FinalParametersCheck", "test.FinalParametersCheck",
53                  CHECKSTYLE_PACKAGE + ".whitespace.HeaderCheck", CHECKSTYLE_PACKAGE + ".test2.FinalParametersCheck" };
54  
55          List<RuleUtil.Matcher> matchers = RuleUtil.parseMatchers( specs );
56  
57          for ( int i = 0; i < matchers.size(); i++ )
58          {
59              String spec = specs[i];
60              RuleUtil.Matcher matcher = matchers.get( i );
61              for ( int j = 0; j < matchers.size(); j++ )
62              {
63                  String eventSrcName = eventSrcNames[j];
64                  assertEquals( spec + " should" + ( ( i == j ) ? " " : " not " ) + "match " + eventSrcName, i == j,
65                                matcher.match( eventSrcName ) );
66              }
67          }
68      }
69  
70      public void testMatcherWithBlankStrings()
71      {
72          String[] specs = ( "   ,,foo, " ).split( "," );
73  
74          List<RuleUtil.Matcher> matchers = RuleUtil.parseMatchers( specs );
75  
76          assertEquals( 1, matchers.size() );
77          assertTrue( matchers.get( 0 ).match( CHECKSTYLE_PACKAGE + ".foo.SomeCheck" ) );
78      }
79  }