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.blocks;
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.blocks.EmptyCatchBlockCheck;
31  
32  public class XpathRegressionEmptyCatchBlockTest extends AbstractXpathTestSupport {
33  
34      private static final Class<EmptyCatchBlockCheck> CLAZZ =
35          EmptyCatchBlockCheck.class;
36  
37      @Override
38      public String getPackageLocation() {
39          return "org/checkstyle/suppressionxpathfilter/blocks/emptycatchblock";
40      }
41  
42      @Override
43      protected String getCheckName() {
44          return CLAZZ.getSimpleName();
45      }
46  
47      @Test
48      public void testOne() throws Exception {
49          final File fileToProcess = new File(
50              getPath("InputXpathEmptyCatchBlockOne.java"));
51  
52          final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
53  
54          final String[] expectedViolation = {
55              "8:38: " + getCheckMessage(CLAZZ, EmptyCatchBlockCheck.MSG_KEY_CATCH_BLOCK_EMPTY),
56          };
57  
58          final List<String> expectedXpathQueries = Collections.singletonList(
59              "/COMPILATION_UNIT/CLASS_DEF"
60                  + "[./IDENT[@text='InputXpathEmptyCatchBlockOne']]"
61                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='main']]"
62                  + "/SLIST/LITERAL_TRY/LITERAL_CATCH/SLIST"
63          );
64  
65          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
66      }
67  
68      @Test
69      public void testTwo() throws Exception {
70          final File fileToProcess = new File(
71              getPath("InputXpathEmptyCatchBlockTwo.java"));
72  
73          final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
74  
75          final String[] expectedViolation = {
76              "8:47: " + getCheckMessage(CLAZZ, EmptyCatchBlockCheck.MSG_KEY_CATCH_BLOCK_EMPTY),
77          };
78  
79          final List<String> expectedXpathQueries = Collections.singletonList(
80              "/COMPILATION_UNIT/CLASS_DEF"
81                  + "[./IDENT[@text='InputXpathEmptyCatchBlockTwo']]"
82                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='main']]"
83                  + "/SLIST/LITERAL_TRY/LITERAL_CATCH/SLIST"
84          );
85  
86          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
87      }
88  
89      @Test
90      public void testThree() throws Exception {
91          final File fileToProcess = new File(
92              getPath("InputXpathEmptyCatchBlockThree.java"));
93  
94          final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
95  
96          final String[] expectedViolation = {
97              "10:47: " + getCheckMessage(CLAZZ, EmptyCatchBlockCheck.MSG_KEY_CATCH_BLOCK_EMPTY),
98          };
99  
100         final List<String> expectedXpathQueries = Collections.singletonList(
101             "/COMPILATION_UNIT/CLASS_DEF"
102                 + "[./IDENT[@text='InputXpathEmptyCatchBlockThree']]"
103                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='process']]"
104                 + "/SLIST/LITERAL_TRY/SLIST/LITERAL_TRY/LITERAL_CATCH/SLIST"
105         );
106 
107         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
108     }
109 
110 }