View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.metrics.ClassDataAbstractionCouplingCheck.MSG_KEY;
24  
25  import org.antlr.v4.runtime.CommonToken;
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.DetailAstImpl;
31  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
34  
35  public class ClassDataAbstractionCouplingCheckTest extends AbstractModuleTestSupport {
36  
37      @Override
38      protected String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/checks/metrics/classdataabstractioncoupling";
40      }
41  
42      @Test
43      public void testTokens() {
44          final ClassDataAbstractionCouplingCheck check = new ClassDataAbstractionCouplingCheck();
45          assertWithMessage("Required tokens should not be null")
46              .that(check.getRequiredTokens())
47              .isNotNull();
48          assertWithMessage("Acceptable tokens should not be null")
49              .that(check.getAcceptableTokens())
50              .isNotNull();
51          assertWithMessage("Invalid default tokens")
52              .that(check.getAcceptableTokens())
53              .isEqualTo(check.getDefaultTokens());
54          assertWithMessage("Invalid acceptable tokens")
55              .that(check.getRequiredTokens())
56              .isEqualTo(check.getDefaultTokens());
57      }
58  
59      @Test
60      public void test() throws Exception {
61  
62          final String[] expected = {
63              "16:1: " + getCheckMessage(MSG_KEY, 4, 0, "[AnotherInnerClass, HashMap, HashSet, int]"),
64              "17:5: " + getCheckMessage(MSG_KEY, 1, 0, "[ArrayList]"),
65              "37:1: " + getCheckMessage(MSG_KEY, 2, 0, "[HashMap, HashSet]"),
66          };
67  
68          verifyWithInlineConfigParser(
69                  getPath("InputClassDataAbstractionCoupling.java"), expected);
70      }
71  
72      @Test
73      public void testExcludedPackageDirectPackages() throws Exception {
74          final String[] expected = {
75              "28:1: " + getCheckMessage(MSG_KEY, 2, 0, "[BasicHttpContext, TlsCiphers]"),
76          };
77  
78          verifyWithInlineConfigParser(
79                  getPath("InputClassDataAbstractionCouplingExcludedPackagesDirectPackages.java"),
80                  expected);
81      }
82  
83      @Test
84      public void testExcludedPackageCommonPackages() throws Exception {
85          final String[] expected = {
86              "28:1: " + getCheckMessage(MSG_KEY, 2, 0, "[BasicHttpContext, TlsCiphers]"),
87              "32:5: " + getCheckMessage(MSG_KEY, 2, 0, "[BasicClientTlsStrategy, CommandSupport]"),
88              "38:1: " + getCheckMessage(MSG_KEY, 1, 0, "[CommandSupport]"),
89          };
90          verifyWithInlineConfigParser(
91                  getPath("InputClassDataAbstractionCouplingExcludedPackagesCommonPackage.java"),
92                  expected);
93      }
94  
95      @Test
96      public void testExcludedPackageWithEndingDot() throws Exception {
97          final DefaultConfiguration checkConfig =
98              createModuleConfig(ClassDataAbstractionCouplingCheck.class);
99  
100         checkConfig.addProperty("max", "0");
101         checkConfig.addProperty("excludedPackages",
102             "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.");
103 
104         try {
105             createChecker(checkConfig);
106             assertWithMessage("exception expected").fail();
107         }
108         catch (CheckstyleException ex) {
109             assertWithMessage("Invalid exception message")
110                 .that(ex.getMessage())
111                 .isEqualTo("cannot initialize module com.puppycrawl.tools.checkstyle.TreeWalker - "
112                     + "cannot initialize module com.puppycrawl.tools.checkstyle.checks."
113                     + "metrics.ClassDataAbstractionCouplingCheck - "
114                     + "Cannot set property 'excludedPackages' to "
115                     + "'com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.'");
116             assertWithMessage("Invalid exception message,")
117                 .that(ex.getCause().getCause().getCause().getCause().getMessage())
118                 .isEqualTo("the following values are not valid identifiers: ["
119                     + "com.puppycrawl.tools.checkstyle.checks.metrics.inputs.a.]");
120         }
121     }
122 
123     @Test
124     public void testExcludedPackageCommonPackagesAllIgnored() throws Exception {
125         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
126         verifyWithInlineConfigParser(
127                 getPath("InputClassDataAbstractionCouplingExcludedPackagesAllIgnored.java"),
128                 expected);
129     }
130 
131     @Test
132     public void testDefaultConfiguration() throws Exception {
133         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
134         verifyWithInlineConfigParser(
135                 getPath("InputClassDataAbstractionCoupling2.java"), expected);
136     }
137 
138     @Test
139     public void testWrongToken() {
140         final ClassDataAbstractionCouplingCheck classDataAbstractionCouplingCheckObj =
141             new ClassDataAbstractionCouplingCheck();
142         final DetailAstImpl ast = new DetailAstImpl();
143         ast.initialize(new CommonToken(TokenTypes.CTOR_DEF, "ctor"));
144         try {
145             classDataAbstractionCouplingCheckObj.visitToken(ast);
146             assertWithMessage("exception expected").fail();
147         }
148         catch (IllegalArgumentException ex) {
149             assertWithMessage("Invalid exception message")
150                 .that(ex.getMessage())
151                 .isEqualTo("Unknown type: ctor[0x-1]");
152         }
153     }
154 
155     @Test
156     public void testRegularExpression() throws Exception {
157 
158         final String[] expected = {
159             "22:1: " + getCheckMessage(MSG_KEY, 2, 0, "[AnotherInnerClass, int]"),
160             "23:5: " + getCheckMessage(MSG_KEY, 1, 0, "[ArrayList]"),
161         };
162 
163         verifyWithInlineConfigParser(
164                 getPath("InputClassDataAbstractionCoupling3.java"), expected);
165     }
166 
167     @Test
168     public void testEmptyRegularExpression() throws Exception {
169 
170         final String[] expected = {
171             "22:1: " + getCheckMessage(MSG_KEY, 4, 0, "[AnotherInnerClass, HashMap, HashSet, int]"),
172             "23:5: " + getCheckMessage(MSG_KEY, 1, 0, "[ArrayList]"),
173             "43:1: " + getCheckMessage(MSG_KEY, 2, 0, "[HashMap, HashSet]"),
174         };
175 
176         verifyWithInlineConfigParser(
177                 getPath("InputClassDataAbstractionCoupling4.java"), expected);
178     }
179 
180     @Test
181     public void testClassDataAbstractionCouplingRecords() throws Exception {
182 
183         final int maxAbstraction = 1;
184         final String[] expected = {
185             "31:1: " + getCheckMessage(MSG_KEY, 2, maxAbstraction, "[Date, Time]"),
186             "36:1: " + getCheckMessage(MSG_KEY, 2, maxAbstraction, "[Date, Time]"),
187             "46:1: " + getCheckMessage(MSG_KEY, 2, maxAbstraction, "[Date, Time]"),
188             "55:1: " + getCheckMessage(MSG_KEY, 2, maxAbstraction, "[Date, Time]"),
189             "67:5: " + getCheckMessage(MSG_KEY, 3, maxAbstraction, "[Date, Place, Time]"),
190         };
191 
192         verifyWithInlineConfigParser(
193                 getNonCompilablePath("InputClassDataAbstractionCouplingRecords.java"),
194             expected);
195     }
196 
197     @Test
198     public void testNew() throws Exception {
199 
200         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
201 
202         verifyWithInlineConfigParser(
203                 getPath("InputClassDataAbstractionCoupling5.java"), expected);
204     }
205 
206 }