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.JavaNCSSCheck.MSG_CLASS;
23  import static com.puppycrawl.tools.checkstyle.checks.metrics.JavaNCSSCheck.MSG_FILE;
24  import static com.puppycrawl.tools.checkstyle.checks.metrics.JavaNCSSCheck.MSG_METHOD;
25  
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
33  
34  /**
35   * Test case for the JavaNCSS-Check.
36   *
37   * @author Lars Ködderitzsch
38   */
39  // -@cs[AbbreviationAsWordInName] Test should be named as its main class.
40  public class JavaNCSSCheckTest extends AbstractModuleTestSupport {
41      @Override
42      protected String getPackageLocation() {
43          return "com/puppycrawl/tools/checkstyle/checks/metrics/javancss";
44      }
45  
46      @Test
47      public void test() throws Exception {
48          final DefaultConfiguration checkConfig = createModuleConfig(JavaNCSSCheck.class);
49  
50          checkConfig.addAttribute("methodMaximum", "0");
51          checkConfig.addAttribute("classMaximum", "1");
52          checkConfig.addAttribute("fileMaximum", "2");
53  
54          final String[] expected = {
55              "2:1: " + getCheckMessage(MSG_FILE, 39, 2),
56              "9:1: " + getCheckMessage(MSG_CLASS, 22, 1),
57              "14:5: " + getCheckMessage(MSG_METHOD, 2, 0),
58              "21:5: " + getCheckMessage(MSG_METHOD, 4, 0),
59              "30:5: " + getCheckMessage(MSG_METHOD, 12, 0),
60              "42:13: " + getCheckMessage(MSG_METHOD, 2, 0),
61              "49:5: " + getCheckMessage(MSG_CLASS, 2, 1),
62              "56:1: " + getCheckMessage(MSG_CLASS, 10, 1),
63              "61:5: " + getCheckMessage(MSG_METHOD, 8, 0),
64              "80:1: " + getCheckMessage(MSG_CLASS, 4, 1),
65              "81:5: " + getCheckMessage(MSG_METHOD, 1, 0),
66              "82:5: " + getCheckMessage(MSG_METHOD, 1, 0),
67              "83:5: " + getCheckMessage(MSG_METHOD, 1, 0),
68          };
69  
70          verify(checkConfig, getPath("InputJavaNCSS.java"), expected);
71      }
72  
73      @Test
74      public void testEqualToMax() throws Exception {
75          final DefaultConfiguration checkConfig = createModuleConfig(JavaNCSSCheck.class);
76  
77          checkConfig.addAttribute("methodMaximum", "12");
78          checkConfig.addAttribute("classMaximum", "22");
79          checkConfig.addAttribute("fileMaximum", "39");
80  
81          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
82  
83          verify(checkConfig, getPath("InputJavaNCSS.java"), expected);
84      }
85  
86      @Test
87      public void testDefaultConfiguration() throws Exception {
88          final DefaultConfiguration checkConfig = createModuleConfig(JavaNCSSCheck.class);
89  
90          createChecker(checkConfig);
91          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
92          verify(checkConfig, getPath("InputJavaNCSS.java"), expected);
93      }
94  
95      @Test
96      public void testGetAcceptableTokens() {
97          final JavaNCSSCheck javaNcssCheckObj = new JavaNCSSCheck();
98          final int[] actual = javaNcssCheckObj.getAcceptableTokens();
99          final int[] expected = {
100             TokenTypes.CLASS_DEF,
101             TokenTypes.INTERFACE_DEF,
102             TokenTypes.METHOD_DEF,
103             TokenTypes.CTOR_DEF,
104             TokenTypes.INSTANCE_INIT,
105             TokenTypes.STATIC_INIT,
106             TokenTypes.PACKAGE_DEF,
107             TokenTypes.IMPORT,
108             TokenTypes.VARIABLE_DEF,
109             TokenTypes.CTOR_CALL,
110             TokenTypes.SUPER_CTOR_CALL,
111             TokenTypes.LITERAL_IF,
112             TokenTypes.LITERAL_ELSE,
113             TokenTypes.LITERAL_WHILE,
114             TokenTypes.LITERAL_DO,
115             TokenTypes.LITERAL_FOR,
116             TokenTypes.LITERAL_SWITCH,
117             TokenTypes.LITERAL_BREAK,
118             TokenTypes.LITERAL_CONTINUE,
119             TokenTypes.LITERAL_RETURN,
120             TokenTypes.LITERAL_THROW,
121             TokenTypes.LITERAL_SYNCHRONIZED,
122             TokenTypes.LITERAL_CATCH,
123             TokenTypes.LITERAL_FINALLY,
124             TokenTypes.EXPR,
125             TokenTypes.LABELED_STAT,
126             TokenTypes.LITERAL_CASE,
127             TokenTypes.LITERAL_DEFAULT,
128         };
129         Assert.assertNotNull("Acceptable tokens should not be null", actual);
130         Assert.assertArrayEquals("Invalid acceptable tokens", expected, actual);
131     }
132 
133     @Test
134     public void testGetRequiredTokens() {
135         final JavaNCSSCheck javaNcssCheckObj = new JavaNCSSCheck();
136         final int[] actual = javaNcssCheckObj.getRequiredTokens();
137         final int[] expected = {
138             TokenTypes.CLASS_DEF,
139             TokenTypes.INTERFACE_DEF,
140             TokenTypes.METHOD_DEF,
141             TokenTypes.CTOR_DEF,
142             TokenTypes.INSTANCE_INIT,
143             TokenTypes.STATIC_INIT,
144             TokenTypes.PACKAGE_DEF,
145             TokenTypes.IMPORT,
146             TokenTypes.VARIABLE_DEF,
147             TokenTypes.CTOR_CALL,
148             TokenTypes.SUPER_CTOR_CALL,
149             TokenTypes.LITERAL_IF,
150             TokenTypes.LITERAL_ELSE,
151             TokenTypes.LITERAL_WHILE,
152             TokenTypes.LITERAL_DO,
153             TokenTypes.LITERAL_FOR,
154             TokenTypes.LITERAL_SWITCH,
155             TokenTypes.LITERAL_BREAK,
156             TokenTypes.LITERAL_CONTINUE,
157             TokenTypes.LITERAL_RETURN,
158             TokenTypes.LITERAL_THROW,
159             TokenTypes.LITERAL_SYNCHRONIZED,
160             TokenTypes.LITERAL_CATCH,
161             TokenTypes.LITERAL_FINALLY,
162             TokenTypes.EXPR,
163             TokenTypes.LABELED_STAT,
164             TokenTypes.LITERAL_CASE,
165             TokenTypes.LITERAL_DEFAULT,
166         };
167         Assert.assertNotNull("Required tokens should not be null", actual);
168         Assert.assertArrayEquals("Invalid required tokens", expected, actual);
169     }
170 }