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 com.puppycrawl.tools.checkstyle.checks.coding;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.UnusedTryResourceShouldBeUnnamedCheck.MSG_UNUSED_TRY_RESOURCE;
24  
25  import java.io.File;
26  import java.util.Collection;
27  import java.util.Optional;
28  
29  import org.junit.jupiter.api.Test;
30  
31  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32  import com.puppycrawl.tools.checkstyle.JavaParser;
33  import com.puppycrawl.tools.checkstyle.api.DetailAST;
34  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
36  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
37  
38  public class UnusedTryResourceShouldBeUnnamedCheckTest extends AbstractModuleTestSupport {
39  
40      @Override
41      public String getPackageLocation() {
42          return "com/puppycrawl/tools/checkstyle/checks/coding/unusedtryresourceshouldbeunnamed";
43      }
44  
45      @Test
46      public void testGetRequiredTokens() {
47          final UnusedTryResourceShouldBeUnnamedCheck checkObj =
48                              new UnusedTryResourceShouldBeUnnamedCheck();
49          final int[] expected = {
50              TokenTypes.LITERAL_TRY,
51              TokenTypes.IDENT,
52          };
53          assertWithMessage("Default required tokens are invalid")
54              .that(checkObj.getRequiredTokens())
55              .isEqualTo(expected);
56          assertWithMessage("Default acceptable tokens are invalid")
57              .that(checkObj.getAcceptableTokens())
58              .isEqualTo(expected);
59      }
60  
61      @Test
62      public void testUnusedTryResourceShouldBeUnnamed() throws Exception {
63          final String[] expected = {
64              "11:26: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "a"),
65              "25:26: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "e"),
66              "38:26: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "b"),
67              "56:28: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "autoCloseable2"),
68          };
69          verifyWithInlineConfigParser(
70              getPath("InputUnusedTryResourceShouldBeUnnamed.java"),
71              expected);
72      }
73  
74      @Test
75      public void testUnusedTryResourceShouldBeUnnamed2() throws Exception {
76          final String[] expected = {
77              "13:23: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "a"),
78              "30:23: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "a"),
79              "31:23: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "b"),
80              "32:23: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "c"),
81              "86:26: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "a"),
82          };
83          verifyWithInlineConfigParser(
84              getPath("InputUnusedTryResourceShouldBeUnnamed2.java"),
85              expected);
86      }
87  
88      @Test
89      public void testUnusedTryResourceShouldBeUnnamedInsideAnonClass() throws Exception {
90          final String[] expected = {
91              "15:30: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "a"),
92              "25:26: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "a"),
93              "30:32: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "b"),
94          };
95          verifyWithInlineConfigParser(
96              getPath("InputUnusedTryResourceShouldBeUnnamedInsideAnonClass.java"),
97              expected);
98      }
99  
100     @Test
101     public void testUnusedTryResourceShouldBeUnnamedNested() throws Exception {
102         final String[] expected = {
103             "14:28: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "b"),
104             "28:32: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "a"),
105             "47:28: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "b"),
106             "51:32: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "d"),
107         };
108         verifyWithInlineConfigParser(
109             getPath("InputUnusedTryResourceShouldBeUnnamedNested.java"),
110             expected);
111     }
112 
113     @Test
114     public void testUnusedTryResourceShouldBeUnnamedNested2() throws Exception {
115         final String[] expected = {
116             "17:36: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "b"),
117             "21:40: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "d"),
118             "47:34: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "b"),
119             "54:44: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "d"),
120             "79:30: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "b"),
121             "83:42: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "e"),
122         };
123         verifyWithInlineConfigParser(
124             getPath("InputUnusedTryResourceShouldBeUnnamedNested2.java"),
125             expected);
126     }
127 
128     @Test
129     public void testUnusedTryResourceShouldBeUnnamedStatic() throws Exception {
130         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
131         verifyWithInlineConfigParser(
132             getPath("InputUnusedTryResourceShouldBeUnnamedStatic.java"),
133             expected);
134     }
135 
136     @Test
137     public void testUnusedTryResourceShouldBeUnnamedForVariable() throws Exception {
138         final String[] expected = {
139             "48:66: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "i2"),
140             "58:60: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "m3"),
141         };
142         verifyWithInlineConfigParser(
143             getPath("InputUnusedTryResourceShouldBeUnnamedForVariable.java"),
144             expected);
145     }
146 
147     @Test
148     public void testUnusedTryResourceShouldBeUnnamedEdgeCases() throws Exception {
149         final String[] expected = {
150             "41:28: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "e"),
151             "49:28: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "lock"),
152             "59:28: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "r"),
153             "99:28: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "close"),
154         };
155         verifyWithInlineConfigParser(
156             getPath("InputUnusedTryResourceShouldBeUnnamedEdgeCases.java"),
157             expected);
158     }
159 
160     @Test
161     public void testUnusedTryResourceShouldBeUnnamedEdgeCases2() throws Exception {
162         final String[] expected = {
163             "15:32: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "s"),
164             "26:34: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "f"),
165             "39:28: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "d"),
166             "54:32: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "inner"),
167             "61:28: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "x"),
168             "71:28: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "res"),
169             "80:28: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "unused1"),
170             "82:28: " + getCheckMessage(MSG_UNUSED_TRY_RESOURCE, "unused2"),
171         };
172         verifyWithInlineConfigParser(
173             getPath("InputUnusedTryResourceShouldBeUnnamedEdgeCases2.java"),
174             expected);
175     }
176 
177     @Test
178     public void testClearState() throws Exception {
179         final UnusedTryResourceShouldBeUnnamedCheck check =
180                 new UnusedTryResourceShouldBeUnnamedCheck();
181 
182         final DetailAST root = JavaParser.parseFile(
183                 new File(getPath(
184                         "InputUnusedTryResourceShouldBeUnnamed.java")),
185                 JavaParser.Options.WITHOUT_COMMENTS);
186 
187         final Optional<DetailAST> literalTry = TestUtil.findTokenInAstByPredicate(root,
188             ast -> ast.getType() == TokenTypes.LITERAL_TRY);
189 
190         assertWithMessage("State is not cleared on beginTree")
191                 .that(TestUtil.isStatefulFieldClearedDuringBeginTree(check,
192                         literalTry.orElseThrow(),
193                         "tryResources",
194                         tryResources -> {
195                             return ((Collection<?>) tryResources).isEmpty();
196                         }))
197                 .isTrue();
198     }
199 
200 }