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