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.naming;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.naming.PackageNameCheck.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.naming.PackageNameCheck;
33  
34  public class XpathRegressionPackageNameTest extends AbstractXpathTestSupport {
35  
36      private final String checkName = PackageNameCheck.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/naming/packagename";
46      }
47  
48      @Test
49      public void testOne() throws Exception {
50  
51          final File fileToProcess =
52                  new File(getPath("InputXpathPackageNameOne.java"));
53  
54          final String pattern = "[A-Z]+";
55  
56          final DefaultConfiguration moduleConfig = createModuleConfig(PackageNameCheck.class);
57          moduleConfig.addProperty("format", pattern);
58  
59          final String[] expectedViolation = {
60              "1:9: " + getCheckMessage(PackageNameCheck.class, MSG_KEY,
61                      "org.checkstyle.suppressionxpathfilter.naming.packagename",
62                      pattern),
63          };
64  
65          final List<String> expectedXpathQueries = Collections.singletonList(
66                  "/COMPILATION_UNIT/PACKAGE_DEF/DOT"
67                          + "[./IDENT[@text='packagename']]/DOT"
68                          + "[./IDENT[@text='naming']]/DOT"
69                          + "[./IDENT[@text='suppressionxpathfilter']]"
70                          + "/DOT/IDENT[@text='org']"
71          );
72  
73          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
74  
75      }
76  
77      @Test
78      public void testThree() throws Exception {
79  
80          final File fileToProcess =
81                  new File(getNonCompilablePath("InputXpathPackageNameThree.java"));
82  
83          final String pattern = "^[a-z]+(\\.[a-z][a-z0-9]*)*$";
84  
85          final DefaultConfiguration moduleConfig = createModuleConfig(PackageNameCheck.class);
86          moduleConfig.addProperty("format", pattern);
87          final String[] expectedViolations = {
88              "1:9: " + getCheckMessage(PackageNameCheck.class, MSG_KEY,
89                      "org.checkstyle.suppressionxpathfilter.PACKAGENAME",
90                      pattern),
91          };
92  
93          final List<String> expectedXpathQueries = Collections.singletonList(
94                  "/COMPILATION_UNIT/PACKAGE_DEF/DOT[./IDENT"
95                          + "[@text='PACKAGENAME']]/DOT[./IDENT"
96                          + "[@text='suppressionxpathfilter']]"
97                          + "/DOT/IDENT[@text='org']"
98          );
99  
100         runVerifications(moduleConfig, fileToProcess, expectedViolations,
101                 expectedXpathQueries);
102     }
103 
104     @Test
105     public void testTwo() throws Exception {
106 
107         final File fileToProcess =
108                 new File(getPath("InputXpathPackageNameTwo.java"));
109 
110         final String pattern = "[A-Z]+";
111 
112         final DefaultConfiguration moduleConfig = createModuleConfig(PackageNameCheck.class);
113         moduleConfig.addProperty("format", pattern);
114 
115         final String[] expectedViolation = {
116             "2:9: " + getCheckMessage(PackageNameCheck.class, MSG_KEY,
117                     "org.checkstyle.suppressionxpathfilter.naming.packagename",
118                     pattern),
119         };
120 
121         final List<String> expectedXpathQueries = Collections.singletonList(
122                 "/COMPILATION_UNIT/PACKAGE_DEF/DOT"
123                         + "[./IDENT[@text='packagename']]/DOT"
124                         + "[./IDENT[@text='naming']]/DOT"
125                         + "[./IDENT[@text='suppressionxpathfilter']]"
126                         + "/DOT/IDENT[@text='org']"
127         );
128 
129         runVerifications(moduleConfig, fileToProcess, expectedViolation,
130                 expectedXpathQueries);
131     }
132 }