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 static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
23  
24  import java.io.File;
25  import java.util.Collections;
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.naming.ConstantNameCheck;
32  
33  public class XpathRegressionConstantNameTest extends AbstractXpathTestSupport {
34  
35      private static final Class<ConstantNameCheck> CLASS = ConstantNameCheck.class;
36      private static final String PATTERN = "^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$";
37      private final String checkName = CLASS.getSimpleName();
38  
39      @Override
40      protected String getCheckName() {
41          return checkName;
42      }
43  
44      @Test
45      public void testLowercase() throws Exception {
46          final File fileToProcess = new File(
47              getPath("InputXpathConstantNameLowercase.java"));
48  
49          final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
50  
51          final String[] expectedViolation = {
52              "4:29: " + getCheckMessage(CLASS, MSG_INVALID_PATTERN, "number", PATTERN),
53          };
54  
55          final List<String> expectedXpathQueries = Collections.singletonList(
56              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
57                  + "[@text='InputXpathConstantNameLowercase']]"
58                  + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='number']"
59          );
60  
61          runVerifications(moduleConfig, fileToProcess, expectedViolation,
62              expectedXpathQueries);
63      }
64  
65      @Test
66      public void testCamelCase() throws Exception {
67          final File fileToProcess =
68              new File(getPath("InputXpathConstantNameCamelCase.java"));
69  
70          final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
71  
72          final String[] expectedViolation = {
73              "4:29: " + getCheckMessage(CLASS,
74                  MSG_INVALID_PATTERN, "badConstant", PATTERN),
75          };
76  
77          final List<String> expectedXpathQueries = Collections.singletonList(
78              "/COMPILATION_UNIT/CLASS_DEF"
79                  + "[./IDENT[@text='InputXpathConstantNameCamelCase']]"
80                  + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='badConstant']"
81          );
82  
83          runVerifications(moduleConfig, fileToProcess, expectedViolation,
84              expectedXpathQueries);
85      }
86  
87      @Test
88      public void testWithBeginningUnderscore() throws Exception {
89          final File fileToProcess = new File(
90              getPath("InputXpathConstantNameWithBeginningUnderscore.java"));
91  
92          final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
93  
94          final String[] expectedViolation = {
95              "4:33: " + getCheckMessage(CLASS, MSG_INVALID_PATTERN, "_CONSTANT", PATTERN),
96          };
97  
98          final List<String> expectedXpathQueries = Collections.singletonList(
99              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
100                 + "[@text='InputXpathConstantNameWithBeginningUnderscore']]"
101                 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='_CONSTANT']"
102         );
103 
104         runVerifications(moduleConfig, fileToProcess, expectedViolation,
105             expectedXpathQueries);
106     }
107 
108     @Test
109     public void testWithTwoUnderscores() throws Exception {
110         final File fileToProcess = new File(
111             getPath("InputXpathConstantNameWithTwoUnderscores.java"));
112 
113         final DefaultConfiguration moduleConfig = createModuleConfig(CLASS);
114 
115         final String[] expectedViolation = {
116             "4:30: " + getCheckMessage(CLASS, MSG_INVALID_PATTERN, "BAD__NAME", PATTERN),
117         };
118 
119         final List<String> expectedXpathQueries = Collections.singletonList(
120             "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
121                 + "[@text='InputXpathConstantNameWithTwoUnderscores']]"
122                 + "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='BAD__NAME']"
123         );
124 
125         runVerifications(moduleConfig, fileToProcess, expectedViolation,
126             expectedXpathQueries);
127     }
128 }