View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.IllegalTokenCheck;
32  
33  public class XpathRegressionIllegalTokenTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = IllegalTokenCheck.class.getSimpleName();
36  
37      @Override
38      protected String getCheckName() {
39          return checkName;
40      }
41  
42      @Override
43      public String getPackageLocation() {
44          return "org/checkstyle/suppressionxpathfilter/coding/illegaltoken";
45      }
46  
47      @Test
48      public void testLabel() throws Exception {
49          final File fileToProcess =
50                  new File(getPath("InputXpathIllegalTokenLabel.java"));
51          final DefaultConfiguration moduleConfig =
52                  createModuleConfig(IllegalTokenCheck.class);
53          final String[] expectedViolation = {
54              "5:10: " + getCheckMessage(IllegalTokenCheck.class,
55                          IllegalTokenCheck.MSG_KEY, "outer:"),
56          };
57          final List<String> expectedXpathQueries = Collections.singletonList(
58                  "/COMPILATION_UNIT"
59                      + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenLabel']]"
60                      + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myTest']]"
61                      + "/SLIST/LABELED_STAT[./IDENT[@text='outer']]"
62          );
63  
64          runVerifications(moduleConfig, fileToProcess, expectedViolation,
65                  expectedXpathQueries);
66      }
67  
68      @Test
69      public void testNative() throws Exception {
70          final File fileToProcess =
71                  new File(getPath("InputXpathIllegalTokenNative.java"));
72          final DefaultConfiguration moduleConfig =
73                  createModuleConfig(IllegalTokenCheck.class);
74  
75          moduleConfig.addProperty("tokens", "LITERAL_NATIVE");
76  
77          final String[] expectedViolation = {
78              "4:10: " + getCheckMessage(IllegalTokenCheck.class,
79                          IllegalTokenCheck.MSG_KEY, "native"),
80          };
81          final List<String> expectedXpathQueries = Collections.singletonList(
82                  "/COMPILATION_UNIT"
83                          + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenNative']]"
84                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myTest']]"
85                          + "/MODIFIERS/LITERAL_NATIVE"
86          );
87  
88          runVerifications(moduleConfig, fileToProcess, expectedViolation,
89                  expectedXpathQueries);
90      }
91  
92      @Test
93      public void testPostIncrement() throws Exception {
94          final File fileToProcess =
95                  new File(getPath("InputXpathIllegalTokenPostIncrement.java"));
96          final DefaultConfiguration moduleConfig =
97                  createModuleConfig(IllegalTokenCheck.class);
98  
99          moduleConfig.addProperty("tokens", "POST_INC");
100 
101         final String[] expectedViolation = {
102             "8:10: " + getCheckMessage(IllegalTokenCheck.class,
103                         IllegalTokenCheck.MSG_KEY, "++"),
104         };
105         final List<String> expectedXpathQueries = Arrays.asList(
106                 "/COMPILATION_UNIT"
107                         + "/ENUM_DEF[./IDENT[@text='InputXpathIllegalTokenPostIncrement']]"
108                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
109                         + "/SLIST/EXPR",
110                 "/COMPILATION_UNIT"
111                         + "/ENUM_DEF[./IDENT[@text='InputXpathIllegalTokenPostIncrement']]"
112                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='method']]"
113                         + "/SLIST/EXPR/POST_INC[./IDENT[@text='i']]"
114         );
115 
116         runVerifications(moduleConfig, fileToProcess, expectedViolation,
117                 expectedXpathQueries);
118     }
119 }