View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.metrics;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.metrics.ClassFanOutComplexityCheck.MSG_KEY;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.fail;
25  
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
34  
35  public class ClassFanOutComplexityCheckTest extends AbstractModuleTestSupport {
36  
37      @Override
38      protected String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/checks/metrics/classfanoutcomplexity";
40      }
41  
42      @Test
43      public void test() throws Exception {
44          final DefaultConfiguration checkConfig =
45              createModuleConfig(ClassFanOutComplexityCheck.class);
46  
47          checkConfig.addAttribute("max", "0");
48  
49          final String[] expected = {
50              "6:1: " + getCheckMessage(MSG_KEY, 3, 0),
51              "38:1: " + getCheckMessage(MSG_KEY, 1, 0),
52          };
53  
54          verify(checkConfig, getPath("InputClassFanOutComplexity.java"), expected);
55      }
56  
57      @Test
58      public void testExcludedPackagesDirectPackages() throws Exception {
59          final DefaultConfiguration checkConfig =
60              createModuleConfig(ClassFanOutComplexityCheck.class);
61  
62          checkConfig.addAttribute("max", "0");
63          checkConfig.addAttribute("excludedPackages",
64              "com.puppycrawl.tools.checkstyle.checks.metrics.classfanoutcomplexity.inputs.c,"
65                  + "com.puppycrawl.tools.checkstyle.checks.metrics.classfanoutcomplexity.inputs.b");
66  
67          final String[] expected = {
68              "8:1: " + getCheckMessage(MSG_KEY, 2, 0),
69          };
70  
71          verify(checkConfig,
72              getPath("InputClassFanOutComplexityExcludedPackagesDirectPackages.java"), expected);
73      }
74  
75      @Test
76      public void testExcludedPackagesCommonPackages() throws Exception {
77          final DefaultConfiguration checkConfig =
78              createModuleConfig(ClassFanOutComplexityCheck.class);
79  
80          checkConfig.addAttribute("max", "0");
81          checkConfig.addAttribute("excludedPackages",
82              "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a");
83  
84          final String[] expected = {
85              "8:1: " + getCheckMessage(MSG_KEY, 2, 0),
86              "12:5: " + getCheckMessage(MSG_KEY, 2, 0),
87              "18:1: " + getCheckMessage(MSG_KEY, 1, 0),
88          };
89          verify(checkConfig,
90              getPath("InputClassFanOutComplexityExcludedPackagesCommonPackage.java"), expected);
91      }
92  
93      @Test
94      public void testExcludedPackagesCommonPackagesWithEndingDot() throws Exception {
95          final DefaultConfiguration checkConfig =
96              createModuleConfig(ClassFanOutComplexityCheck.class);
97  
98          checkConfig.addAttribute("max", "0");
99          checkConfig.addAttribute("excludedPackages",
100             "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.");
101 
102         try {
103             createChecker(checkConfig);
104             fail("exception expected");
105         }
106         catch (CheckstyleException ex) {
107             final String messageStart =
108                 "cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
109                     + "Cannot set property 'excludedPackages' to "
110                     + "'com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.' in module "
111                     + "com.puppycrawl.tools.checkstyle.checks.metrics."
112                     + "ClassFanOutComplexityCheck";
113 
114             assertTrue("Invalid exception message, should start with: " + messageStart,
115                 ex.getMessage().startsWith(messageStart));
116         }
117     }
118 
119     @Test
120     public void testExcludedPackagesAllIgnored() throws Exception {
121         final DefaultConfiguration checkConfig =
122             createModuleConfig(ClassFanOutComplexityCheck.class);
123 
124         checkConfig.addAttribute("max", "0");
125         checkConfig.addAttribute("excludedPackages",
126             "com.puppycrawl.tools.checkstyle.checks.metrics.classfanoutcomplexity.inputs.a.aa,"
127                 + "com.puppycrawl.tools.checkstyle.checks.metrics.classfanoutcomplexity."
128                     + "inputs.a.ab,"
129                 + "com.puppycrawl.tools.checkstyle.checks.metrics.classfanoutcomplexity.inputs.b,"
130                 + "com.puppycrawl.tools.checkstyle.checks.metrics.classfanoutcomplexity.inputs.c");
131 
132         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
133         verify(checkConfig,
134             getPath("InputClassFanOutComplexityExcludedPackagesAllIgnored.java"), expected);
135     }
136 
137     @Test
138     public void test15() throws Exception {
139         final DefaultConfiguration checkConfig =
140             createModuleConfig(ClassFanOutComplexityCheck.class);
141 
142         checkConfig.addAttribute("max", "0");
143 
144         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
145 
146         verify(checkConfig, getPath("InputClassFanOutComplexity15Extensions.java"), expected);
147     }
148 
149     @Test
150     public void testDefaultConfiguration() throws Exception {
151         final DefaultConfiguration checkConfig =
152             createModuleConfig(ClassFanOutComplexityCheck.class);
153 
154         createChecker(checkConfig);
155         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
156         verify(checkConfig, getPath("InputClassFanOutComplexity.java"), expected);
157     }
158 
159     @Test
160     public void testGetAcceptableTokens() {
161         final ClassFanOutComplexityCheck classFanOutComplexityCheckObj =
162             new ClassFanOutComplexityCheck();
163         final int[] actual = classFanOutComplexityCheckObj.getAcceptableTokens();
164         final int[] expected = {
165             TokenTypes.PACKAGE_DEF,
166             TokenTypes.IMPORT,
167             TokenTypes.CLASS_DEF,
168             TokenTypes.INTERFACE_DEF,
169             TokenTypes.ENUM_DEF,
170             TokenTypes.TYPE,
171             TokenTypes.LITERAL_NEW,
172             TokenTypes.LITERAL_THROWS,
173             TokenTypes.ANNOTATION_DEF,
174         };
175         Assert.assertNotNull("Acceptable tokens should not be null", actual);
176         Assert.assertArrayEquals("Invalid acceptable tokens", expected, actual);
177     }
178 
179     @Test
180     public void testRegularExpression() throws Exception {
181         final DefaultConfiguration checkConfig =
182                 createModuleConfig(ClassFanOutComplexityCheck.class);
183 
184         checkConfig.addAttribute("max", "0");
185         checkConfig.addAttribute("excludeClassesRegexps", "^Inner.*");
186 
187         final String[] expected = {
188             "6:1: " + getCheckMessage(MSG_KEY, 2, 0),
189             "38:1: " + getCheckMessage(MSG_KEY, 1, 0),
190         };
191 
192         verify(checkConfig, getPath("InputClassFanOutComplexity.java"), expected);
193     }
194 
195     @Test
196     public void testEmptyRegularExpression() throws Exception {
197         final DefaultConfiguration checkConfig =
198                 createModuleConfig(ClassFanOutComplexityCheck.class);
199 
200         checkConfig.addAttribute("max", "0");
201         checkConfig.addAttribute("excludeClassesRegexps", "");
202 
203         final String[] expected = {
204             "6:1: " + getCheckMessage(MSG_KEY, 3, 0),
205             "38:1: " + getCheckMessage(MSG_KEY, 1, 0),
206         };
207 
208         verify(checkConfig, getPath("InputClassFanOutComplexity.java"), expected);
209     }
210 
211     @Test
212     public void testWithMultiDimensionalArray() throws Exception {
213         final DefaultConfiguration moduleConfig =
214                 createModuleConfig(ClassFanOutComplexityCheck.class);
215         moduleConfig.addAttribute("max", "0");
216 
217         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
218         verify(moduleConfig,
219                 getPath("InputClassFanOutComplexityMultiDimensionalArray.java"), expected);
220     }
221 
222     @Test
223     public void testPackageName() throws Exception {
224         final DefaultConfiguration moduleConfig =
225                 createModuleConfig(ClassFanOutComplexityCheck.class);
226         moduleConfig.addAttribute("max", "0");
227 
228         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
229         verify(moduleConfig,
230                 getPath("InputClassFanOutComplexityPackageName.java"), expected);
231     }
232 
233 }