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.coding;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.coding.MultipleVariableDeclarationsCheck;
31  
32  public class XpathRegressionMultipleVariableDeclarationsTest extends AbstractXpathTestSupport {
33  
34      private final String checkName = MultipleVariableDeclarationsCheck.class.getSimpleName();
35  
36      @Override
37      protected String getCheckName() {
38          return checkName;
39      }
40  
41      @Override
42      protected String getPackageLocation() {
43          return "org/checkstyle/suppressionxpathfilter/coding/multiplevariabledeclarations";
44      }
45  
46      @Test
47      public void testCommaSeparator() throws Exception {
48          final File fileToProcess = new File(
49                  getPath("InputXpathMultipleVariableDeclarationsCommaSeparator.java"));
50  
51          final DefaultConfiguration moduleConfig =
52                  createModuleConfig(MultipleVariableDeclarationsCheck.class);
53  
54          final String[] expectedViolation = {
55              "4:5: " + getCheckMessage(MultipleVariableDeclarationsCheck.class,
56                  MultipleVariableDeclarationsCheck.MSG_MULTIPLE_COMMA),
57          };
58  
59          final List<String> expectedXpathQueries = Arrays.asList(
60              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
61                  + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
62                  + "/VARIABLE_DEF[./IDENT[@text='i']]",
63              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
64                  + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
65                  + "/VARIABLE_DEF[./IDENT[@text='i']]/MODIFIERS",
66              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
67                  + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
68                  + "/VARIABLE_DEF[./IDENT[@text='i']]/TYPE",
69              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
70                  + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
71                  + "/VARIABLE_DEF[./IDENT[@text='i']]/TYPE/LITERAL_INT",
72              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
73                  + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
74                  + "/VARIABLE_DEF[./IDENT[@text='j']]",
75              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
76                  + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
77                  + "/VARIABLE_DEF[./IDENT[@text='j']]/MODIFIERS",
78              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
79                  + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
80                  + "/VARIABLE_DEF[./IDENT[@text='j']]/TYPE",
81              "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
82                  + "@text='InputXpathMultipleVariableDeclarationsCommaSeparator']]/OBJBLOCK"
83                  + "/VARIABLE_DEF[./IDENT[@text='j']]/TYPE/LITERAL_INT"
84          );
85  
86          runVerifications(moduleConfig, fileToProcess, expectedViolation,
87                  expectedXpathQueries);
88      }
89  
90      @Test
91      public void testMultipleVariableDeclarations() throws Exception {
92          final File fileToProcess = new File(
93                  getPath("InputXpathMultipleVariableDeclarations.java"));
94  
95          final DefaultConfiguration moduleConfig =
96                  createModuleConfig(MultipleVariableDeclarationsCheck.class);
97  
98          final String[] expectedViolation = {
99              "4:5: " + getCheckMessage(MultipleVariableDeclarationsCheck.class,
100                 MultipleVariableDeclarationsCheck.MSG_MULTIPLE),
101         };
102 
103         final List<String> expectedXpathQueries = Arrays.asList(
104             "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
105                 + "@text='InputXpathMultipleVariableDeclarations']]/OBJBLOCK"
106                 + "/VARIABLE_DEF[./IDENT[@text='i1']]",
107             "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
108                 + "@text='InputXpathMultipleVariableDeclarations']]/OBJBLOCK"
109                 + "/VARIABLE_DEF[./IDENT[@text='i1']]/MODIFIERS",
110             "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
111                 + "@text='InputXpathMultipleVariableDeclarations']]/OBJBLOCK"
112                 + "/VARIABLE_DEF[./IDENT[@text='i1']]/TYPE",
113             "/COMPILATION_UNIT/CLASS_DEF[./IDENT["
114                 + "@text='InputXpathMultipleVariableDeclarations']]/OBJBLOCK"
115                 + "/VARIABLE_DEF[./IDENT[@text='i1']]/TYPE/LITERAL_INT"
116         );
117 
118         runVerifications(moduleConfig, fileToProcess, expectedViolation,
119                 expectedXpathQueries);
120     }
121 }