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