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