View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.Collections;
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.UnusedTryResourceShouldBeUnnamedCheck;
31  
32  public class XpathRegressionUnusedTryResourceShouldBeUnnamedTest
33          extends AbstractXpathTestSupport {
34  
35      private final String checkName = UnusedTryResourceShouldBeUnnamedCheck.class
36                                                  .getSimpleName();
37  
38      @Override
39      protected String getCheckName() {
40          return checkName;
41  
42      }
43  
44      @Override
45      public String getPackageLocation() {
46          return "org/checkstyle/suppressionxpathfilter/coding/unusedtryresourceshouldbeunnamed";
47      }
48  
49      @Test
50      public void testSimple() throws Exception {
51          final File fileToProcess =
52                  new File(getPath(
53                          "InputXpathUnusedTryResourceShouldBeUnnamedSimple.java"));
54  
55          final DefaultConfiguration moduleConfig =
56                  createModuleConfig(UnusedTryResourceShouldBeUnnamedCheck.class);
57  
58          final String[] expectedViolation = {
59              "5:26: " + getCheckMessage(UnusedTryResourceShouldBeUnnamedCheck.class,
60                      UnusedTryResourceShouldBeUnnamedCheck.MSG_UNUSED_TRY_RESOURCE,
61                      "a"),
62          };
63  
64          final List<String> expectedXpathQueries = Collections.singletonList(
65                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
66                          + "[@text='InputXpathUnusedTryResourceShouldBeUnnamedSimple']]"
67                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_TRY"
68                          + "/RESOURCE_SPECIFICATION/RESOURCES/RESOURCE/IDENT[@text='a']"
69          );
70  
71          runVerifications(moduleConfig, fileToProcess, expectedViolation,
72                  expectedXpathQueries);
73      }
74  
75      @Test
76      public void testNested() throws Exception {
77          final File fileToProcess =
78                  new File(getPath(
79                          "InputXpathUnusedTryResourceShouldBeUnnamedNested.java"));
80  
81          final DefaultConfiguration moduleConfig =
82                  createModuleConfig(UnusedTryResourceShouldBeUnnamedCheck.class);
83  
84          final String[] expectedViolation = {
85              "7:28: " + getCheckMessage(UnusedTryResourceShouldBeUnnamedCheck.class,
86                      UnusedTryResourceShouldBeUnnamedCheck.MSG_UNUSED_TRY_RESOURCE,
87                      "b"),
88          };
89  
90          final List<String> expectedXpathQueries = Collections.singletonList(
91                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
92                  + "[@text='InputXpathUnusedTryResourceShouldBeUnnamedNested']]"
93                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_TRY"
94                  + "/SLIST/LITERAL_TRY/RESOURCE_SPECIFICATION/RESOURCES/RESOURCE/IDENT[@text='b']"
95          );
96  
97          runVerifications(moduleConfig, fileToProcess, expectedViolation,
98                  expectedXpathQueries);
99      }
100 
101     @Test
102     public void testAnon() throws Exception {
103         final File fileToProcess =
104                 new File(getPath(
105                         "InputXpathUnusedTryResourceShouldBeUnnamedAnon.java"));
106 
107         final DefaultConfiguration moduleConfig =
108                 createModuleConfig(UnusedTryResourceShouldBeUnnamedCheck.class);
109 
110         final String[] expectedViolation = {
111             "8:30: " + getCheckMessage(UnusedTryResourceShouldBeUnnamedCheck.class,
112                     UnusedTryResourceShouldBeUnnamedCheck.MSG_UNUSED_TRY_RESOURCE,
113                     "a"),
114         };
115 
116         final List<String> expectedXpathQueries = Collections.singletonList(
117                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
118                 + "[@text='InputXpathUnusedTryResourceShouldBeUnnamedAnon']]"
119                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF"
120                 + "[./IDENT[@text='runnable']]/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='Runnable']]"
121                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='run']]/SLIST/LITERAL_TRY"
122                 + "/RESOURCE_SPECIFICATION/RESOURCES/RESOURCE/IDENT[@text='a']"
123         );
124 
125         runVerifications(moduleConfig, fileToProcess, expectedViolation,
126                 expectedXpathQueries);
127     }
128 
129     @Test
130     public void testVar() throws Exception {
131         final File fileToProcess =
132                 new File(getPath(
133                         "InputXpathUnusedTryResourceShouldBeUnnamedVar.java"));
134 
135         final DefaultConfiguration moduleConfig =
136                 createModuleConfig(UnusedTryResourceShouldBeUnnamedCheck.class);
137 
138         final String[] expectedViolation = {
139             "5:18: " + getCheckMessage(UnusedTryResourceShouldBeUnnamedCheck.class,
140                     UnusedTryResourceShouldBeUnnamedCheck.MSG_UNUSED_TRY_RESOURCE,
141                     "a"),
142         };
143 
144         final List<String> expectedXpathQueries = Collections.singletonList(
145                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
146                         + "[@text='InputXpathUnusedTryResourceShouldBeUnnamedVar']]"
147                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_TRY"
148                         + "/RESOURCE_SPECIFICATION/RESOURCES/RESOURCE/IDENT[@text='a']"
149         );
150 
151         runVerifications(moduleConfig, fileToProcess, expectedViolation,
152                 expectedXpathQueries);
153     }
154 }