View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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      @Override
37      protected String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/metrics/classfanoutcomplexity";
39      }
40  
41      @Test
42      public void test() throws Exception {
43          final DefaultConfiguration checkConfig =
44              createModuleConfig(ClassFanOutComplexityCheck.class);
45  
46          checkConfig.addAttribute("max", "0");
47  
48          final String[] expected = {
49              "6:1: " + getCheckMessage(MSG_KEY, 3, 0),
50              "38:1: " + getCheckMessage(MSG_KEY, 1, 0),
51          };
52  
53          verify(checkConfig, getPath("InputClassFanOutComplexity.java"), expected);
54      }
55  
56      @Test
57      public void testExcludedPackagesDirectPackages() throws Exception {
58          final DefaultConfiguration checkConfig =
59              createModuleConfig(ClassFanOutComplexityCheck.class);
60  
61          checkConfig.addAttribute("max", "0");
62          checkConfig.addAttribute("excludedPackages",
63              "com.puppycrawl.tools.checkstyle.checks.metrics.classfanoutcomplexity.inputs.c,"
64                  + "com.puppycrawl.tools.checkstyle.checks.metrics.classfanoutcomplexity.inputs.b");
65  
66          final String[] expected = {
67              "8:1: " + getCheckMessage(MSG_KEY, 2, 0),
68          };
69  
70          verify(checkConfig,
71              getPath("InputClassFanOutComplexityExcludedPackagesDirectPackages.java"), expected);
72      }
73  
74      @Test
75      public void testExcludedPackagesCommonPackages() throws Exception {
76          final DefaultConfiguration checkConfig =
77              createModuleConfig(ClassFanOutComplexityCheck.class);
78  
79          checkConfig.addAttribute("max", "0");
80          checkConfig.addAttribute("excludedPackages",
81              "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a");
82  
83          final String[] expected = {
84              "8:1: " + getCheckMessage(MSG_KEY, 2, 0),
85              "12:5: " + getCheckMessage(MSG_KEY, 2, 0),
86              "18:1: " + getCheckMessage(MSG_KEY, 1, 0),
87          };
88          verify(checkConfig,
89              getPath("InputClassFanOutComplexityExcludedPackagesCommonPackage.java"), expected);
90      }
91  
92      @Test
93      public void testExcludedPackagesCommonPackagesWithEndingDot() throws Exception {
94          final DefaultConfiguration checkConfig =
95              createModuleConfig(ClassFanOutComplexityCheck.class);
96  
97          checkConfig.addAttribute("max", "0");
98          checkConfig.addAttribute("excludedPackages",
99              "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.");
100 
101         try {
102             createChecker(checkConfig);
103             fail("exception expected");
104         }
105         catch (CheckstyleException ex) {
106             final String messageStart =
107                 "cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
108                     + "Cannot set property 'excludedPackages' to "
109                     + "'com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.' in module "
110                     + "com.puppycrawl.tools.checkstyle.checks.metrics."
111                     + "ClassFanOutComplexityCheck";
112 
113             assertTrue("Invalid exception message, should start with: " + messageStart,
114                 ex.getMessage().startsWith(messageStart));
115         }
116     }
117 
118     @Test
119     public void testExcludedPackagesAllIgnored() throws Exception {
120         final DefaultConfiguration checkConfig =
121             createModuleConfig(ClassFanOutComplexityCheck.class);
122 
123         checkConfig.addAttribute("max", "0");
124         checkConfig.addAttribute("excludedPackages",
125             "com.puppycrawl.tools.checkstyle.checks.metrics.classfanoutcomplexity.inputs.a.aa,"
126                 + "com.puppycrawl.tools.checkstyle.checks.metrics.classfanoutcomplexity."
127                     + "inputs.a.ab,"
128                 + "com.puppycrawl.tools.checkstyle.checks.metrics.classfanoutcomplexity.inputs.b,"
129                 + "com.puppycrawl.tools.checkstyle.checks.metrics.classfanoutcomplexity.inputs.c");
130 
131         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
132         verify(checkConfig,
133             getPath("InputClassFanOutComplexityExcludedPackagesAllIgnored.java"), expected);
134     }
135 
136     @Test
137     public void test15() throws Exception {
138         final DefaultConfiguration checkConfig =
139             createModuleConfig(ClassFanOutComplexityCheck.class);
140 
141         checkConfig.addAttribute("max", "0");
142 
143         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
144 
145         verify(checkConfig, getPath("InputClassFanOutComplexity15Extensions.java"), expected);
146     }
147 
148     @Test
149     public void testDefaultConfiguration() throws Exception {
150         final DefaultConfiguration checkConfig =
151             createModuleConfig(ClassFanOutComplexityCheck.class);
152 
153         createChecker(checkConfig);
154         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
155         verify(checkConfig, getPath("InputClassFanOutComplexity.java"), expected);
156     }
157 
158     @Test
159     public void testGetAcceptableTokens() {
160         final ClassFanOutComplexityCheck classFanOutComplexityCheckObj =
161             new ClassFanOutComplexityCheck();
162         final int[] actual = classFanOutComplexityCheckObj.getAcceptableTokens();
163         final int[] expected = {
164             TokenTypes.PACKAGE_DEF,
165             TokenTypes.IMPORT,
166             TokenTypes.CLASS_DEF,
167             TokenTypes.INTERFACE_DEF,
168             TokenTypes.ENUM_DEF,
169             TokenTypes.TYPE,
170             TokenTypes.LITERAL_NEW,
171             TokenTypes.LITERAL_THROWS,
172             TokenTypes.ANNOTATION_DEF,
173         };
174         Assert.assertNotNull("Acceptable tokens should not be null", actual);
175         Assert.assertArrayEquals("Invalid acceptable tokens", expected, actual);
176     }
177 
178     @Test
179     public void testRegularExpression() throws Exception {
180         final DefaultConfiguration checkConfig =
181                 createModuleConfig(ClassFanOutComplexityCheck.class);
182 
183         checkConfig.addAttribute("max", "0");
184         checkConfig.addAttribute("excludeClassesRegexps", "^Inner.*");
185 
186         final String[] expected = {
187             "6:1: " + getCheckMessage(MSG_KEY, 2, 0),
188             "38:1: " + getCheckMessage(MSG_KEY, 1, 0),
189         };
190 
191         verify(checkConfig, getPath("InputClassFanOutComplexity.java"), expected);
192     }
193 
194     @Test
195     public void testEmptyRegularExpression() throws Exception {
196         final DefaultConfiguration checkConfig =
197                 createModuleConfig(ClassFanOutComplexityCheck.class);
198 
199         checkConfig.addAttribute("max", "0");
200         checkConfig.addAttribute("excludeClassesRegexps", "");
201 
202         final String[] expected = {
203             "6:1: " + getCheckMessage(MSG_KEY, 3, 0),
204             "38:1: " + getCheckMessage(MSG_KEY, 1, 0),
205         };
206 
207         verify(checkConfig, getPath("InputClassFanOutComplexity.java"), expected);
208     }
209 
210     @Test
211     public void testWithMultiDimensionalArray() throws Exception {
212         final DefaultConfiguration moduleConfig =
213                 createModuleConfig(ClassFanOutComplexityCheck.class);
214         moduleConfig.addAttribute("max", "0");
215 
216         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
217         verify(moduleConfig,
218                 getPath("InputClassFanOutComplexityMultiDimensionalArray.java"), expected);
219     }
220 
221     @Test
222     public void testPackageName() throws Exception {
223         final DefaultConfiguration moduleConfig =
224                 createModuleConfig(ClassFanOutComplexityCheck.class);
225         moduleConfig.addAttribute("max", "0");
226 
227         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
228         verify(moduleConfig,
229                 getPath("InputClassFanOutComplexityPackageName.java"), expected);
230     }
231 }