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.ClassDataAbstractionCouplingCheck.MSG_KEY;
23  import static org.junit.Assert.assertArrayEquals;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertTrue;
27  import static org.junit.Assert.fail;
28  
29  import org.junit.Test;
30  
31  import antlr.CommonHiddenStreamToken;
32  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
33  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
34  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
35  import com.puppycrawl.tools.checkstyle.api.DetailAST;
36  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
37  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
38  
39  public class ClassDataAbstractionCouplingCheckTest extends AbstractModuleTestSupport {
40      @Override
41      protected String getPackageLocation() {
42          return "com/puppycrawl/tools/checkstyle/checks/metrics/classdataabstractioncoupling";
43      }
44  
45      @Test
46      public void testTokens() {
47          final ClassDataAbstractionCouplingCheck check = new ClassDataAbstractionCouplingCheck();
48          assertNotNull("Required tokens should not be null", check.getRequiredTokens());
49          assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
50          assertArrayEquals("Invalid default tokens", check.getDefaultTokens(),
51                  check.getAcceptableTokens());
52          assertArrayEquals("Invalid acceptable tokens", check.getDefaultTokens(),
53                  check.getRequiredTokens());
54      }
55  
56      @Test
57      public void test() throws Exception {
58          final DefaultConfiguration checkConfig =
59              createModuleConfig(ClassDataAbstractionCouplingCheck.class);
60  
61          checkConfig.addAttribute("max", "0");
62          checkConfig.addAttribute("excludedClasses", "InnerClass");
63  
64          final String[] expected = {
65              "6:1: " + getCheckMessage(MSG_KEY, 4, 0, "[AnotherInnerClass, HashMap, HashSet, int]"),
66              "7:5: " + getCheckMessage(MSG_KEY, 1, 0, "[ArrayList]"),
67              "27:1: " + getCheckMessage(MSG_KEY, 2, 0, "[HashMap, HashSet]"),
68          };
69  
70          verify(checkConfig, getPath("InputClassDataAbstractionCoupling.java"), expected);
71      }
72  
73      @Test
74      public void testExcludedPackageDirectPackages() throws Exception {
75          final DefaultConfiguration checkConfig =
76              createModuleConfig(ClassDataAbstractionCouplingCheck.class);
77  
78          checkConfig.addAttribute("max", "0");
79          checkConfig.addAttribute("excludedPackages",
80              "com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.inputs.c,"
81                  + "com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling."
82                  + "inputs.b");
83  
84          final String[] expected = {
85              "8:1: " + getCheckMessage(MSG_KEY, 2, 0, "[AAClass, ABClass]"),
86          };
87  
88          verify(checkConfig,
89              getPath("InputClassDataAbstractionCouplingExcludedPackagesDirectPackages.java"),
90                  expected);
91      }
92  
93      @Test
94      public void testExcludedPackageCommonPackages() throws Exception {
95          final DefaultConfiguration checkConfig =
96              createModuleConfig(ClassDataAbstractionCouplingCheck.class);
97  
98          checkConfig.addAttribute("max", "0");
99          checkConfig.addAttribute("excludedPackages",
100             "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a");
101 
102         final String[] expected = {
103             "8:1: " + getCheckMessage(MSG_KEY, 2, 0, "[AAClass, ABClass]"),
104             "12:5: " + getCheckMessage(MSG_KEY, 2, 0, "[BClass, CClass]"),
105             "18:1: " + getCheckMessage(MSG_KEY, 1, 0, "[CClass]"),
106         };
107         verify(checkConfig,
108             getPath("InputClassDataAbstractionCouplingExcludedPackagesCommonPackage.java"),
109                 expected);
110     }
111 
112     @Test
113     public void testExcludedPackageWithEndingDot() throws Exception {
114         final DefaultConfiguration checkConfig =
115             createModuleConfig(ClassDataAbstractionCouplingCheck.class);
116 
117         checkConfig.addAttribute("max", "0");
118         checkConfig.addAttribute("excludedPackages",
119             "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.");
120 
121         try {
122             createChecker(checkConfig);
123             fail("exception expected");
124         }
125         catch (CheckstyleException ex) {
126             final String messageStart =
127                 "cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
128                     + "Cannot set property 'excludedPackages' to "
129                     + "'com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.' in module "
130                     + "com.puppycrawl.tools.checkstyle.checks.metrics."
131                     + "ClassDataAbstractionCouplingCheck";
132 
133             assertTrue("Invalid exception message, should start with: " + messageStart,
134                 ex.getMessage().startsWith(messageStart));
135         }
136     }
137 
138     @Test
139     public void testExcludedPackageCommonPackagesAllIgnored() throws Exception {
140         final DefaultConfiguration checkConfig =
141             createModuleConfig(ClassDataAbstractionCouplingCheck.class);
142 
143         checkConfig.addAttribute("max", "0");
144         checkConfig.addAttribute("excludedPackages",
145             "com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling.inputs."
146                     + "a.aa,"
147                 + "com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling."
148                     + "inputs.a.ab,"
149                 + "com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling."
150                     + "inputs.b,"
151                 + "com.puppycrawl.tools.checkstyle.checks.metrics.classdataabstractioncoupling."
152                     + "inputs.c");
153 
154         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
155         verify(checkConfig,
156             getPath("InputClassDataAbstractionCouplingExcludedPackagesAllIgnored.java"), expected);
157     }
158 
159     @Test
160     public void testDefaultConfiguration() throws Exception {
161         final DefaultConfiguration checkConfig =
162             createModuleConfig(ClassDataAbstractionCouplingCheck.class);
163 
164         createChecker(checkConfig);
165         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
166         verify(checkConfig, getPath("InputClassDataAbstractionCoupling.java"), expected);
167     }
168 
169     @Test
170     public void testWrongToken() {
171         final ClassDataAbstractionCouplingCheck classDataAbstractionCouplingCheckObj =
172             new ClassDataAbstractionCouplingCheck();
173         final DetailAST ast = new DetailAST();
174         ast.initialize(new CommonHiddenStreamToken(TokenTypes.CTOR_DEF, "ctor"));
175         try {
176             classDataAbstractionCouplingCheckObj.visitToken(ast);
177             fail("exception expected");
178         }
179         catch (IllegalArgumentException ex) {
180             assertEquals("Invalid exception message",
181                 "Unknown type: ctor[0x-1]", ex.getMessage());
182         }
183     }
184 
185     @Test
186     public void testRegularExpression() throws Exception {
187         final DefaultConfiguration checkConfig =
188                 createModuleConfig(ClassDataAbstractionCouplingCheck.class);
189 
190         checkConfig.addAttribute("max", "0");
191         checkConfig.addAttribute("excludedClasses", "InnerClass");
192         checkConfig.addAttribute("excludeClassesRegexps", "^Hash.*");
193 
194         final String[] expected = {
195             "6:1: " + getCheckMessage(MSG_KEY, 2, 0, "[AnotherInnerClass, int]"),
196             "7:5: " + getCheckMessage(MSG_KEY, 1, 0, "[ArrayList]"),
197         };
198 
199         verify(checkConfig, getPath("InputClassDataAbstractionCoupling.java"), expected);
200     }
201 
202     @Test
203     public void testEmptyRegularExpression() throws Exception {
204         final DefaultConfiguration checkConfig =
205                 createModuleConfig(ClassDataAbstractionCouplingCheck.class);
206 
207         checkConfig.addAttribute("max", "0");
208         checkConfig.addAttribute("excludedClasses", "InnerClass");
209         checkConfig.addAttribute("excludeClassesRegexps", "");
210 
211         final String[] expected = {
212             "6:1: " + getCheckMessage(MSG_KEY, 4, 0, "[AnotherInnerClass, HashMap, HashSet, int]"),
213             "7:5: " + getCheckMessage(MSG_KEY, 1, 0, "[ArrayList]"),
214             "27:1: " + getCheckMessage(MSG_KEY, 2, 0, "[HashMap, HashSet]"),
215         };
216 
217         verify(checkConfig, getPath("InputClassDataAbstractionCoupling.java"), expected);
218     }
219 }