1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.design.ThrowsCountCheck;
30
31 public class XpathRegressionThrowsCountTest extends AbstractXpathTestSupport {
32
33 private final String checkName = ThrowsCountCheck.class.getSimpleName();
34
35 @Override
36 protected String getCheckName() {
37 return checkName;
38 }
39
40 @Test
41 public void testDefault() throws Exception {
42 final File fileToProcess =
43 new File(getPath("InputXpathThrowsCountDefault.java"));
44 final DefaultConfiguration moduleConfig =
45 createModuleConfig(ThrowsCountCheck.class);
46 final String[] expectedViolation = {
47 "4:30: " + getCheckMessage(ThrowsCountCheck.class,
48 ThrowsCountCheck.MSG_KEY, 5, 4),
49 };
50 final List<String> expectedXpathQueries = Collections.singletonList(
51 "/COMPILATION_UNIT"
52 + "/CLASS_DEF[./IDENT[@text='InputXpathThrowsCountDefault']]"
53 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunction']]"
54 + "/LITERAL_THROWS[./IDENT[@text='CloneNotSupportedException']]"
55
56 );
57
58 runVerifications(moduleConfig, fileToProcess, expectedViolation,
59 expectedXpathQueries);
60 }
61
62 @Test
63 public void testCustomMax() throws Exception {
64 final File fileToProcess =
65 new File(getPath("InputXpathThrowsCountCustomMax.java"));
66 final DefaultConfiguration moduleConfig =
67 createModuleConfig(ThrowsCountCheck.class);
68
69 moduleConfig.addProperty("max", "2");
70
71 final String[] expectedViolation = {
72 "4:30: " + getCheckMessage(ThrowsCountCheck.class,
73 ThrowsCountCheck.MSG_KEY, 3, 2),
74 };
75 final List<String> expectedXpathQueries = Collections.singletonList(
76 "/COMPILATION_UNIT"
77 + "/INTERFACE_DEF[./IDENT[@text='InputXpathThrowsCountCustomMax']]"
78 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunction']]"
79 + "/LITERAL_THROWS[./IDENT[@text='IllegalStateException']]"
80
81 );
82
83 runVerifications(moduleConfig, fileToProcess, expectedViolation,
84 expectedXpathQueries);
85 }
86
87 @Test
88 public void testPrivateMethods() throws Exception {
89 final File fileToProcess =
90 new File(getPath("InputXpathThrowsCountPrivateMethods.java"));
91 final DefaultConfiguration moduleConfig =
92 createModuleConfig(ThrowsCountCheck.class);
93
94 moduleConfig.addProperty("ignorePrivateMethods", "false");
95
96 final String[] expectedViolation = {
97 "9:40: " + getCheckMessage(ThrowsCountCheck.class,
98 ThrowsCountCheck.MSG_KEY, 5, 4),
99 };
100 final List<String> expectedXpathQueries = Collections.singletonList(
101 "/COMPILATION_UNIT"
102 + "/CLASS_DEF[./IDENT[@text='InputXpathThrowsCountPrivateMethods']]"
103 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myFunc']]"
104 + "/SLIST/VARIABLE_DEF[./IDENT[@text='foo']]"
105 + "/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='myClass']]"
106 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='privateFunc']]"
107 + "/LITERAL_THROWS[./IDENT[@text='CloneNotSupportedException']]"
108
109 );
110
111 runVerifications(moduleConfig, fileToProcess, expectedViolation,
112 expectedXpathQueries);
113 }
114 }