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 org.checkstyle.suppressionxpathfilter.modifier;
21  
22  import java.io.File;
23  import java.util.List;
24  
25  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.modifier.AnnotatedDeclarationVisibilityCheck;
30  
31  public class XpathRegressionAnnotatedDeclarationVisibilityTest extends AbstractXpathTestSupport {
32  
33      private final String checkName = AnnotatedDeclarationVisibilityCheck.class.getSimpleName();
34  
35      @Override
36      protected String getCheckName() {
37          return checkName;
38      }
39  
40      @Override
41      public String getPackageLocation() {
42          return "org/checkstyle/suppressionxpathfilter/modifier/annotateddeclarationvisibility";
43      }
44  
45      @Test
46      public void testPublicModifier() throws Exception {
47          final File fileToProcess =
48                  new File(getPath("InputXpathAnnotatedDeclarationVisibilityPublic.java"));
49  
50          final DefaultConfiguration moduleConfig =
51                  createModuleConfig(AnnotatedDeclarationVisibilityCheck.class);
52  
53          final String[] expectedViolation = {
54              "5:5: " + getCheckMessage(AnnotatedDeclarationVisibilityCheck.class,
55                      AnnotatedDeclarationVisibilityCheck.MSG_KEY, "public"),
56          };
57  
58          final String methodXpath =
59              "/COMPILATION_UNIT/CLASS_DEF"
60                  + "[./IDENT[@text='InputXpathAnnotatedDeclarationVisibilityPublic']]"
61                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='violationMethod']]";
62  
63          final List<String> expectedXpathQueries = List.of(
64              methodXpath,
65              methodXpath + "/MODIFIERS",
66              methodXpath + "/MODIFIERS/ANNOTATION",
67              methodXpath + "/MODIFIERS/ANNOTATION/AT"
68          );
69  
70          runVerifications(moduleConfig, fileToProcess, expectedViolation,
71                  expectedXpathQueries);
72      }
73  
74      @Test
75      public void testPrivateModifier() throws Exception {
76          final File fileToProcess =
77                  new File(getPath("InputXpathAnnotatedDeclarationVisibilityPrivate.java"));
78  
79          final DefaultConfiguration moduleConfig =
80                  createModuleConfig(AnnotatedDeclarationVisibilityCheck.class);
81  
82          final String[] expectedViolation = {
83              "6:9: " + getCheckMessage(AnnotatedDeclarationVisibilityCheck.class,
84                      AnnotatedDeclarationVisibilityCheck.MSG_KEY, "private"),
85          };
86  
87          final String methodXpath =
88              "/COMPILATION_UNIT/CLASS_DEF"
89                  + "[./IDENT[@text='InputXpathAnnotatedDeclarationVisibilityPrivate']]"
90                  + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]"
91                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='violationMethod']]";
92  
93          final List<String> expectedXpathQueries = List.of(
94              methodXpath,
95              methodXpath + "/MODIFIERS",
96              methodXpath + "/MODIFIERS/ANNOTATION",
97              methodXpath + "/MODIFIERS/ANNOTATION/AT"
98          );
99  
100         runVerifications(moduleConfig, fileToProcess, expectedViolation,
101                 expectedXpathQueries);
102     }
103 
104     @Test
105     public void testPackagePrivateModifier() throws Exception {
106         final File fileToProcess =
107                 new File(getPath("InputXpathAnnotatedDeclarationVisibilityPackagePrivate.java"));
108 
109         final DefaultConfiguration moduleConfig =
110                 createModuleConfig(AnnotatedDeclarationVisibilityCheck.class);
111 
112         moduleConfig.addProperty("visibility", "public");
113 
114         final String[] expectedViolation = {
115             "6:9: " + getCheckMessage(AnnotatedDeclarationVisibilityCheck.class,
116                     AnnotatedDeclarationVisibilityCheck.MSG_KEY, "package-private"),
117         };
118 
119         final String methodXpath =
120             "/COMPILATION_UNIT/CLASS_DEF"
121                 + "[./IDENT[@text='InputXpathAnnotatedDeclarationVisibilityPackagePrivate']]"
122                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='object']]"
123                 + "/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='Object']]/OBJBLOCK"
124                 + "/METHOD_DEF[./IDENT[@text='violationMethod']]";
125 
126         final List<String> expectedXpathQueries = List.of(
127             methodXpath,
128             methodXpath + "/MODIFIERS",
129             methodXpath + "/MODIFIERS/ANNOTATION",
130             methodXpath + "/MODIFIERS/ANNOTATION/AT"
131         );
132 
133         runVerifications(moduleConfig, fileToProcess, expectedViolation,
134                 expectedXpathQueries);
135     }
136 
137     @Test
138     public void testNestedClassDeclaration() throws Exception {
139         final File fileToProcess =
140                 new File(getPath("InputXpathAnnotatedDeclarationVisibilityNestedClass.java"));
141 
142         final DefaultConfiguration moduleConfig =
143                 createModuleConfig(AnnotatedDeclarationVisibilityCheck.class);
144 
145         final String[] expectedViolation = {
146             "5:5: " + getCheckMessage(AnnotatedDeclarationVisibilityCheck.class,
147                     AnnotatedDeclarationVisibilityCheck.MSG_KEY, "public"),
148         };
149 
150         final String declarationXpath =
151             "/COMPILATION_UNIT/CLASS_DEF"
152                 + "[./IDENT[@text='InputXpathAnnotatedDeclarationVisibilityNestedClass']]"
153                 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='ViolationNestedClass']]";
154 
155         final List<String> expectedXpathQueries = List.of(
156             declarationXpath,
157             declarationXpath + "/MODIFIERS",
158             declarationXpath + "/MODIFIERS/ANNOTATION",
159             declarationXpath + "/MODIFIERS/ANNOTATION/AT"
160         );
161 
162         runVerifications(moduleConfig, fileToProcess, expectedViolation,
163                 expectedXpathQueries);
164     }
165 
166     @Test
167     public void testAnonymousClassField() throws Exception {
168         final File fileToProcess =
169                 new File(getPath("InputXpathAnnotatedDeclarationVisibilityAnonymousField.java"));
170 
171         final DefaultConfiguration moduleConfig =
172                 createModuleConfig(AnnotatedDeclarationVisibilityCheck.class);
173 
174         moduleConfig.addProperty("visibility", "public");
175 
176         final String[] expectedViolation = {
177             "6:9: " + getCheckMessage(AnnotatedDeclarationVisibilityCheck.class,
178                     AnnotatedDeclarationVisibilityCheck.MSG_KEY, "package-private"),
179         };
180 
181         final String declarationXpath =
182             "/COMPILATION_UNIT/CLASS_DEF"
183                 + "[./IDENT[@text='InputXpathAnnotatedDeclarationVisibilityAnonymousField']]"
184                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='object']]"
185                 + "/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='Object']]/OBJBLOCK"
186                 + "/VARIABLE_DEF[./IDENT[@text='violationField']]";
187 
188         final List<String> expectedXpathQueries = List.of(
189             declarationXpath,
190             declarationXpath + "/MODIFIERS",
191             declarationXpath + "/MODIFIERS/ANNOTATION",
192             declarationXpath + "/MODIFIERS/ANNOTATION/AT"
193         );
194 
195         runVerifications(moduleConfig, fileToProcess, expectedViolation,
196                 expectedXpathQueries);
197     }
198 
199 }