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.CyclomaticComplexityCheck.MSG_KEY;
23  
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
31  
32  public class CyclomaticComplexityCheckTest
33      extends AbstractModuleTestSupport {
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          final DefaultConfiguration checkConfig =
42              createModuleConfig(CyclomaticComplexityCheck.class);
43          checkConfig.addAttribute("max", "0");
44          checkConfig.addAttribute("switchBlockAsSingleDecisionPoint", "true");
45  
46          final String[] expected = {
47              "4:5: " + getCheckMessage(MSG_KEY, 2, 0),
48          };
49  
50          verify(checkConfig, getPath("InputCyclomaticComplexitySwitchBlocks.java"), expected);
51      }
52  
53      @Test
54      public void testSwitchBlockAsSingleDecisionPointSetToFalse() throws Exception {
55          final DefaultConfiguration checkConfig =
56              createModuleConfig(CyclomaticComplexityCheck.class);
57          checkConfig.addAttribute("max", "0");
58          checkConfig.addAttribute("switchBlockAsSingleDecisionPoint", "false");
59  
60          final String[] expected = {
61              "4:5: " + getCheckMessage(MSG_KEY, 5, 0),
62          };
63  
64          verify(checkConfig, getPath("InputCyclomaticComplexitySwitchBlocks.java"), expected);
65      }
66  
67      @Test
68      public void testEqualsMaxComplexity() throws Exception {
69          final DefaultConfiguration checkConfig =
70              createModuleConfig(CyclomaticComplexityCheck.class);
71          checkConfig.addAttribute("max", "5");
72  
73          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
74  
75          verify(checkConfig, getPath("InputCyclomaticComplexitySwitchBlocks.java"), expected);
76      }
77  
78      @Test
79      public void test() throws Exception {
80          final DefaultConfiguration checkConfig =
81              createModuleConfig(CyclomaticComplexityCheck.class);
82  
83          checkConfig.addAttribute("max", "0");
84  
85          final String[] expected = {
86              "5:5: " + getCheckMessage(MSG_KEY, 2, 0),
87              "10:17: " + getCheckMessage(MSG_KEY, 2, 0),
88              "22:5: " + getCheckMessage(MSG_KEY, 6, 0),
89              "35:5: " + getCheckMessage(MSG_KEY, 3, 0),
90              "45:5: " + getCheckMessage(MSG_KEY, 5, 0),
91              "63:5: " + getCheckMessage(MSG_KEY, 3, 0),
92              "76:5: " + getCheckMessage(MSG_KEY, 3, 0),
93              "88:5: " + getCheckMessage(MSG_KEY, 3, 0),
94              "100:5: " + getCheckMessage(MSG_KEY, 1, 0),
95              "104:13: " + getCheckMessage(MSG_KEY, 2, 0),
96          };
97  
98          verify(checkConfig, getPath("InputCyclomaticComplexity.java"), expected);
99      }
100 
101     @Test
102     public void testGetAcceptableTokens() {
103         final CyclomaticComplexityCheck cyclomaticComplexityCheckObj =
104             new CyclomaticComplexityCheck();
105         final int[] actual = cyclomaticComplexityCheckObj.getAcceptableTokens();
106         final int[] expected = {
107             TokenTypes.CTOR_DEF,
108             TokenTypes.METHOD_DEF,
109             TokenTypes.INSTANCE_INIT,
110             TokenTypes.STATIC_INIT,
111             TokenTypes.LITERAL_WHILE,
112             TokenTypes.LITERAL_DO,
113             TokenTypes.LITERAL_FOR,
114             TokenTypes.LITERAL_IF,
115             TokenTypes.LITERAL_SWITCH,
116             TokenTypes.LITERAL_CASE,
117             TokenTypes.LITERAL_CATCH,
118             TokenTypes.QUESTION,
119             TokenTypes.LAND,
120             TokenTypes.LOR,
121         };
122         Assert.assertArrayEquals("Invalid acceptable tokens", expected, actual);
123     }
124 
125     @Test
126     public void testHighMax() throws Exception {
127         final DefaultConfiguration checkConfig =
128             createModuleConfig(CyclomaticComplexityCheck.class);
129         checkConfig.addAttribute("max", "100");
130         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
131 
132         verify(checkConfig, getPath("InputCyclomaticComplexitySwitchBlocks.java"), expected);
133     }
134 }