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.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      protected 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 test() 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              "73:5: " + getCheckMessage(MSG_KEY, 3, 0),
80              "86:5: " + getCheckMessage(MSG_KEY, 3, 0),
81              "98:5: " + getCheckMessage(MSG_KEY, 3, 0),
82              "110:5: " + getCheckMessage(MSG_KEY, 1, 0),
83              "114:13: " + getCheckMessage(MSG_KEY, 2, 0),
84          };
85  
86          verifyWithInlineConfigParser(
87                  getPath("InputCyclomaticComplexity.java"), expected);
88      }
89  
90      @Test
91      public void testCyclomaticComplexityRecords() throws Exception {
92  
93          final int max = 0;
94  
95          final String[] expected = {
96              "17:9: " + getCheckMessage(MSG_KEY, 11, max),
97              "48:9: " + getCheckMessage(MSG_KEY, 11, max),
98              "83:5: " + getCheckMessage(MSG_KEY, 11, max),
99              "115:5: " + getCheckMessage(MSG_KEY, 11, max),
100             "148:5: " + getCheckMessage(MSG_KEY, 11, max),
101         };
102 
103         verifyWithInlineConfigParser(
104                 getNonCompilablePath("InputCyclomaticComplexityRecords.java"), expected);
105     }
106 
107     @Test
108     public void testGetAcceptableTokens() {
109         final CyclomaticComplexityCheck cyclomaticComplexityCheckObj =
110             new CyclomaticComplexityCheck();
111         final int[] actual = cyclomaticComplexityCheckObj.getAcceptableTokens();
112         final int[] expected = {
113             TokenTypes.CTOR_DEF,
114             TokenTypes.METHOD_DEF,
115             TokenTypes.INSTANCE_INIT,
116             TokenTypes.STATIC_INIT,
117             TokenTypes.LITERAL_WHILE,
118             TokenTypes.LITERAL_DO,
119             TokenTypes.LITERAL_FOR,
120             TokenTypes.LITERAL_IF,
121             TokenTypes.LITERAL_SWITCH,
122             TokenTypes.LITERAL_CASE,
123             TokenTypes.LITERAL_CATCH,
124             TokenTypes.QUESTION,
125             TokenTypes.LAND,
126             TokenTypes.LOR,
127             TokenTypes.COMPACT_CTOR_DEF,
128         };
129         assertWithMessage("Invalid acceptable tokens")
130             .that(actual)
131             .isEqualTo(expected);
132     }
133 
134     @Test
135     public void testGetRequiredTokens() {
136         final CyclomaticComplexityCheck cyclomaticComplexityCheckObj =
137             new CyclomaticComplexityCheck();
138         final int[] actual = cyclomaticComplexityCheckObj.getRequiredTokens();
139         final int[] expected = {
140             TokenTypes.CTOR_DEF,
141             TokenTypes.METHOD_DEF,
142             TokenTypes.INSTANCE_INIT,
143             TokenTypes.STATIC_INIT,
144             TokenTypes.COMPACT_CTOR_DEF,
145         };
146         assertWithMessage("Invalid required tokens")
147             .that(actual)
148             .isEqualTo(expected);
149     }
150 
151     @Test
152     public void testHighMax() throws Exception {
153         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
154 
155         verifyWithInlineConfigParser(
156                 getPath("InputCyclomaticComplexitySwitchBlocks4.java"), expected);
157     }
158 
159     @Test
160     public void testDefaultMax() throws Exception {
161         final String[] expected = {
162             "14:5: " + getCheckMessage(MSG_KEY, 12, 10),
163         };
164 
165         verifyWithInlineConfigParser(
166                 getPath("InputCyclomaticComplexitySwitchBlocks5.java"), expected);
167     }
168 
169 }