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