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.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      protected 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              getNonCompilablePath(
80                      "InputUnnecessarySemicolonAfterTypeMemberDeclarationRecords.java"),
81              expected);
82      }
83  
84      @Test
85      public void testTokens() {
86          final UnnecessarySemicolonAfterTypeMemberDeclarationCheck check =
87              new UnnecessarySemicolonAfterTypeMemberDeclarationCheck();
88          final int[] expected = {
89              TokenTypes.CLASS_DEF,
90              TokenTypes.INTERFACE_DEF,
91              TokenTypes.ENUM_DEF,
92              TokenTypes.ANNOTATION_DEF,
93              TokenTypes.VARIABLE_DEF,
94              TokenTypes.ANNOTATION_FIELD_DEF,
95              TokenTypes.STATIC_INIT,
96              TokenTypes.INSTANCE_INIT,
97              TokenTypes.CTOR_DEF,
98              TokenTypes.METHOD_DEF,
99              TokenTypes.ENUM_CONSTANT_DEF,
100             TokenTypes.COMPACT_CTOR_DEF,
101             TokenTypes.RECORD_DEF,
102         };
103         assertWithMessage("Acceptable required tokens are invalid")
104             .that(check.getAcceptableTokens())
105             .isEqualTo(expected);
106         assertWithMessage("Default required tokens are invalid")
107             .that(check.getDefaultTokens())
108             .isEqualTo(expected);
109         assertWithMessage("Required required tokens are invalid")
110             .that(check.getRequiredTokens())
111             .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
112     }
113 
114     @Test
115     public void testIsSemicolonWithNullAst() throws Exception {
116         final String[] expected = {"24:32: " + getCheckMessage(MSG_SEMI)};
117 
118         verifyWithInlineConfigParser(
119                 getPath("InputUnnecessarySemicolonAfterTypeMemberDeclarationNullAst.java"),
120                 expected);
121     }
122 }