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.CyclomaticComplexityCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  public class CyclomaticComplexityCheckTest
32      extends AbstractModuleTestSupport {
33  
34      @Override
35      public String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity";
37      }
38  
39      @Test
40      public void testSwitchBlockAsSingleDecisionPointSetToTrue() throws Exception {
41  
42          final String[] expected = {
43              "14:5: " + getCheckMessage(MSG_KEY, 2, 0),
44          };
45  
46          verifyWithInlineConfigParser(
47                  getPath("InputCyclomaticComplexitySwitchBlocks.java"), expected);
48      }
49  
50      @Test
51      public void testSwitchBlockAsSingleDecisionPointSetToFalse() throws Exception {
52  
53          final String[] expected = {
54              "14:5: " + getCheckMessage(MSG_KEY, 5, 0),
55          };
56  
57          verifyWithInlineConfigParser(
58                  getPath("InputCyclomaticComplexitySwitchBlocks2.java"), expected);
59      }
60  
61      @Test
62      public void testEqualsMaxComplexity() throws Exception {
63  
64          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
65  
66          verifyWithInlineConfigParser(
67                  getPath("InputCyclomaticComplexitySwitchBlocks3.java"), expected);
68      }
69  
70      @Test
71      public void test1() throws Exception {
72  
73          final String[] expected = {
74              "15:5: " + getCheckMessage(MSG_KEY, 2, 0),
75              "20:17: " + getCheckMessage(MSG_KEY, 2, 0),
76              "32:5: " + getCheckMessage(MSG_KEY, 6, 0),
77              "45:5: " + getCheckMessage(MSG_KEY, 3, 0),
78              "55:5: " + getCheckMessage(MSG_KEY, 5, 0),
79          };
80  
81          verifyWithInlineConfigParser(
82                  getPath("InputCyclomaticComplexity1.java"), expected);
83      }
84  
85      @Test
86      public void test2() throws Exception {
87  
88          final String[] expected = {
89              "16:5: " + getCheckMessage(MSG_KEY, 3, 0),
90              "29:5: " + getCheckMessage(MSG_KEY, 3, 0),
91              "41:5: " + getCheckMessage(MSG_KEY, 3, 0),
92              "54:5: " + getCheckMessage(MSG_KEY, 1, 0),
93              "58:13: " + getCheckMessage(MSG_KEY, 2, 0),
94          };
95  
96          verifyWithInlineConfigParser(
97                  getPath("InputCyclomaticComplexity2.java"), expected);
98      }
99  
100     @Test
101     public void testCyclomaticComplexityRecords1() throws Exception {
102 
103         final int max = 0;
104 
105         final String[] expected = {
106             "18:9: " + getCheckMessage(MSG_KEY, 11, max),
107             "49:9: " + getCheckMessage(MSG_KEY, 11, max),
108             "81:9: " + getCheckMessage(MSG_KEY, 11, max),
109         };
110 
111         verifyWithInlineConfigParser(
112                 getPath("InputCyclomaticComplexityRecords1.java"), expected);
113     }
114 
115     @Test
116     public void testCyclomaticComplexityRecords2() throws Exception {
117 
118         final int max = 0;
119 
120         final String[] expected = {
121             "17:9: " + getCheckMessage(MSG_KEY, 11, max),
122             "49:9: " + getCheckMessage(MSG_KEY, 11, max),
123         };
124 
125         verifyWithInlineConfigParser(
126                 getPath("InputCyclomaticComplexityRecords2.java"), expected);
127     }
128 
129     @Test
130     public void testGetAcceptableTokens() {
131         final CyclomaticComplexityCheck cyclomaticComplexityCheckObj =
132             new CyclomaticComplexityCheck();
133         final int[] actual = cyclomaticComplexityCheckObj.getAcceptableTokens();
134         final int[] expected = {
135             TokenTypes.CTOR_DEF,
136             TokenTypes.METHOD_DEF,
137             TokenTypes.INSTANCE_INIT,
138             TokenTypes.STATIC_INIT,
139             TokenTypes.LITERAL_WHILE,
140             TokenTypes.LITERAL_DO,
141             TokenTypes.LITERAL_FOR,
142             TokenTypes.LITERAL_IF,
143             TokenTypes.LITERAL_SWITCH,
144             TokenTypes.LITERAL_CASE,
145             TokenTypes.LITERAL_CATCH,
146             TokenTypes.QUESTION,
147             TokenTypes.LAND,
148             TokenTypes.LOR,
149             TokenTypes.COMPACT_CTOR_DEF,
150             TokenTypes.LITERAL_WHEN,
151         };
152         assertWithMessage("Invalid acceptable tokens")
153             .that(actual)
154             .isEqualTo(expected);
155     }
156 
157     @Test
158     public void testGetRequiredTokens() {
159         final CyclomaticComplexityCheck cyclomaticComplexityCheckObj =
160             new CyclomaticComplexityCheck();
161         final int[] actual = cyclomaticComplexityCheckObj.getRequiredTokens();
162         final int[] expected = {
163             TokenTypes.CTOR_DEF,
164             TokenTypes.METHOD_DEF,
165             TokenTypes.INSTANCE_INIT,
166             TokenTypes.STATIC_INIT,
167             TokenTypes.COMPACT_CTOR_DEF,
168         };
169         assertWithMessage("Invalid required tokens")
170             .that(actual)
171             .isEqualTo(expected);
172     }
173 
174     @Test
175     public void testHighMax() throws Exception {
176         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
177 
178         verifyWithInlineConfigParser(
179                 getPath("InputCyclomaticComplexitySwitchBlocks4.java"), expected);
180     }
181 
182     @Test
183     public void testDefaultMax() throws Exception {
184         final String[] expected = {
185             "14:5: " + getCheckMessage(MSG_KEY, 12, 10),
186         };
187 
188         verifyWithInlineConfigParser(
189                 getPath("InputCyclomaticComplexitySwitchBlocks5.java"), expected);
190     }
191 
192     @Test
193     public void testWhenExpression() throws Exception {
194         final String[] expected = {
195             "16:4: " + getCheckMessage(MSG_KEY, 5, 0),
196             "22:4: " + getCheckMessage(MSG_KEY, 5, 0),
197             "31:4: " + getCheckMessage(MSG_KEY, 7, 0),
198         };
199         verifyWithInlineConfigParser(
200                 getPath("InputCyclomaticComplexityWhenExpression.java"), expected);
201     }
202 
203     @Test
204     public void testWhenExpressionSwitchAsSinglePoint() throws Exception {
205         final String[] expected = {
206             "16:5: " + getCheckMessage(MSG_KEY, 5, 0),
207             "22:5: " + getCheckMessage(MSG_KEY, 2, 0),
208             "31:5: " + getCheckMessage(MSG_KEY, 2, 0),
209             "41:5: " + getCheckMessage(MSG_KEY, 2, 0),
210         };
211         verifyWithInlineConfigParser(
212                 getPath(
213                         "InputCyclomaticComplexityWhenSwitchAsSinglePoint.java"), expected);
214     }
215 
216     @Test
217     public void testSwitchBlockAsSingleDecisionPointWithNestedSwitch() throws Exception {
218         final String[] expected = {
219             "17:5: " + getCheckMessage(MSG_KEY, 2, 0),
220             "26:5: " + getCheckMessage(MSG_KEY, 2, 0),
221             "41:5: " + getCheckMessage(MSG_KEY, 2, 0),
222         };
223         verifyWithInlineConfigParser(
224                 getPath(
225                         "InputCyclomaticComplexitySwitchBlocks6.java"), expected);
226     }
227 
228 }