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.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      @Override
33      protected String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/metrics/classdataabstractioncoupling";
35      }
36  
37      @Test
38      public void testExample1() throws Exception {
39          final String[] expected = {};
40  
41          verifyWithInlineConfigParser(getPath("Example1.java"), expected);
42      }
43  
44      @Test
45      public void testExample2() throws Exception {
46          final String expectedClasses = List.of(
47              "AtomicInteger",
48              "BigInteger",
49              "Example1",
50              "Example3",
51              "Example4",
52              "Example5",
53              "Example6",
54              "Example7"
55          ).toString();
56  
57          final String[] expected = {
58              "25:1: " + getCheckMessage(MSG_KEY, 8, 7, expectedClasses),
59          };
60  
61          verifyWithInlineConfigParser(getPath("Example2.java"), expected);
62      }
63  
64      @Test
65      public void testExample3() throws Exception {
66          final String[] expected = {};
67  
68          verifyWithInlineConfigParser(getPath("Example3.java"), expected);
69      }
70  
71      @Test
72      public void testExample4() throws Exception {
73          final String[] expected = {
74              "22:1: " + getCheckMessage(MSG_KEY, 3, 2, "[AtomicInteger, BigDecimal, BigInteger]"),
75          };
76  
77          verifyWithInlineConfigParser(getPath("ignore/deeper/Example4.java"), expected);
78      }
79  
80      @Test
81      public void testExample5() throws Exception {
82          final String[] expected = {};
83  
84          verifyWithInlineConfigParser(getPath("ignore/deeper/Example5.java"), expected);
85      }
86  
87      @Test
88      public void testExample6() throws Exception {
89          final String expectedClasses = List.of(
90              "AtomicInteger",
91              "BigInteger",
92              "Example2",
93              "Example3",
94              "Example4",
95              "Example5",
96              "Example7",
97              "Example8"
98          ).toString();
99  
100         final String[] expected = {
101             "27:1: " + getCheckMessage(MSG_KEY, 8, 7, expectedClasses),
102         };
103 
104         verifyWithInlineConfigParser(getPath("ignore/deeper/Example6.java"), expected);
105     }
106 
107     @Test
108     public void testExample7() throws Exception {
109         final String[] expected = {};
110 
111         verifyWithInlineConfigParser(getPath("ignore/Example7.java"), expected);
112     }
113 
114     @Test
115     public void testExample8() throws Exception {
116         final String expectedClasses = List.of(
117                 "AtomicInteger",
118                 "BigDecimal",
119                 "BigInteger",
120                 "ByteArrayInputStream",
121                 "CharArrayWriter",
122                 "File",
123                 "MathContext",
124                 "StringWriter"
125         ).toString();
126 
127         final String[] expected = {
128             "28:1: " + getCheckMessage(MSG_KEY, 8, 7, expectedClasses),
129         };
130 
131         verifyWithInlineConfigParser(getPath("ignore/Example8.java"), expected);
132     }
133 
134     @Test
135     public void testExample9() throws Exception {
136         final String[] expected = {};
137 
138         verifyWithInlineConfigParser(getPath("ignore/Example9.java"), expected);
139     }
140 
141     @Test
142     public void testExample10() throws Exception {
143         final String expectedClasses = List.of(
144                 "AtomicInteger",
145                 "BigDecimal",
146                 "BigInteger",
147                 "Example2",
148                 "Example3",
149                 "Example4",
150                 "Example6",
151                 "MathContext"
152         ).toString();
153 
154         final String[] expected = {
155             "33:1: " + getCheckMessage(MSG_KEY, 8, 7, expectedClasses),
156         };
157 
158         verifyWithInlineConfigParser(getPath("ignore/Example10.java"), expected);
159     }
160 
161     @Test
162     public void testExample11() throws Exception {
163         final String[] expected = {};
164 
165         verifyWithInlineConfigParser(getPath("Example11.java"), expected);
166     }
167 }