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;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.checks.HexLiteralCaseCheck;
30  
31  public class XpathRegressionHexLiteralCaseTest extends AbstractXpathTestSupport {
32  
33      private final String checkName = HexLiteralCaseCheck.class.getSimpleName();
34  
35      @Override
36      protected String getCheckName() {
37          return checkName;
38      }
39  
40      @Test
41      public void testHexLiteralCaseCheck() throws Exception {
42          final File fileProcess =
43                  new File(getPath("InputXpathHexLiteralCase.java"));
44          final DefaultConfiguration config =
45                  createModuleConfig(HexLiteralCaseCheck.class);
46  
47          final String[] expected = {
48              "4:14: " + getCheckMessage(HexLiteralCaseCheck.class,
49                        HexLiteralCaseCheck.MSG_KEY),
50          };
51          final List<String> expectedXpathQueries = Arrays.asList(
52                  "/COMPILATION_UNIT/CLASS_DEF"
53                          + "[./IDENT[@text='InputXpathHexLiteralCase']]"
54                          + "/OBJBLOCK/VARIABLE_DEF"
55                          + "[./IDENT[@text='i']]/ASSIGN/EXPR[./NUM_INT[@text='0xa']]",
56  
57                  "/COMPILATION_UNIT/CLASS_DEF"
58                          + "[./IDENT[@text='InputXpathHexLiteralCase']]"
59                          + "/OBJBLOCK/VARIABLE_DEF"
60                          + "[./IDENT[@text='i']]/ASSIGN/EXPR/NUM_INT[@text='0xa']"
61          );
62          runVerifications(config, fileProcess, expected, expectedXpathQueries);
63      }
64  
65      @Test
66      public void testLongHexLiteralCaseCheck() throws Exception {
67          final File fileProcess =
68                  new File(getPath("InputXpathHexLiteralCaseLong.java"));
69          final DefaultConfiguration config =
70                  createModuleConfig(HexLiteralCaseCheck.class);
71  
72          final String[] expected = {
73              "4:15: " + getCheckMessage(HexLiteralCaseCheck.class,
74                      HexLiteralCaseCheck.MSG_KEY),
75          };
76          final List<String> expectedXpathQueries = Arrays.asList(
77                  "/COMPILATION_UNIT/CLASS_DEF"
78                          + "[./IDENT[@text='InputXpathHexLiteralCaseLong']]"
79                          + "/OBJBLOCK/VARIABLE_DEF"
80                          + "[./IDENT[@text='i']]/ASSIGN/EXPR[./NUM_LONG[@text='0x00efL']]",
81  
82                  "/COMPILATION_UNIT/CLASS_DEF"
83                          + "[./IDENT[@text='InputXpathHexLiteralCaseLong']]"
84                          + "/OBJBLOCK/VARIABLE_DEF"
85                          + "[./IDENT[@text='i']]/ASSIGN/EXPR/NUM_LONG[@text='0x00efL']"
86          );
87          runVerifications(config, fileProcess, expected, expectedXpathQueries);
88      }
89  
90      @Test
91      public void testHexLiteralCaseCheckTwo() throws Exception {
92          final File fileProcess =
93                  new File(getPath("InputXpathHexLiteralCaseTwo.java"));
94          final DefaultConfiguration config =
95                  createModuleConfig(HexLiteralCaseCheck.class);
96  
97          final String[] expected = {
98              "4:14: " + getCheckMessage(HexLiteralCaseCheck.class,
99                      HexLiteralCaseCheck.MSG_KEY),
100         };
101         final List<String> expectedXpathQueries = Arrays.asList(
102                 "/COMPILATION_UNIT/CLASS_DEF"
103                         + "[./IDENT[@text='InputXpathHexLiteralCaseTwo']]"
104                         + "/OBJBLOCK/VARIABLE_DEF"
105                         + "[./IDENT[@text='a']]/ASSIGN/EXPR[./NUM_INT[@text='0xFa1']]",
106 
107                 "/COMPILATION_UNIT/CLASS_DEF"
108                         + "[./IDENT[@text='InputXpathHexLiteralCaseTwo']]"
109                         + "/OBJBLOCK/VARIABLE_DEF"
110                         + "[./IDENT[@text='a']]/ASSIGN/EXPR/NUM_INT[@text='0xFa1']"
111         );
112         runVerifications(config, fileProcess, expected, expectedXpathQueries);
113     }
114 
115 }