View Javadoc
1   package org.apache.maven.plugin.checkstyle;
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 junit.framework.TestCase;
23  
24  public class RuleUtilTest
25      extends TestCase
26  {
27      private static final String CHECKSTYLE_PACKAGE = "com.puppycrawl.tools.checkstyle.checks";
28  
29      public void testGetName()
30      {
31          assertEquals( "FinalParameters", RuleUtil.getName( CHECKSTYLE_PACKAGE + ".FinalParameters" ) );
32          assertEquals( "FinalParameters", RuleUtil.getName( CHECKSTYLE_PACKAGE + ".FinalParametersCheck" ) );
33          assertNull( RuleUtil.getName( (String) null ) );
34      }
35  
36      public void testGetCategory()
37      {
38          assertEquals( "misc", RuleUtil.getCategory( CHECKSTYLE_PACKAGE + ".FinalParametersCheck" ) );
39          assertEquals( "test", RuleUtil.getCategory( CHECKSTYLE_PACKAGE + ".test.FinalParametersCheck" ) );
40          assertEquals( "extension", RuleUtil.getCategory( "test.FinalParametersCheck" ) );
41          assertNull( RuleUtil.getCategory( (String) null ) );
42      }
43  
44      public void testMatcher()
45      {
46          String[] specs = ( "misc, test, extension, Header, " + CHECKSTYLE_PACKAGE + ".test2" ).split( "," );
47          String[] eventSrcNames =
48              new String[] { CHECKSTYLE_PACKAGE + ".FinalParametersCheck",
49                  CHECKSTYLE_PACKAGE + ".test.FinalParametersCheck", "test.FinalParametersCheck",
50                  CHECKSTYLE_PACKAGE + ".whitespace.HeaderCheck", CHECKSTYLE_PACKAGE + ".test2.FinalParametersCheck" };
51  
52          RuleUtil.Matcher[] matchers = RuleUtil.parseMatchers( specs );
53  
54          for ( int i = 0; i < matchers.length; i++ )
55          {
56              String spec = specs[i];
57              RuleUtil.Matcher matcher = matchers[i];
58              for ( int j = 0; j < matchers.length; j++ )
59              {
60                  String eventSrcName = eventSrcNames[j];
61                  assertEquals( spec + " should" + ( ( i == j ) ? " " : " not " ) + "match " + eventSrcName, i == j,
62                                matcher.match( eventSrcName ) );
63              }
64          }
65      }
66  }