1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }