View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.BooleanExpressionComplexityCheck.MSG_KEY;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.fail;
25  
26  import org.junit.Test;
27  
28  import antlr.CommonHiddenStreamToken;
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.api.DetailAST;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
34  
35  public class BooleanExpressionComplexityCheckTest extends AbstractModuleTestSupport {
36      @Override
37      protected String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/metrics/booleanexpressioncomplexity";
39      }
40  
41      @Test
42      public void test() throws Exception {
43          final DefaultConfiguration checkConfig =
44              createModuleConfig(BooleanExpressionComplexityCheck.class);
45  
46          final String[] expected = {
47              "13:9: " + getCheckMessage(MSG_KEY, 4, 3),
48              "29:99: " + getCheckMessage(MSG_KEY, 4, 3),
49              "39:9: " + getCheckMessage(MSG_KEY, 6, 3),
50              "45:34: " + getCheckMessage(MSG_KEY, 4, 3),
51              "47:34: " + getCheckMessage(MSG_KEY, 4, 3),
52          };
53  
54          verify(checkConfig, getPath("InputBooleanExpressionComplexity.java"), expected);
55      }
56  
57      @Test
58      public void testNoBitwise() throws Exception {
59          final DefaultConfiguration checkConfig =
60              createModuleConfig(BooleanExpressionComplexityCheck.class);
61          checkConfig.addAttribute("max", "5");
62          checkConfig.addAttribute("tokens", "BXOR,LAND,LOR");
63  
64          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
65  
66          verify(checkConfig, getPath("InputBooleanExpressionComplexity.java"), expected);
67      }
68  
69      @Test
70      public void testNullPointerException() throws Exception {
71          final DefaultConfiguration checkConfig =
72              createModuleConfig(BooleanExpressionComplexityCheck.class);
73  
74          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
75  
76          verify(checkConfig, getPath("InputBooleanExpressionComplexityNPE.java"), expected);
77      }
78  
79      @Test
80      public void testWrongToken() {
81          final BooleanExpressionComplexityCheck booleanExpressionComplexityCheckObj =
82              new BooleanExpressionComplexityCheck();
83          final DetailAST ast = new DetailAST();
84          ast.initialize(new CommonHiddenStreamToken(TokenTypes.INTERFACE_DEF, "interface"));
85          try {
86              booleanExpressionComplexityCheckObj.visitToken(ast);
87              fail("exception expected");
88          }
89          catch (IllegalArgumentException ex) {
90              assertEquals("Invalid exception message",
91                  "Unknown type: interface[0x-1]", ex.getMessage());
92          }
93      }
94  }