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.DefaultComesLastCheck;
32  
33  public class XpathRegressionDefaultComesLastTest extends AbstractXpathTestSupport {
34  
35      private final String checkName = DefaultComesLastCheck.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/defaultcomeslast";
45      }
46  
47      @Test
48      public void testNonEmptyCase() throws Exception {
49          final File fileToProcess =
50                  new File(getPath("InputXpathDefaultComesLastNonEmptyCase.java"));
51  
52          final DefaultConfiguration moduleConfig =
53                  createModuleConfig(DefaultComesLastCheck.class);
54  
55          final String[] expectedViolation = {
56              "8:13: " + getCheckMessage(DefaultComesLastCheck.class,
57                      DefaultComesLastCheck.MSG_KEY),
58          };
59  
60          final List<String> expectedXpathQueries = Arrays.asList(
61              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
62                  + "[@text='InputXpathDefaultComesLastNonEmptyCase']]/OBJBLOCK"
63                  + "/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_SWITCH/CASE_GROUP["
64                  + "./SLIST/EXPR/ASSIGN/IDENT[@text='id']]",
65              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
66                  + "[@text='InputXpathDefaultComesLastNonEmptyCase']]/OBJBLOCK"
67                  + "/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_SWITCH/CASE_GROUP"
68                  + "/LITERAL_DEFAULT"
69          );
70  
71          runVerifications(moduleConfig, fileToProcess, expectedViolation,
72                  expectedXpathQueries);
73      }
74  
75      @Test
76      public void testEmptyCase() throws Exception {
77          final File fileToProcess =
78                  new File(getPath("InputXpathDefaultComesLastEmptyCase.java"));
79  
80          final DefaultConfiguration moduleConfig =
81                  createModuleConfig(DefaultComesLastCheck.class);
82          moduleConfig.addProperty("skipIfLastAndSharedWithCase", "true");
83  
84          final String[] expectedViolation = {
85              "15:13: " + getCheckMessage(DefaultComesLastCheck.class,
86                  DefaultComesLastCheck.MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE),
87          };
88  
89          final List<String> expectedXpathQueries = Collections.singletonList(
90              "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
91                  + "[@text='InputXpathDefaultComesLastEmptyCase']]/OBJBLOCK"
92                  + "/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_SWITCH/CASE_GROUP"
93                  + "/LITERAL_DEFAULT"
94          );
95  
96          runVerifications(moduleConfig, fileToProcess, expectedViolation,
97                  expectedXpathQueries);
98      }
99  
100 }