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.AnnotatedDeclarationVisibilityCheck.MSG_KEY;
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.checks.naming.AccessModifierOption;
30  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
31  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
32  
33  public class AnnotatedDeclarationVisibilityCheckTest
34      extends AbstractModuleTestSupport {
35  
36      @Override
37      public String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/modifier/annotateddeclarationvisibility";
39      }
40  
41      @Test
42      public void testGetRequiredTokens() {
43          final AnnotatedDeclarationVisibilityCheck annotatedMethodVisibilityModifierCheck =
44                  new AnnotatedDeclarationVisibilityCheck();
45          final int[] actual = annotatedMethodVisibilityModifierCheck.getRequiredTokens();
46          final int[] expected = {
47              TokenTypes.PACKAGE_DEF,
48              TokenTypes.IMPORT,
49          };
50          assertWithMessage("Invalid required tokens")
51              .that(actual)
52              .isEqualTo(expected);
53      }
54  
55      @Test
56      public void testCloneInVisibilityProperty() {
57          final AccessModifierOption[] input = {
58              AccessModifierOption.PACKAGE,
59          };
60          final AnnotatedDeclarationVisibilityCheck check =
61                  new AnnotatedDeclarationVisibilityCheck();
62          check.setVisibility(input);
63  
64          assertWithMessage("check creates its own instance of visibility array")
65              .that(TestUtil.getInternalState(
66                      check, "visibility", AccessModifierOption[].class))
67              .isNotSameInstanceAs(input);
68      }
69  
70      @Test
71      public void testBasic() throws Exception {
72          final String[] expected = {
73              "23:5: " + getCheckMessage(MSG_KEY, "public"),
74              "27:5: " + getCheckMessage(MSG_KEY, "private"),
75              "34:5: " + getCheckMessage(MSG_KEY, "public"),
76              "40:5: " + getCheckMessage(MSG_KEY, "public"),
77          };
78          verifyWithInlineConfigParser(
79                  getPath("InputAnnotatedDeclarationVisibilityBasic.java"),
80              expected);
81      }
82  
83      @Test
84      public void testSimpleImport() throws Exception {
85          final String[] expected = {
86              "22:5: " + getCheckMessage(MSG_KEY, "public"),
87              "26:5: " + getCheckMessage(MSG_KEY, "private"),
88              "33:5: " + getCheckMessage(MSG_KEY, "public"),
89          };
90  
91          verifyWithInlineConfigParser(
92                  getPath("InputAnnotatedDeclarationVisibilitySimpleImport.java"),
93              expected);
94      }
95  
96      @Test
97      public void testStarImport() throws Exception {
98          final String[] expected = {
99              "25:5: " + getCheckMessage(MSG_KEY, "public"),
100             "29:5: " + getCheckMessage(MSG_KEY, "private"),
101             "36:5: " + getCheckMessage(MSG_KEY, "public"),
102         };
103 
104         verifyWithInlineConfigParser(
105                 getPath("InputAnnotatedDeclarationVisibilityStarImport.java"),
106             expected);
107     }
108 
109     @Test
110     public void testCustomVisibility() throws Exception {
111         final String[] expected = {
112             "20:5: " + getCheckMessage(MSG_KEY, "protected"),
113             "24:5: " + getCheckMessage(MSG_KEY, "private"),
114             "28:5: " + getCheckMessage(MSG_KEY, "package-private"),
115             "35:5: " + getCheckMessage(MSG_KEY, "protected"),
116         };
117 
118         verifyWithInlineConfigParser(
119             getPath("InputAnnotatedDeclarationVisibilityCustomVisibility.java"),
120             expected);
121     }
122 
123     @Test
124     public void testMultipleConfiguredAnnotations() throws Exception {
125         final String[] expected = {
126             "25:5: " + getCheckMessage(MSG_KEY, "public"),
127             "32:5: " + getCheckMessage(MSG_KEY, "private"),
128             "46:5: " + getCheckMessage(MSG_KEY, "public"),
129             "51:5: " + getCheckMessage(MSG_KEY, "private"),
130             "59:5: " + getCheckMessage(MSG_KEY, "public"),
131         };
132 
133         verifyWithInlineConfigParser(
134             getPath("InputAnnotatedDeclarationVisibilityMultipleConfigured.java"),
135             expected);
136     }
137 
138     @Test
139     public void testInterfaceImplicitVisibility() throws Exception {
140         final String[] expected = {
141             "19:5: " + getCheckMessage(MSG_KEY, "public"),
142             "23:5: " + getCheckMessage(MSG_KEY, "public"),
143             "27:5: " + getCheckMessage(MSG_KEY, "public"),
144             "31:5: " + getCheckMessage(MSG_KEY, "public"),
145         };
146 
147         verifyWithInlineConfigParser(
148             getPath("InputAnnotatedDeclarationVisibilityInterfaceImplicit.java"),
149             expected);
150     }
151 
152     @Test
153     public void testLocalVariablesIgnored() throws Exception {
154         final String[] expected = {
155             "19:5: " + getCheckMessage(MSG_KEY, "public"),
156         };
157 
158         verifyWithInlineConfigParser(
159             getPath("InputAnnotatedDeclarationVisibilityLocalVariables.java"),
160             expected);
161     }
162 
163     @Test
164     public void testAllDefinitionTypes() throws Exception {
165         final String[] expected = {
166             "23:5: " + getCheckMessage(MSG_KEY, "public"),
167             "27:5: " + getCheckMessage(MSG_KEY, "private"),
168             "35:5: " + getCheckMessage(MSG_KEY, "public"),
169             "43:5: " + getCheckMessage(MSG_KEY, "public"),
170             "51:5: " + getCheckMessage(MSG_KEY, "public"),
171             "59:5: " + getCheckMessage(MSG_KEY, "public"),
172             "69:5: " + getCheckMessage(MSG_KEY, "public"),
173             "79:5: " + getCheckMessage(MSG_KEY, "private"),
174             "87:5: " + getCheckMessage(MSG_KEY, "public"),
175         };
176 
177         verifyWithInlineConfigParser(
178             getPath("InputAnnotatedDeclarationVisibilityAllDefinitions.java"),
179             expected);
180     }
181 
182     @Test
183     public void testSamePackageAnnotation() throws Exception {
184         final String[] expected = {
185             "21:5: " + getCheckMessage(MSG_KEY, "public"),
186             "25:5: " + getCheckMessage(MSG_KEY, "private"),
187             "32:5: " + getCheckMessage(MSG_KEY, "public"),
188         };
189 
190         verifyWithInlineConfigParser(
191             getPath("InputAnnotatedDeclarationVisibilitySamePackage.java"),
192             expected);
193     }
194 
195     @Test
196     public void testNonDefaultTokens() throws Exception {
197         final String[] expected = {
198             "19:5: " + getCheckMessage(MSG_KEY, "protected"),
199         };
200 
201         verifyWithInlineConfigParser(
202             getPath("InputAnnotatedDeclarationVisibilityNonDefaultTokens.java"),
203             expected);
204     }
205 
206     @Test
207     public void testImport() throws Exception {
208         final String[] expected = {
209             "24:5: " + getCheckMessage(MSG_KEY, "public"),
210             "28:5: " + getCheckMessage(MSG_KEY, "private"),
211         };
212 
213         verifyWithInlineConfigParser(
214             getPath("InputAnnotatedDeclarationVisibilityImport.java"),
215             expected);
216     }
217 
218     @Test
219     public void testImportsClearedOnBeginTree() throws Exception {
220         final String[] expected = {
221             "22:5: " + getCheckMessage(MSG_KEY, "private"),
222             "26:5: " + getCheckMessage(MSG_KEY, "public"),
223         };
224         verifyWithInlineConfigParser(
225             getPath("InputAnnotatedDeclarationVisibilityClear1.java"),
226             getPath("InputAnnotatedDeclarationVisibilityClear2.java"),
227                 expected);
228     }
229 
230     @Test
231     public void testCompactSourceFile() throws Exception {
232         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
233 
234         verifyWithInlineConfigParser(
235             getNonCompilablePath(
236                     "compact/InputAnnotatedDeclarationVisibilityCompactSourceFile.java"),
237             expected);
238     }
239 
240     @Test
241     public void testCompactSourceFileCustom() throws Exception {
242         final String[] expected = {
243             "18:1: " + getCheckMessage(MSG_KEY, "private"),
244             "23:1: " + getCheckMessage(MSG_KEY, "public"),
245             "27:1: " + getCheckMessage(MSG_KEY, "package-private"),
246         };
247 
248         verifyWithInlineConfigParser(
249             getNonCompilablePath(
250                     "compact/InputAnnotatedDeclarationVisibilityCompactSourceFileCustom.java"),
251             expected);
252     }
253 
254 }