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 java.io.File;
23  import java.util.Collections;
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.coding.UnnecessarySemicolonInTryWithResourcesCheck;
30  
31  public class XpathRegressionUnnecessarySemicolonInTryWithResourcesTest
32          extends AbstractXpathTestSupport {
33  
34      private final String checkName =
35              UnnecessarySemicolonInTryWithResourcesCheck.class.getSimpleName();
36  
37      @Override
38      protected String getCheckName() {
39          return checkName;
40      }
41  
42      @Test
43      public void testDefault() throws Exception {
44          final File fileToProcess = new File(
45                  getPath("InputXpathUnnecessarySemicolonInTryWithResourcesDefault.java"));
46          final DefaultConfiguration moduleConfig =
47                  createModuleConfig(UnnecessarySemicolonInTryWithResourcesCheck.class);
48          final String[] expectedViolation = {
49              "11:76: " + getCheckMessage(UnnecessarySemicolonInTryWithResourcesCheck.class,
50                  UnnecessarySemicolonInTryWithResourcesCheck.MSG_SEMI),
51          };
52          final List<String> expectedXpathQueries = Collections.singletonList(
53                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
54                          + "'InputXpathUnnecessarySemicolonInTryWithResourcesDefault']]"
55                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='m']]/SLIST/LITERAL_TRY"
56                          + "/RESOURCE_SPECIFICATION/SEMI"
57          );
58          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
59      }
60  
61      @Test
62      public void testNoBrace() throws Exception {
63          final File fileToProcess = new File(getPath(
64              "InputXpathUnnecessarySemicolonInTryWithResourcesNoBrace.java"
65          ));
66  
67          final DefaultConfiguration moduleConfig =
68              createModuleConfig(UnnecessarySemicolonInTryWithResourcesCheck.class);
69          moduleConfig.addProperty("allowWhenNoBraceAfterSemicolon", "false");
70  
71          final String[] expectedViolation = {
72              "8:44: " + getCheckMessage(UnnecessarySemicolonInTryWithResourcesCheck.class,
73                  UnnecessarySemicolonInTryWithResourcesCheck.MSG_SEMI),
74          };
75  
76          final List<String> expectedXpathQueries = Collections.singletonList(
77              "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
78                      + "'InputXpathUnnecessarySemicolonInTryWithResourcesNoBrace']]"
79                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
80                  + "/SLIST/LITERAL_TRY/RESOURCE_SPECIFICATION/SEMI"
81          );
82          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
83      }
84  }