View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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;
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.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.checks.naming.PackageNameCheck;
32  
33  public class XpathRegressionPackageNameTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = PackageNameCheck.class.getSimpleName();
36  
37      @Override
38      protected String getCheckName() {
39          return checkName;
40      }
41  
42      @Test
43      public void testOne() throws Exception {
44  
45          final File fileToProcess =
46                  new File(getPath("InputXpathPackageNameOne.java"));
47  
48          final String pattern = "[A-Z]+";
49  
50          final DefaultConfiguration moduleConfig = createModuleConfig(PackageNameCheck.class);
51          moduleConfig.addProperty("format", pattern);
52  
53          final String[] expectedViolation = {
54              "1:9: " + getCheckMessage(PackageNameCheck.class, MSG_KEY,
55                      "org.checkstyle.suppressionxpathfilter.packagename",
56                      pattern),
57          };
58  
59          final List<String> expectedXpathQueries = Collections.singletonList(
60                  "/COMPILATION_UNIT/PACKAGE_DEF/DOT"
61                          + "[./IDENT[@text='packagename']]/DOT"
62                          + "[./IDENT[@text='suppressionxpathfilter']]"
63                          + "/DOT/IDENT[@text='org']"
64          );
65  
66          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
67  
68      }
69  
70      @Test
71      public void testThree() throws Exception {
72  
73          final File fileToProcess =
74                  new File(getNonCompilablePath("InputXpathPackageNameThree.java"));
75  
76          final String pattern = "^[a-z]+(\\.[a-z][a-z0-9]*)*$";
77  
78          final DefaultConfiguration moduleConfig = createModuleConfig(PackageNameCheck.class);
79          moduleConfig.addProperty("format", pattern);
80          final String[] expectedViolations = {
81              "1:9: " + getCheckMessage(PackageNameCheck.class, MSG_KEY,
82                      "org.checkstyle.suppressionxpathfilter.PACKAGENAME",
83                      pattern),
84          };
85  
86          final List<String> expectedXpathQueries = Collections.singletonList(
87                  "/COMPILATION_UNIT/PACKAGE_DEF/DOT[./IDENT"
88                          + "[@text='PACKAGENAME']]/DOT[./IDENT"
89                          + "[@text='suppressionxpathfilter']]"
90                          + "/DOT/IDENT[@text='org']"
91          );
92  
93          runVerifications(moduleConfig, fileToProcess, expectedViolations,
94                  expectedXpathQueries);
95      }
96  
97      @Test
98      public void testTwo() throws Exception {
99  
100         final File fileToProcess =
101                 new File(getPath("InputXpathPackageNameTwo.java"));
102 
103         final String pattern = "[A-Z]+";
104 
105         final DefaultConfiguration moduleConfig = createModuleConfig(PackageNameCheck.class);
106         moduleConfig.addProperty("format", pattern);
107 
108         final String[] expectedViolation = {
109             "2:9: " + getCheckMessage(PackageNameCheck.class, MSG_KEY,
110                     "org.checkstyle.suppressionxpathfilter.packagename",
111                     pattern),
112         };
113 
114         final List<String> expectedXpathQueries = Collections.singletonList(
115                 "/COMPILATION_UNIT/PACKAGE_DEF/DOT"
116                         + "[./IDENT[@text='packagename']]/DOT"
117                         + "[./IDENT[@text='suppressionxpathfilter']]"
118                         + "/DOT/IDENT[@text='org']"
119         );
120 
121         runVerifications(moduleConfig, fileToProcess, expectedViolation,
122                 expectedXpathQueries);
123     }
124 }