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.coding;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
28  import org.junit.jupiter.api.Test;
29  
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.checks.coding.IllegalTokenTextCheck;
32  
33  public class XpathRegressionIllegalTokenTextTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = IllegalTokenTextCheck.class.getSimpleName();
36  
37      @Override
38      protected String getCheckName() {
39          return checkName;
40      }
41  
42      @Override
43      protected String getPackageLocation() {
44          return "org/checkstyle/suppressionxpathfilter/coding/illegaltokentext";
45      }
46  
47      @Test
48      public void testField() throws Exception {
49          final File fileToProcess =
50                  new File(getPath("InputXpathIllegalTokenTextField.java"));
51          final DefaultConfiguration moduleConfig =
52                  createModuleConfig(IllegalTokenTextCheck.class);
53          moduleConfig.addProperty("format", "12345");
54          moduleConfig.addProperty("tokens", "NUM_INT");
55          final String[] expectedViolation = {
56              "4:33: " + getCheckMessage(IllegalTokenTextCheck.class,
57                          IllegalTokenTextCheck.MSG_KEY, "12345"),
58          };
59          final List<String> expectedXpathQueries = Arrays.asList(
60                  "/COMPILATION_UNIT"
61                      + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenTextField']]"
62                      + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='illegalNumber']]"
63                      + "/ASSIGN/EXPR[./NUM_INT[@text='12345']]",
64                  "/COMPILATION_UNIT"
65                      + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenTextField']]"
66                      + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='illegalNumber']]"
67                      + "/ASSIGN/EXPR/NUM_INT[@text='12345']"
68          );
69  
70          runVerifications(moduleConfig, fileToProcess, expectedViolation,
71                  expectedXpathQueries);
72      }
73  
74      @Test
75      public void testMethod() throws Exception {
76          final File fileToProcess =
77                  new File(getPath("InputXpathIllegalTokenTextMethod.java"));
78          final DefaultConfiguration moduleConfig =
79                  createModuleConfig(IllegalTokenTextCheck.class);
80          moduleConfig.addProperty("format", "forbiddenText");
81          moduleConfig.addProperty("tokens", "STRING_LITERAL");
82          final String[] expectedViolation = {
83              "5:32: " + getCheckMessage(IllegalTokenTextCheck.class,
84                          IllegalTokenTextCheck.MSG_KEY, "forbiddenText"),
85          };
86          final List<String> expectedXpathQueries = Arrays.asList(
87                  "/COMPILATION_UNIT"
88                      + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenTextMethod']]"
89                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myMethod']]"
90                      + "/SLIST/VARIABLE_DEF[./IDENT[@text='illegalString']]"
91                      + "/ASSIGN/EXPR[./STRING_LITERAL[@text='forbiddenText']]",
92                  "/COMPILATION_UNIT"
93                      + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenTextMethod']]"
94                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myMethod']]"
95                      + "/SLIST/VARIABLE_DEF[./IDENT[@text='illegalString']]"
96                      + "/ASSIGN/EXPR/STRING_LITERAL[@text='forbiddenText']"
97          );
98  
99          runVerifications(moduleConfig, fileToProcess, expectedViolation,
100                 expectedXpathQueries);
101     }
102 
103     @Test
104     public void testInterface() throws Exception {
105         final File fileToProcess =
106                 new File(getPath("InputXpathIllegalTokenTextInterface.java"));
107         final DefaultConfiguration moduleConfig =
108                 createModuleConfig(IllegalTokenTextCheck.class);
109         moduleConfig.addProperty("format", "invalidIdentifier");
110         moduleConfig.addProperty("tokens", "IDENT");
111         final String[] expectedViolation = {
112             "4:10: " + getCheckMessage(IllegalTokenTextCheck.class,
113                         IllegalTokenTextCheck.MSG_KEY, "invalidIdentifier"),
114         };
115         final List<String> expectedXpathQueries = Collections.singletonList(
116                 "/COMPILATION_UNIT"
117                     + "/INTERFACE_DEF[./IDENT[@text='InputXpathIllegalTokenTextInterface']]"
118                     + "/OBJBLOCK/METHOD_DEF/IDENT[@text='invalidIdentifier']"
119         );
120 
121         runVerifications(moduleConfig, fileToProcess, expectedViolation,
122                 expectedXpathQueries);
123     }
124 
125 }