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;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.AvoidEscapedUnicodeCharactersCheck.MSG_KEY;
23  
24  import java.io.File;
25  import java.util.Arrays;
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.AvoidEscapedUnicodeCharactersCheck;
32  
33  public class XpathRegressionAvoidEscapedUnicodeCharactersTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = AvoidEscapedUnicodeCharactersCheck.class.getSimpleName();
36  
37      @Override
38      protected String getCheckName() {
39          return checkName;
40      }
41  
42      @Test
43      public void testDefault() throws Exception {
44          final File fileToProcess = new File(getPath(
45              "InputXpathAvoidEscapedUnicodeCharactersDefault.java"));
46  
47          final DefaultConfiguration moduleConfig =
48                  createModuleConfig(AvoidEscapedUnicodeCharactersCheck.class);
49  
50          final String[] expectedViolation = {
51              "4:34: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY),
52          };
53  
54          final List<String> expectedXpathQueries = Arrays.asList(
55              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
56                  + "[@text='InputXpathAvoidEscapedUnicodeCharactersDefault']]"
57                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev2']]"
58                  + "/ASSIGN/EXPR[./STRING_LITERAL[@text='\\u03bcs']]",
59              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
60                  + "[@text='InputXpathAvoidEscapedUnicodeCharactersDefault']]"
61                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev2']]"
62                  + "/ASSIGN/EXPR/STRING_LITERAL[@text='\\u03bcs']"
63          );
64  
65          runVerifications(moduleConfig, fileToProcess, expectedViolation,
66                  expectedXpathQueries);
67      }
68  
69      @Test
70      public void testControlCharacters() throws Exception {
71          final File fileToProcess = new File(getPath(
72              "InputXpathAvoidEscapedUnicodeCharactersControlCharacters.java")
73          );
74  
75          final DefaultConfiguration moduleConfig =
76                  createModuleConfig(AvoidEscapedUnicodeCharactersCheck.class);
77          moduleConfig.addProperty("allowEscapesForControlCharacters", "true");
78  
79          final String[] expectedViolation = {
80              "4:34: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY),
81          };
82  
83          final List<String> expectedXpathQueries = Arrays.asList(
84              "/COMPILATION_UNIT/CLASS_DEF[."
85                  + "/IDENT[@text="
86                      + "'InputXpathAvoidEscapedUnicodeCharactersControlCharacters']]"
87                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
88                  + "/ASSIGN/EXPR[./STRING_LITERAL[@text='\\u03bcs']]",
89              "/COMPILATION_UNIT/CLASS_DEF[."
90                  + "/IDENT[@text="
91                      + "'InputXpathAvoidEscapedUnicodeCharactersControlCharacters']]"
92                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
93                  + "/ASSIGN/EXPR/STRING_LITERAL[@text='\\u03bcs']"
94          );
95  
96          runVerifications(moduleConfig, fileToProcess, expectedViolation,
97                  expectedXpathQueries);
98      }
99  
100     @Test
101     public void testTailComment() throws Exception {
102         final File fileToProcess = new File(getPath(
103             "InputXpathAvoidEscapedUnicodeCharactersTailComment.java"));
104 
105         final DefaultConfiguration moduleConfig =
106                 createModuleConfig(AvoidEscapedUnicodeCharactersCheck.class);
107         moduleConfig.addProperty("allowByTailComment", "true");
108 
109         final String[] expectedViolation = {
110             "4:45: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY),
111         };
112 
113         final List<String> expectedXpathQueries = Arrays.asList(
114             "/COMPILATION_UNIT/CLASS_DEF[."
115                 + "/IDENT[@text="
116                     + "'InputXpathAvoidEscapedUnicodeCharactersTailComment']]"
117                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
118                 + "/ASSIGN/EXPR[./STRING_LITERAL[@text='\\u03bcs']]",
119             "/COMPILATION_UNIT/CLASS_DEF[."
120                 + "/IDENT[@text="
121                     + "'InputXpathAvoidEscapedUnicodeCharactersTailComment']]"
122                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
123                 + "/ASSIGN/EXPR/STRING_LITERAL[@text='\\u03bcs']"
124         );
125 
126         runVerifications(moduleConfig, fileToProcess, expectedViolation,
127                 expectedXpathQueries);
128     }
129 
130     @Test
131     public void testAllCharactersEscaped() throws Exception {
132         final File fileToProcess = new File(getPath(
133             "InputXpathAvoidEscapedUnicodeCharactersAllEscaped.java"));
134 
135         final DefaultConfiguration moduleConfig =
136                 createModuleConfig(AvoidEscapedUnicodeCharactersCheck.class);
137         moduleConfig.addProperty("allowIfAllCharactersEscaped", "true");
138 
139         final String[] expectedViolation = {
140             "4:34: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY),
141         };
142 
143         final List<String> expectedXpathQueries = Arrays.asList(
144                 "/COMPILATION_UNIT/CLASS_DEF[."
145                     + "/IDENT[@text="
146                         + "'InputXpathAvoidEscapedUnicodeCharactersAllEscaped']]"
147                     + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
148                     + "/ASSIGN/EXPR[./STRING_LITERAL[@text='\\u03bcs']]",
149                 "/COMPILATION_UNIT/CLASS_DEF[."
150                     + "/IDENT[@text="
151                         + "'InputXpathAvoidEscapedUnicodeCharactersAllEscaped']]"
152                     + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
153                     + "/ASSIGN/EXPR/STRING_LITERAL[@text='\\u03bcs']"
154         );
155 
156         runVerifications(moduleConfig, fileToProcess, expectedViolation,
157                 expectedXpathQueries);
158     }
159 
160     @Test
161     public void testNonPrintableCharacters() throws Exception {
162         final File fileToProcess = new File(getPath(
163             "InputXpathAvoidEscapedUnicodeCharactersNonPrintable.java"));
164 
165         final DefaultConfiguration moduleConfig =
166                 createModuleConfig(AvoidEscapedUnicodeCharactersCheck.class);
167         moduleConfig.addProperty("allowNonPrintableEscapes", "true");
168 
169         final String[] expectedViolation = {
170             "4:34: " + getCheckMessage(AvoidEscapedUnicodeCharactersCheck.class, MSG_KEY),
171         };
172 
173         final List<String> expectedXpathQueries = Arrays.asList(
174             "/COMPILATION_UNIT/CLASS_DEF[."
175                 + "/IDENT[@text="
176                     + "'InputXpathAvoidEscapedUnicodeCharactersNonPrintable']]"
177                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
178                 + "/ASSIGN/EXPR[./STRING_LITERAL[@text='\\u03bcs']]",
179             "/COMPILATION_UNIT/CLASS_DEF[."
180                 + "/IDENT[@text="
181                     + "'InputXpathAvoidEscapedUnicodeCharactersNonPrintable']]"
182                 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='unitAbbrev9']]"
183                 + "/ASSIGN/EXPR/STRING_LITERAL[@text='\\u03bcs']"
184         );
185 
186         runVerifications(moduleConfig, fileToProcess, expectedViolation,
187                 expectedXpathQueries);
188     }
189 
190 }