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.coding;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessarySemicolonAfterTypeMemberDeclarationCheck.MSG_SEMI;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  public class UnnecessarySemicolonAfterTypeMemberDeclarationCheckTest
32      extends AbstractModuleTestSupport {
33  
34      @Override
35      public String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/coding/"
37              + "unnecessarysemicolonaftertypememberdeclaration";
38      }
39  
40      @Test
41      public void testDefault() throws Exception {
42  
43          final String[] expected = {
44              "13:5: " + getCheckMessage(MSG_SEMI),
45              "15:21: " + getCheckMessage(MSG_SEMI),
46              "17:14: " + getCheckMessage(MSG_SEMI),
47              "19:60: " + getCheckMessage(MSG_SEMI),
48              "21:14: " + getCheckMessage(MSG_SEMI),
49              "23:20: " + getCheckMessage(MSG_SEMI),
50              "25:19: " + getCheckMessage(MSG_SEMI),
51              "27:15: " + getCheckMessage(MSG_SEMI),
52              "29:23: " + getCheckMessage(MSG_SEMI),
53              "31:15: " + getCheckMessage(MSG_SEMI),
54              "34:13: " + getCheckMessage(MSG_SEMI),
55              "46:5: " + getCheckMessage(MSG_SEMI),
56              "49:5: " + getCheckMessage(MSG_SEMI),
57              "52:20: " + getCheckMessage(MSG_SEMI),
58          };
59  
60          verifyWithInlineConfigParser(
61                  getPath("InputUnnecessarySemicolonAfterTypeMemberDeclaration.java"),
62              expected);
63      }
64  
65      @Test
66      public void testUnnecessarySemicolonAfterTypeMemberDeclarationRecords() throws Exception {
67  
68          final String[] expected = {
69              "14:5: " + getCheckMessage(MSG_SEMI),
70              "18:5: " + getCheckMessage(MSG_SEMI),
71              "22:5: " + getCheckMessage(MSG_SEMI),
72              "27:5: " + getCheckMessage(MSG_SEMI),
73              "33:5: " + getCheckMessage(MSG_SEMI),
74              "38:5: " + getCheckMessage(MSG_SEMI),
75              "40:5: " + getCheckMessage(MSG_SEMI),
76          };
77  
78          verifyWithInlineConfigParser(
79              getPath(
80                      "InputUnnecessarySemicolonAfterTypeMemberDeclarationRecords.java"),
81              expected);
82      }
83  
84      @Test
85      public void testCompactSourceFile() throws Exception {
86  
87          final String[] expected = {
88              "14:2: " + getCheckMessage(MSG_SEMI),
89              "18:2: " + getCheckMessage(MSG_SEMI),
90              "22:2: " + getCheckMessage(MSG_SEMI),
91              "26:2: " + getCheckMessage(MSG_SEMI),
92              "30:2: " + getCheckMessage(MSG_SEMI),
93              "36:6: " + getCheckMessage(MSG_SEMI),
94              "38:2: " + getCheckMessage(MSG_SEMI),
95              "41:5: " + getCheckMessage(MSG_SEMI),
96              "48:11: " + getCheckMessage(MSG_SEMI),
97              "52:2: " + getCheckMessage(MSG_SEMI),
98          };
99  
100         verifyWithInlineConfigParser(
101             getNonCompilablePath(
102                 "InputUnnecessarySemicolonAfterTypeMemberDeclarationCompactSourceFile.java"),
103             expected);
104     }
105 
106     @Test
107     public void testTokens() {
108         final UnnecessarySemicolonAfterTypeMemberDeclarationCheck check =
109             new UnnecessarySemicolonAfterTypeMemberDeclarationCheck();
110         final int[] expected = {
111             TokenTypes.CLASS_DEF,
112             TokenTypes.INTERFACE_DEF,
113             TokenTypes.ENUM_DEF,
114             TokenTypes.ANNOTATION_DEF,
115             TokenTypes.VARIABLE_DEF,
116             TokenTypes.ANNOTATION_FIELD_DEF,
117             TokenTypes.STATIC_INIT,
118             TokenTypes.INSTANCE_INIT,
119             TokenTypes.CTOR_DEF,
120             TokenTypes.METHOD_DEF,
121             TokenTypes.ENUM_CONSTANT_DEF,
122             TokenTypes.COMPACT_CTOR_DEF,
123             TokenTypes.RECORD_DEF,
124         };
125         assertWithMessage("Acceptable required tokens are invalid")
126             .that(check.getAcceptableTokens())
127             .isEqualTo(expected);
128         assertWithMessage("Default required tokens are invalid")
129             .that(check.getDefaultTokens())
130             .isEqualTo(expected);
131         assertWithMessage("Required required tokens are invalid")
132             .that(check.getRequiredTokens())
133             .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
134     }
135 
136     @Test
137     public void testIsSemicolonWithNullAst() throws Exception {
138         final String[] expected = {"24:32: " + getCheckMessage(MSG_SEMI)};
139 
140         verifyWithInlineConfigParser(
141                 getPath("InputUnnecessarySemicolonAfterTypeMemberDeclarationNullAst.java"),
142                 expected);
143     }
144 
145 }