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.JavaNCSSCheck.MSG_CLASS;
24  import static com.puppycrawl.tools.checkstyle.checks.metrics.JavaNCSSCheck.MSG_FILE;
25  import static com.puppycrawl.tools.checkstyle.checks.metrics.JavaNCSSCheck.MSG_METHOD;
26  import static com.puppycrawl.tools.checkstyle.checks.metrics.JavaNCSSCheck.MSG_RECORD;
27  
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
33  
34  /**
35   * Test case for the JavaNCSS-Check.
36   *
37   */
38  // -@cs[AbbreviationAsWordInName] Test should be named as its main class.
39  public class JavaNCSSCheckTest extends AbstractModuleTestSupport {
40  
41      @Override
42      public String getPackageLocation() {
43          return "com/puppycrawl/tools/checkstyle/checks/metrics/javancss";
44      }
45  
46      @Test
47      public void test() throws Exception {
48  
49          final String[] expected = {
50              "12:1: " + getCheckMessage(MSG_FILE, 39, 2),
51              "19:1: " + getCheckMessage(MSG_CLASS, 22, 1),
52              "24:3: " + getCheckMessage(MSG_METHOD, 2, 0),
53              "31:3: " + getCheckMessage(MSG_METHOD, 4, 0),
54              "40:3: " + getCheckMessage(MSG_METHOD, 12, 0),
55              "52:7: " + getCheckMessage(MSG_METHOD, 2, 0),
56              "59:3: " + getCheckMessage(MSG_CLASS, 2, 1),
57              "66:1: " + getCheckMessage(MSG_CLASS, 10, 1),
58              "71:3: " + getCheckMessage(MSG_METHOD, 8, 0),
59              "90:1: " + getCheckMessage(MSG_CLASS, 4, 1),
60              "91:3: " + getCheckMessage(MSG_METHOD, 1, 0),
61              "92:3: " + getCheckMessage(MSG_METHOD, 1, 0),
62              "93:3: " + getCheckMessage(MSG_METHOD, 1, 0),
63          };
64  
65          verifyWithInlineConfigParser(
66                  getPath("InputJavaNCSS.java"), expected);
67      }
68  
69      @Test
70      public void testEqualToMax() throws Exception {
71  
72          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
73  
74          verifyWithInlineConfigParser(
75                  getPath("InputJavaNCSS2.java"), expected);
76      }
77  
78      @Test
79      public void testDefaultConfiguration() throws Exception {
80          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
81          verifyWithInlineConfigParser(
82                  getPath("InputJavaNCSS3.java"), expected);
83      }
84  
85      @Test
86      public void testRecordsAndCompactCtors() throws Exception {
87  
88          final String[] expected = {
89              "12:1: " + getCheckMessage(MSG_FILE, 55, 2),
90              "14:1: " + getCheckMessage(MSG_CLASS, 54, 3),
91              "16:3: " + getCheckMessage(MSG_CLASS, 7, 3),
92              "34:3: " + getCheckMessage(MSG_RECORD, 6, 4),
93              "43:3: " + getCheckMessage(MSG_RECORD, 15, 4),
94              "54:5: " + getCheckMessage(MSG_METHOD, 8, 7),
95              "73:3: " + getCheckMessage(MSG_RECORD, 6, 4),
96              "106:3: " + getCheckMessage(MSG_RECORD, 10, 4),
97              "107:5: " + getCheckMessage(MSG_METHOD, 9, 7),
98          };
99  
100         verifyWithInlineConfigParser(
101                 getPath("InputJavaNCSSRecordsAndCompactCtors.java"),
102                 expected);
103     }
104 
105     @Test
106     public void testForMutation() throws Exception {
107         final String[] expected = {
108             "13:1: " + getCheckMessage(MSG_CLASS, 84, 80),
109             "16:5: " + getCheckMessage(MSG_CLASS, 83, 80),
110         };
111         verifyWithInlineConfigParser(
112                 getPath("InputJavaNCSSResolveMutation.java"),
113                 expected);
114     }
115 
116     @Test
117     public void testRecordMax() throws Exception {
118         final String[] expected = {
119             "14:1: " + getCheckMessage(MSG_CLASS, 105, 80),
120             "15:5: " + getCheckMessage(MSG_RECORD, 104, 100),
121         };
122 
123         verifyWithInlineConfigParser(
124                 getPath("InputJavaNCSSRecordsMax.java"),
125                 expected);
126     }
127 
128     @Test
129     public void testGetAcceptableTokens() {
130         final JavaNCSSCheck javaNcssCheckObj = new JavaNCSSCheck();
131         final int[] actual = javaNcssCheckObj.getAcceptableTokens();
132         final int[] expected = {
133             TokenTypes.CLASS_DEF,
134             TokenTypes.INTERFACE_DEF,
135             TokenTypes.METHOD_DEF,
136             TokenTypes.CTOR_DEF,
137             TokenTypes.INSTANCE_INIT,
138             TokenTypes.STATIC_INIT,
139             TokenTypes.PACKAGE_DEF,
140             TokenTypes.IMPORT,
141             TokenTypes.VARIABLE_DEF,
142             TokenTypes.CTOR_CALL,
143             TokenTypes.SUPER_CTOR_CALL,
144             TokenTypes.LITERAL_IF,
145             TokenTypes.LITERAL_ELSE,
146             TokenTypes.LITERAL_WHILE,
147             TokenTypes.LITERAL_DO,
148             TokenTypes.LITERAL_FOR,
149             TokenTypes.LITERAL_SWITCH,
150             TokenTypes.LITERAL_BREAK,
151             TokenTypes.LITERAL_CONTINUE,
152             TokenTypes.LITERAL_RETURN,
153             TokenTypes.LITERAL_THROW,
154             TokenTypes.LITERAL_SYNCHRONIZED,
155             TokenTypes.LITERAL_CATCH,
156             TokenTypes.LITERAL_FINALLY,
157             TokenTypes.EXPR,
158             TokenTypes.LABELED_STAT,
159             TokenTypes.LITERAL_CASE,
160             TokenTypes.LITERAL_DEFAULT,
161             TokenTypes.RECORD_DEF,
162             TokenTypes.COMPACT_CTOR_DEF,
163         };
164         assertWithMessage("Acceptable tokens should not be null")
165             .that(actual)
166             .isNotNull();
167         assertWithMessage("Invalid acceptable tokens")
168             .that(actual)
169             .isEqualTo(expected);
170     }
171 
172     @Test
173     public void testGetRequiredTokens() {
174         final JavaNCSSCheck javaNcssCheckObj = new JavaNCSSCheck();
175         final int[] actual = javaNcssCheckObj.getRequiredTokens();
176         final int[] expected = {
177             TokenTypes.CLASS_DEF,
178             TokenTypes.INTERFACE_DEF,
179             TokenTypes.METHOD_DEF,
180             TokenTypes.CTOR_DEF,
181             TokenTypes.INSTANCE_INIT,
182             TokenTypes.STATIC_INIT,
183             TokenTypes.PACKAGE_DEF,
184             TokenTypes.IMPORT,
185             TokenTypes.VARIABLE_DEF,
186             TokenTypes.CTOR_CALL,
187             TokenTypes.SUPER_CTOR_CALL,
188             TokenTypes.LITERAL_IF,
189             TokenTypes.LITERAL_ELSE,
190             TokenTypes.LITERAL_WHILE,
191             TokenTypes.LITERAL_DO,
192             TokenTypes.LITERAL_FOR,
193             TokenTypes.LITERAL_SWITCH,
194             TokenTypes.LITERAL_BREAK,
195             TokenTypes.LITERAL_CONTINUE,
196             TokenTypes.LITERAL_RETURN,
197             TokenTypes.LITERAL_THROW,
198             TokenTypes.LITERAL_SYNCHRONIZED,
199             TokenTypes.LITERAL_CATCH,
200             TokenTypes.LITERAL_FINALLY,
201             TokenTypes.EXPR,
202             TokenTypes.LABELED_STAT,
203             TokenTypes.LITERAL_CASE,
204             TokenTypes.LITERAL_DEFAULT,
205             TokenTypes.RECORD_DEF,
206             TokenTypes.COMPACT_CTOR_DEF,
207         };
208         assertWithMessage("Required tokens should not be null")
209             .that(actual)
210             .isNotNull();
211         assertWithMessage("Invalid required tokens")
212             .that(actual)
213             .isEqualTo(expected);
214     }
215 
216 }