View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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 org.checkstyle.suppressionxpathfilter.metrics;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.metrics.ClassDataAbstractionCouplingCheck;
31  
32  public class XpathRegressionClassDataAbstractionCouplingTest extends AbstractXpathTestSupport {
33  
34      private final String checkName = ClassDataAbstractionCouplingCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Override
42      protected String getPackageLocation() {
43          return "org/checkstyle/suppressionxpathfilter/metrics/classdataabstractioncoupling";
44      }
45  
46      @Test
47      public void testClassDataAbstractCouplingClass() throws Exception {
48          final File classPath =
49                  new File(getPath("InputXpathClassDataAbstractionCouplingClass.java"));
50  
51          final DefaultConfiguration configuration =
52                  createModuleConfig(ClassDataAbstractionCouplingCheck.class);
53  
54          final String expectedClasses = List.of(
55              "AtomicInteger",
56              "BigDecimal",
57              "BigInteger",
58              "ByteArrayInputStream",
59              "CharArrayWriter",
60              "File",
61              "MathContext",
62              "StringWriter"
63          ).toString();
64  
65          final String[] expectedViolations = {
66              "14:1: " + getCheckMessage(ClassDataAbstractionCouplingCheck.class,
67                  ClassDataAbstractionCouplingCheck.MSG_KEY, 8, 7, expectedClasses),
68          };
69  
70          final List<String> expectedXpathQueries = Arrays.asList(
71              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
72              + "[@text='InputXpathClassDataAbstractionCouplingClass']]",
73              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
74              + "[@text='InputXpathClassDataAbstractionCouplingClass']]"
75              + "/MODIFIERS",
76              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
77              + "[@text='InputXpathClassDataAbstractionCouplingClass']]"
78              + "/MODIFIERS/LITERAL_PUBLIC"
79          );
80  
81          runVerifications(configuration, classPath, expectedViolations,
82                  expectedXpathQueries);
83      }
84  
85      @Test
86      public void testClassDataAbstractCouplingEnum() throws Exception {
87          final File classPath =
88                  new File(getPath("InputXpathClassDataAbstractionCouplingEnum.java"));
89  
90          final DefaultConfiguration configuration =
91                  createModuleConfig(ClassDataAbstractionCouplingCheck.class);
92  
93          final String expectedClasses = List.of(
94              "BigDecimal",
95              "BigInteger",
96              "CharArrayWriter",
97              "File",
98              "MathContext",
99              "Runnable",
100             "StringWriter",
101             "Thread"
102         ).toString();
103 
104         final String[] expectedViolations = {
105             "11:1: " + getCheckMessage(ClassDataAbstractionCouplingCheck.class,
106                 ClassDataAbstractionCouplingCheck.MSG_KEY, 8, 7, expectedClasses),
107         };
108 
109         final List<String> expectedXpathQueries = Arrays.asList(
110             "/COMPILATION_UNIT/ENUM_DEF[./IDENT"
111             + "[@text='InputXpathClassDataAbstractionCouplingEnum']]",
112             "/COMPILATION_UNIT/ENUM_DEF[./IDENT"
113             + "[@text='InputXpathClassDataAbstractionCouplingEnum']]"
114             + "/MODIFIERS",
115             "/COMPILATION_UNIT/ENUM_DEF[./IDENT"
116             + "[@text='InputXpathClassDataAbstractionCouplingEnum']]"
117             + "/MODIFIERS/LITERAL_PUBLIC"
118         );
119 
120         runVerifications(configuration, classPath, expectedViolations,
121                 expectedXpathQueries);
122     }
123 
124     @Test
125     public void testClassDataAbstractCouplingInterface() throws Exception {
126         final File classPath =
127                 new File(getPath("InputXpathClassDataAbstractionCouplingInterface.java"));
128 
129         final DefaultConfiguration configuration =
130                 createModuleConfig(ClassDataAbstractionCouplingCheck.class);
131 
132         final String expectedClasses = List.of(
133             "BigDecimal",
134             "BigInteger",
135             "CharArrayWriter",
136             "File",
137             "MathContext",
138             "Runnable",
139             "StringWriter",
140             "Thread"
141         ).toString();
142 
143         final String[] expectedViolations = {
144             "11:1: " + getCheckMessage(ClassDataAbstractionCouplingCheck.class,
145                 ClassDataAbstractionCouplingCheck.MSG_KEY, 8, 7, expectedClasses),
146         };
147 
148         final List<String> expectedXpathQueries = Arrays.asList(
149             "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
150             + "[@text='InputXpathClassDataAbstractionCouplingInterface']]",
151             "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
152             + "[@text='InputXpathClassDataAbstractionCouplingInterface']]"
153             + "/MODIFIERS",
154             "/COMPILATION_UNIT/INTERFACE_DEF[./IDENT"
155             + "[@text='InputXpathClassDataAbstractionCouplingInterface']]"
156             + "/MODIFIERS/LITERAL_PUBLIC"
157         );
158 
159         runVerifications(configuration, classPath, expectedViolations,
160                 expectedXpathQueries);
161     }
162 }