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.BooleanExpressionComplexityCheck.MSG_KEY;
24  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.getExpectedThrowable;
25  
26  import org.antlr.v4.runtime.CommonToken;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DetailAstImpl;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
33  
34  public class BooleanExpressionComplexityCheckTest extends AbstractModuleTestSupport {
35  
36      @Override
37      public String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/metrics/booleanexpressioncomplexity";
39      }
40  
41      @Test
42      public void test() throws Exception {
43  
44          final String[] expected = {
45              "21:9: " + getCheckMessage(MSG_KEY, 4, 3),
46              "39:46: " + getCheckMessage(MSG_KEY, 4, 3),
47              "50:9: " + getCheckMessage(MSG_KEY, 6, 3),
48              "57:34: " + getCheckMessage(MSG_KEY, 4, 3),
49              "60:34: " + getCheckMessage(MSG_KEY, 4, 3),
50          };
51  
52          verifyWithInlineConfigParser(
53                  getPath("InputBooleanExpressionComplexity.java"), expected);
54      }
55  
56      @Test
57      public void testNoBitwise() throws Exception {
58  
59          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
60  
61          verifyWithInlineConfigParser(
62                  getPath("InputBooleanExpressionComplexity2.java"), expected);
63      }
64  
65      @Test
66      public void testNullPointerException() throws Exception {
67  
68          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
69  
70          verifyWithInlineConfigParser(
71                  getPath("InputBooleanExpressionComplexityNPE.java"), expected);
72      }
73  
74      @Test
75      public void testWrongToken() {
76          final BooleanExpressionComplexityCheck booleanExpressionComplexityCheckObj =
77              new BooleanExpressionComplexityCheck();
78          final DetailAstImpl ast = new DetailAstImpl();
79          ast.initialize(new CommonToken(TokenTypes.INTERFACE_DEF, "interface"));
80          final IllegalArgumentException exc =
81                  getExpectedThrowable(IllegalArgumentException.class,
82                          () -> booleanExpressionComplexityCheckObj.visitToken(ast));
83          assertWithMessage("Invalid exception message")
84              .that(exc.getMessage())
85              .isEqualTo("Unknown type: interface[0x-1]");
86      }
87  
88      @Test
89      public void testSmall() throws Exception {
90  
91          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
92  
93          verifyWithInlineConfigParser(
94                  getPath("InputBooleanExpressionComplexitySmall.java"), expected);
95      }
96  
97      @Test
98      public void testBooleanExpressionComplexityRecordsAndCompactCtors() throws Exception {
99  
100         final int max = 3;
101 
102         final String[] expected = {
103             "16:12: " + getCheckMessage(MSG_KEY, 4, max),
104             "25:23: " + getCheckMessage(MSG_KEY, 4, max),
105             "37:23: " + getCheckMessage(MSG_KEY, 4, max),
106             "48:27: " + getCheckMessage(MSG_KEY, 4, max),
107         };
108 
109         verifyWithInlineConfigParser(
110                 getPath(
111                         "InputBooleanExpressionComplexityRecordsAndCompactCtors.java"),
112                 expected);
113     }
114 
115     @Test
116     public void testLeaves() throws Exception {
117 
118         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
119 
120         verifyWithInlineConfigParser(
121                 getPath("InputBooleanExpressionComplexityLeaves.java"), expected);
122     }
123 
124     @Test
125     public void testRecordLeaves() throws Exception {
126 
127         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
128 
129         verifyWithInlineConfigParser(
130                 getPath("InputBooleanExpressionComplexityRecordLeaves.java"),
131                 expected);
132     }
133 
134     @Test
135     public void testWhenExpression() throws Exception {
136 
137         final int max = 0;
138 
139         final String[] expected = {
140             "17:21: " + getCheckMessage(MSG_KEY, 6, max),
141             "21:17: " + getCheckMessage(MSG_KEY, 6, max),
142             "25:27: " + getCheckMessage(MSG_KEY, 6, max),
143             "29:48: " + getCheckMessage(MSG_KEY, 1, max),
144         };
145 
146         verifyWithInlineConfigParser(
147                 getNonCompilablePath("InputBooleanExpressionComplexityWhenExpression.java"),
148                 expected);
149     }
150 
151 }