View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.design;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.design.VisibilityModifierCheck.MSG_KEY;
23  
24  import java.io.File;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
29  import org.junit.jupiter.api.Test;
30  
31  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32  import com.puppycrawl.tools.checkstyle.checks.design.VisibilityModifierCheck;
33  
34  public class XpathRegressionVisibilityModifierTest extends AbstractXpathTestSupport {
35  
36      private final String checkName = VisibilityModifierCheck.class.getSimpleName();
37  
38      @Override
39      protected String getCheckName() {
40          return checkName;
41      }
42  
43      @Override
44      protected String getPackageLocation() {
45          return "org/checkstyle/suppressionxpathfilter/design/visibilitymodifier";
46      }
47  
48      @Test
49      public void testDefaultModifier() throws Exception {
50          final File fileToProcess =
51                  new File(getPath("InputXpathVisibilityModifierDefault.java"));
52  
53          final DefaultConfiguration moduleConfig =
54                  createModuleConfig(VisibilityModifierCheck.class);
55  
56          final String[] expectedViolation = {
57              "6:9: " + getCheckMessage(VisibilityModifierCheck.class, MSG_KEY, "field"),
58          };
59  
60          final List<String> expectedXpathQueries = Collections.singletonList(
61                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
62                          + "@text='InputXpathVisibilityModifierDefault']]"
63                          + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='field']"
64          );
65  
66          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
67      }
68  
69      @Test
70      public void testAnnotation() throws Exception {
71          final File fileToProcess =
72                  new File(getPath("InputXpathVisibilityModifierAnnotation.java"));
73  
74          final DefaultConfiguration moduleConfig =
75                  createModuleConfig(VisibilityModifierCheck.class);
76          moduleConfig.addProperty("ignoreAnnotationCanonicalNames", "Deprecated");
77  
78          final String[] expectedViolation = {
79              "5:12: " + getCheckMessage(VisibilityModifierCheck.class, MSG_KEY,
80                      "annotatedString"),
81          };
82  
83          final List<String> expectedXpathQueries = Collections.singletonList(
84                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
85                          + "@text='InputXpathVisibilityModifierAnnotation']]"
86                          + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='annotatedString']"
87          );
88  
89          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
90      }
91  
92      @Test
93      public void testAnonymousClass() throws Exception {
94          final File fileToProcess =
95                  new File(getPath("InputXpathVisibilityModifierAnonymous.java"));
96  
97          final DefaultConfiguration moduleConfig =
98                  createModuleConfig(VisibilityModifierCheck.class);
99  
100         final String[] expectedViolation = {
101             "6:23: " + getCheckMessage(VisibilityModifierCheck.class, MSG_KEY, "field1"),
102         };
103 
104         final List<String> expectedXpathQueries = Collections.singletonList(
105                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
106                         + "@text='InputXpathVisibilityModifierAnonymous']]"
107                         + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='runnable']]"
108                         + "/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='Runnable']]"
109                         + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='field1']"
110         );
111 
112         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
113     }
114 
115     @Test
116     public void testInnerClass() throws Exception {
117         final File fileToProcess =
118                 new File(getPath("InputXpathVisibilityModifierInner.java"));
119 
120         final DefaultConfiguration moduleConfig =
121                 createModuleConfig(VisibilityModifierCheck.class);
122 
123         final String[] expectedViolation = {
124             "7:20: " + getCheckMessage(VisibilityModifierCheck.class, MSG_KEY, "field2"),
125         };
126 
127         final List<String> expectedXpathQueries = Collections.singletonList(
128                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
129                         + "@text='InputXpathVisibilityModifierInner']]"
130                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClass']]/OBJBLOCK/"
131                         + "VARIABLE_DEF/IDENT[@text='field2']"
132         );
133 
134         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
135     }
136 }