View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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      protected 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, 89, 2),
90              "16:1: " + getCheckMessage(MSG_CLASS, 87, 3),
91              "18:3: " + getCheckMessage(MSG_CLASS, 7, 3),
92              "36:3: " + getCheckMessage(MSG_RECORD, 6, 4),
93              "45:3: " + getCheckMessage(MSG_RECORD, 15, 4),
94              "56:5: " + getCheckMessage(MSG_METHOD, 8, 7),
95              "75:3: " + getCheckMessage(MSG_RECORD, 6, 4),
96              "109:3: " + getCheckMessage(MSG_RECORD, 8, 4),
97              "130:3: " + getCheckMessage(MSG_CLASS, 11, 3),
98              "151:3: " + getCheckMessage(MSG_RECORD, 12, 4),
99              "152:5: " + getCheckMessage(MSG_METHOD, 11, 7),
100             "166:3: " + getCheckMessage(MSG_CLASS, 12, 3),
101             "167:5: " + getCheckMessage(MSG_METHOD, 11, 7),
102         };
103 
104         verifyWithInlineConfigParser(
105                 getNonCompilablePath("InputJavaNCSSRecordsAndCompactCtors.java"),
106                 expected);
107     }
108 
109     @Test
110     public void testForMutation() throws Exception {
111         final String[] expected = {
112             "13:1: " + getCheckMessage(MSG_CLASS, 84, 80),
113             "16:5: " + getCheckMessage(MSG_CLASS, 83, 80),
114         };
115         verifyWithInlineConfigParser(
116                 getPath("InputJavaNCSSResolveMutation.java"),
117                 expected);
118     }
119 
120     @Test
121     public void testRecordMax() throws Exception {
122         final String[] expected = {
123             "14:1: " + getCheckMessage(MSG_CLASS, 152, 80),
124             "15:5: " + getCheckMessage(MSG_RECORD, 151, 150),
125         };
126 
127         verifyWithInlineConfigParser(
128                 getNonCompilablePath("InputJavaNCSSRecordsMax.java"),
129                 expected);
130     }
131 
132     @Test
133     public void testGetAcceptableTokens() {
134         final JavaNCSSCheck javaNcssCheckObj = new JavaNCSSCheck();
135         final int[] actual = javaNcssCheckObj.getAcceptableTokens();
136         final int[] expected = {
137             TokenTypes.CLASS_DEF,
138             TokenTypes.INTERFACE_DEF,
139             TokenTypes.METHOD_DEF,
140             TokenTypes.CTOR_DEF,
141             TokenTypes.INSTANCE_INIT,
142             TokenTypes.STATIC_INIT,
143             TokenTypes.PACKAGE_DEF,
144             TokenTypes.IMPORT,
145             TokenTypes.VARIABLE_DEF,
146             TokenTypes.CTOR_CALL,
147             TokenTypes.SUPER_CTOR_CALL,
148             TokenTypes.LITERAL_IF,
149             TokenTypes.LITERAL_ELSE,
150             TokenTypes.LITERAL_WHILE,
151             TokenTypes.LITERAL_DO,
152             TokenTypes.LITERAL_FOR,
153             TokenTypes.LITERAL_SWITCH,
154             TokenTypes.LITERAL_BREAK,
155             TokenTypes.LITERAL_CONTINUE,
156             TokenTypes.LITERAL_RETURN,
157             TokenTypes.LITERAL_THROW,
158             TokenTypes.LITERAL_SYNCHRONIZED,
159             TokenTypes.LITERAL_CATCH,
160             TokenTypes.LITERAL_FINALLY,
161             TokenTypes.EXPR,
162             TokenTypes.LABELED_STAT,
163             TokenTypes.LITERAL_CASE,
164             TokenTypes.LITERAL_DEFAULT,
165             TokenTypes.RECORD_DEF,
166             TokenTypes.COMPACT_CTOR_DEF,
167         };
168         assertWithMessage("Acceptable tokens should not be null")
169             .that(actual)
170             .isNotNull();
171         assertWithMessage("Invalid acceptable tokens")
172             .that(actual)
173             .isEqualTo(expected);
174     }
175 
176     @Test
177     public void testGetRequiredTokens() {
178         final JavaNCSSCheck javaNcssCheckObj = new JavaNCSSCheck();
179         final int[] actual = javaNcssCheckObj.getRequiredTokens();
180         final int[] expected = {
181             TokenTypes.CLASS_DEF,
182             TokenTypes.INTERFACE_DEF,
183             TokenTypes.METHOD_DEF,
184             TokenTypes.CTOR_DEF,
185             TokenTypes.INSTANCE_INIT,
186             TokenTypes.STATIC_INIT,
187             TokenTypes.PACKAGE_DEF,
188             TokenTypes.IMPORT,
189             TokenTypes.VARIABLE_DEF,
190             TokenTypes.CTOR_CALL,
191             TokenTypes.SUPER_CTOR_CALL,
192             TokenTypes.LITERAL_IF,
193             TokenTypes.LITERAL_ELSE,
194             TokenTypes.LITERAL_WHILE,
195             TokenTypes.LITERAL_DO,
196             TokenTypes.LITERAL_FOR,
197             TokenTypes.LITERAL_SWITCH,
198             TokenTypes.LITERAL_BREAK,
199             TokenTypes.LITERAL_CONTINUE,
200             TokenTypes.LITERAL_RETURN,
201             TokenTypes.LITERAL_THROW,
202             TokenTypes.LITERAL_SYNCHRONIZED,
203             TokenTypes.LITERAL_CATCH,
204             TokenTypes.LITERAL_FINALLY,
205             TokenTypes.EXPR,
206             TokenTypes.LABELED_STAT,
207             TokenTypes.LITERAL_CASE,
208             TokenTypes.LITERAL_DEFAULT,
209             TokenTypes.RECORD_DEF,
210             TokenTypes.COMPACT_CTOR_DEF,
211         };
212         assertWithMessage("Required tokens should not be null")
213             .that(actual)
214             .isNotNull();
215         assertWithMessage("Invalid required tokens")
216             .that(actual)
217             .isEqualTo(expected);
218     }
219 
220 }