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