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.modifier;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.modifier.InterfaceMemberImpliedModifierCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DetailAstImpl;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31  
32  public class ClassMemberImpliedModifierCheckTest
33      extends AbstractModuleTestSupport {
34  
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/modifier/classmemberimpliedmodifier";
38      }
39  
40      @Test
41      public void testMethodsOnClass() throws Exception {
42          final String[] expected = {
43              "51:9: " + getCheckMessage(MSG_KEY, "static"),
44              "58:9: " + getCheckMessage(MSG_KEY, "static"),
45              "62:5: " + getCheckMessage(MSG_KEY, "static"),
46              "69:9: " + getCheckMessage(MSG_KEY, "static"),
47              "76:9: " + getCheckMessage(MSG_KEY, "static"),
48              "83:5: " + getCheckMessage(MSG_KEY, "static"),
49          };
50          verifyWithInlineConfigParser(
51                  getPath("InputClassMemberImpliedModifierOnClass.java"),
52              expected);
53      }
54  
55      @Test
56      public void testGetRequiredTokens() {
57          final ClassMemberImpliedModifierCheck check = new ClassMemberImpliedModifierCheck();
58          final int[] actual = check.getRequiredTokens();
59          final int[] expected = {
60              TokenTypes.INTERFACE_DEF,
61              TokenTypes.ENUM_DEF,
62              TokenTypes.RECORD_DEF,
63          };
64          assertWithMessage("Required tokens are invalid")
65              .that(actual)
66              .isEqualTo(expected);
67      }
68  
69      @Test
70      public void testMethodsOnClassNoImpliedStaticEnum() throws Exception {
71          final String[] expected = {
72              "59:9: " + getCheckMessage(MSG_KEY, "static"),
73              "77:9: " + getCheckMessage(MSG_KEY, "static"),
74              "84:5: " + getCheckMessage(MSG_KEY, "static"),
75          };
76          verifyWithInlineConfigParser(
77                  getPath("InputClassMemberImpliedModifierOnClassSetEnumFalse.java"),
78              expected);
79      }
80  
81      @Test
82      public void testMethodsOnClassNoImpliedStaticInterface() throws Exception {
83          final String[] expected = {
84              "52:9: " + getCheckMessage(MSG_KEY, "static"),
85              "63:5: " + getCheckMessage(MSG_KEY, "static"),
86              "70:9: " + getCheckMessage(MSG_KEY, "static"),
87          };
88          verifyWithInlineConfigParser(
89                  getPath("InputClassMemberImpliedModifierOnClassSetInterfaceFalse.java"),
90              expected);
91      }
92  
93      @Test
94      public void testMethodsOnClassNoViolationsChecked() throws Exception {
95          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
96          verifyWithInlineConfigParser(
97                  getPath("InputClassMemberImpliedModifierOnClassNoViolations.java"),
98              expected);
99      }
100 
101     @Test
102     public void testMethodsOnInterface() throws Exception {
103         final String[] expected = {
104             "60:13: " + getCheckMessage(MSG_KEY, "static"),
105             "67:13: " + getCheckMessage(MSG_KEY, "static"),
106             "71:9: " + getCheckMessage(MSG_KEY, "static"),
107             "78:13: " + getCheckMessage(MSG_KEY, "static"),
108             "85:13: " + getCheckMessage(MSG_KEY, "static"),
109             "92:9: " + getCheckMessage(MSG_KEY, "static"),
110         };
111         verifyWithInlineConfigParser(
112                 getPath("InputClassMemberImpliedModifierOnInterface.java"),
113             expected);
114     }
115 
116     @Test
117     public void testClassMemberImpliedModifierRecords() throws Exception {
118         final String[] expected = {
119             "16:5: " + getCheckMessage(MSG_KEY, "static"),
120             "20:5: " + getCheckMessage(MSG_KEY, "static"),
121             "24:5: " + getCheckMessage(MSG_KEY, "static"),
122             "29:9: " + getCheckMessage(MSG_KEY, "static"),
123             "33:9: " + getCheckMessage(MSG_KEY, "static"),
124             "37:9: " + getCheckMessage(MSG_KEY, "static"),
125             "42:9: " + getCheckMessage(MSG_KEY, "static"),
126         };
127         verifyWithInlineConfigParser(
128                 getNonCompilablePath("InputClassMemberImpliedModifierRecords.java"),
129                 expected);
130     }
131 
132     @Test
133     public void testClassMemberImpliedModifierNoViolationRecords() throws Exception {
134         final String[] expected = {
135             "16:5: " + getCheckMessage(MSG_KEY, "static"),
136             "20:5: " + getCheckMessage(MSG_KEY, "static"),
137             "33:9: " + getCheckMessage(MSG_KEY, "static"),
138             "37:9: " + getCheckMessage(MSG_KEY, "static"),
139         };
140         verifyWithInlineConfigParser(
141                 getNonCompilablePath(
142                         "InputClassMemberImpliedModifierNoViolationRecords.java"),
143                 expected);
144     }
145 
146     @Test
147     public void testIllegalState() {
148         final DetailAstImpl init = new DetailAstImpl();
149         init.setType(TokenTypes.STATIC_INIT);
150         final DetailAstImpl objBlock = new DetailAstImpl();
151         objBlock.setType(TokenTypes.OBJBLOCK);
152         objBlock.addChild(init);
153         final DetailAstImpl interfaceAst = new DetailAstImpl();
154         interfaceAst.setType(TokenTypes.CLASS_DEF);
155         interfaceAst.addChild(objBlock);
156         final ClassMemberImpliedModifierCheck check =
157             new ClassMemberImpliedModifierCheck();
158         try {
159             check.visitToken(init);
160             assertWithMessage("IllegalStateException is expected").fail();
161         }
162         catch (IllegalStateException ex) {
163             assertWithMessage("Error message is unexpected")
164                 .that(ex.getMessage())
165                 .isEqualTo(init.toString());
166         }
167     }
168 
169 }