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.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.ArrayTrailingCommaCheck;
32  
33  public class XpathRegressionArrayTrailingCommaTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = ArrayTrailingCommaCheck.class.getSimpleName();
36  
37      @Override
38      protected String getCheckName() {
39          return checkName;
40      }
41  
42      @Override
43      protected String getPackageLocation() {
44          return "org/checkstyle/suppressionxpathfilter/coding/arraytrailingcomma";
45      }
46  
47      @Test
48      public void testOne() throws Exception {
49          final File fileToProcess =
50                  new File(getPath("InputXpathArrayTrailingCommaLinear.java"));
51  
52          final DefaultConfiguration moduleConfig =
53                  createModuleConfig(ArrayTrailingCommaCheck.class);
54  
55          final String[] expectedViolation = {
56              "16:9: " + getCheckMessage(ArrayTrailingCommaCheck.class,
57                  ArrayTrailingCommaCheck.MSG_KEY),
58          };
59  
60          final List<String> expectedXpathQueries = Arrays.asList(
61              "/COMPILATION_UNIT/CLASS_DEF"
62                  + "[./IDENT[@text='InputXpathArrayTrailingCommaLinear']]"
63                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='a2']]/ASSIGN/EXPR/LITERAL_NEW"
64                  + "/ARRAY_INIT/EXPR[./NUM_INT[@text='3']]",
65              "/COMPILATION_UNIT/CLASS_DEF"
66                  + "[./IDENT[@text='InputXpathArrayTrailingCommaLinear']]"
67                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='a2']]/ASSIGN/EXPR/LITERAL_NEW"
68                  + "/ARRAY_INIT/EXPR/NUM_INT[@text='3']"
69          );
70  
71          runVerifications(moduleConfig, fileToProcess, expectedViolation,
72                  expectedXpathQueries);
73      }
74  
75      @Test
76      public void testTwo() throws Exception {
77          final File fileToProcess =
78                  new File(getPath("InputXpathArrayTrailingCommaMatrix.java"));
79  
80          final DefaultConfiguration moduleConfig =
81                  createModuleConfig(ArrayTrailingCommaCheck.class);
82  
83          final String[] expectedViolation = {
84              "17:9: " + getCheckMessage(ArrayTrailingCommaCheck.class,
85                  ArrayTrailingCommaCheck.MSG_KEY),
86          };
87  
88          final List<String> expectedXpathQueries = Collections.singletonList(
89              "/COMPILATION_UNIT/CLASS_DEF"
90                  + "[./IDENT[@text='InputXpathArrayTrailingCommaMatrix']]"
91                  + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='d2']]/ASSIGN/EXPR/LITERAL_NEW"
92                  + "/ARRAY_INIT/ARRAY_INIT[./EXPR/NUM_INT[@text='5']]"
93          );
94  
95          runVerifications(moduleConfig, fileToProcess, expectedViolation,
96                  expectedXpathQueries);
97      }
98  
99  }