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.puppycrawl.tools.checkstyle.checks.metrics.ClassDataAbstractionCouplingCheck.MSG_KEY;
23  
24  import java.util.List;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport;
29  
30  public class ClassDataAbstractionCouplingCheckExamplesTest
31          extends AbstractExamplesModuleTestSupport {
32  
33      @Override
34      public String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/metrics/classdataabstractioncoupling";
36      }
37  
38      @Test
39      public void testExample1() throws Exception {
40          final String expectedClasses = List.of(
41              "AtomicInteger",
42              "BigDecimal",
43              "BigInteger",
44              "BufferedReader",
45              "ByteArrayInputStream",
46              "CharArrayWriter",
47              "MathContext",
48              "PipedReader",
49              "UseCase1"
50          ).toString();
51  
52          final String[] expected = {
53              "26:1: " + getCheckMessage(MSG_KEY, 9, 7, expectedClasses),
54          };
55  
56          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
57      }
58  
59      @Test
60      public void testUseCase1() throws Exception {
61          final String expectedClasses = List.of(
62              "AtomicInteger",
63              "BigInteger",
64              "Example1",
65              "Example2",
66              "Example3",
67              "Example4",
68              "UseCase2",
69              "UseCase3"
70          ).toString();
71  
72          final String[] expected = {
73              "25:1: " + getCheckMessage(MSG_KEY, 8, 7, expectedClasses),
74          };
75  
76          verifyWithInlineConfigParser(getPath("UseCase1.java"), expected);
77      }
78  
79      @Test
80      public void testExample2() throws Exception {
81          final String expectedClasses = List.of(
82              "AtomicInteger",
83              "BigDecimal",
84              "BigInteger",
85              "BufferedReader",
86              "ByteArrayInputStream",
87              "CharArrayWriter",
88              "Example1",
89              "MathContext",
90              "PipedReader",
91              "UseCase1"
92          ).toString();
93  
94          final String[] expected = {
95              "28:1: " + getCheckMessage(MSG_KEY, 10, 9, expectedClasses),
96          };
97  
98          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
99      }
100 
101     @Test
102     public void testUseCase2() throws Exception {
103         final String[] expected = {
104             "22:1: " + getCheckMessage(MSG_KEY, 3, 2, "[AtomicInteger, BigDecimal, BigInteger]"),
105         };
106 
107         verifyWithInlineConfigParser(getPath("ignore/deeper/UseCase2.java"), expected);
108     }
109 
110     @Test
111     public void testExample3() throws Exception {
112         final String expectedClasses = List.of(
113             "AtomicInteger",
114             "BigDecimal",
115             "BigInteger",
116             "BufferedReader",
117             "ByteArrayInputStream",
118             "CharArrayWriter",
119             "MathContext",
120             "Object",
121             "PipedReader",
122             "UseCase1",
123             "byte"
124         ).toString();
125 
126         final String[] expected = {
127             "32:1: " + getCheckMessage(MSG_KEY, 11, 7, expectedClasses),
128         };
129 
130         verifyWithInlineConfigParser(getPath("ignore/deeper/Example3.java"), expected);
131     }
132 
133     @Test
134     public void testUseCase3() throws Exception {
135         final String expectedClasses = List.of(
136             "AtomicInteger",
137             "BigInteger",
138             "Example2",
139             "Example3",
140             "Example4",
141             "UseCase1",
142             "UseCase2",
143             "UseCase4"
144         ).toString();
145 
146         final String[] expected = {
147             "27:1: " + getCheckMessage(MSG_KEY, 8, 7, expectedClasses),
148         };
149 
150         verifyWithInlineConfigParser(getPath("ignore/deeper/UseCase3.java"), expected);
151     }
152 
153     @Test
154     public void testExample4() throws Exception {
155         final String expectedClasses = List.of(
156             "AtomicInteger",
157             "BigDecimal",
158             "BigInteger",
159             "ByteArrayInputStream",
160             "CharArrayWriter",
161             "Example1",
162             "MathContext",
163             "UseCase1"
164         ).toString();
165 
166         final String[] expected = {
167             "31:1: " + getCheckMessage(MSG_KEY, 8, 7, expectedClasses),
168         };
169 
170         verifyWithInlineConfigParser(getPath("ignore/Example4.java"), expected);
171     }
172 
173     @Test
174     public void testUseCase4() throws Exception {
175         final String expectedClasses = List.of(
176                 "AtomicInteger",
177                 "BigDecimal",
178                 "BigInteger",
179                 "ByteArrayInputStream",
180                 "CharArrayWriter",
181                 "File",
182                 "MathContext",
183                 "StringWriter"
184         ).toString();
185 
186         final String[] expected = {
187             "28:1: " + getCheckMessage(MSG_KEY, 8, 7, expectedClasses),
188         };
189 
190         verifyWithInlineConfigParser(getPath("ignore/UseCase4.java"), expected);
191     }
192 
193     @Test
194     public void testExample5() throws Exception {
195         final String[] expected = {};
196 
197         verifyWithInlineConfigParser(getPath("ignore/Example5.java"), expected);
198     }
199 
200     @Test
201     public void testUseCase5() throws Exception {
202         final String expectedClasses = List.of(
203                 "AtomicInteger",
204                 "BigDecimal",
205                 "BigInteger",
206                 "Example2",
207                 "MathContext",
208                 "UseCase1",
209                 "UseCase2",
210                 "UseCase3"
211         ).toString();
212 
213         final String[] expected = {
214             "33:1: " + getCheckMessage(MSG_KEY, 8, 7, expectedClasses),
215         };
216 
217         verifyWithInlineConfigParser(getPath("ignore/UseCase5.java"), expected);
218     }
219 
220     @Test
221     public void testUseCase6() throws Exception {
222         final String[] expected = {};
223 
224         verifyWithInlineConfigParser(getPath("UseCase6.java"), expected);
225     }
226 
227 }