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