1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.IllegalThrowsCheck.MSG_KEY;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
29
30 public class IllegalThrowsCheckTest extends AbstractModuleTestSupport {
31
32 @Override
33 protected String getPackageLocation() {
34 return "com/puppycrawl/tools/checkstyle/checks/coding/illegalthrows";
35 }
36
37 @Test
38 public void testDefault() throws Exception {
39
40 final String[] expected = {
41 "19:51: " + getCheckMessage(MSG_KEY, "RuntimeException"),
42 "24:45: " + getCheckMessage(MSG_KEY, "java.lang.RuntimeException"),
43 "25:22: " + getCheckMessage(MSG_KEY, "java.lang.Error"),
44 };
45
46 verifyWithInlineConfigParser(
47 getPath("InputIllegalThrowsTestDefault.java"), expected);
48 }
49
50 @Test
51 public void testIllegalClassNames() throws Exception {
52
53 final String[] expected = {
54 "15:33: " + getCheckMessage(MSG_KEY, "NullPointerException"),
55 "24:73: " + getCheckMessage(MSG_KEY, "java.lang.Error"),
56 };
57
58 verifyWithInlineConfigParser(
59 getPath("InputIllegalThrowsTestIllegalClassNames.java"), expected);
60 }
61
62
63
64
65 @Test
66 public void testIgnoreMethodNames() throws Exception {
67
68 final String[] expected = {
69 "19:51: " + getCheckMessage(MSG_KEY, "RuntimeException"),
70 "28:35: " + getCheckMessage(MSG_KEY, "Throwable"),
71 };
72
73 verifyWithInlineConfigParser(
74 getPath("InputIllegalThrowsIgnoreMethodNames.java"), expected);
75 }
76
77
78
79
80 @Test
81 public void testIllegalClassNamesWithIgnoreMethodNames() throws Exception {
82 final String[] expected = {
83 "14:33: " + getCheckMessage(MSG_KEY, "NullPointerException"),
84 "27:35: " + getCheckMessage(MSG_KEY, "Throwable"),
85 };
86
87 verifyWithInlineConfigParser(
88 getPath("InputIllegalThrowsTestClassNames.java"), expected);
89 }
90
91
92
93
94
95 @Test
96 public void testIgnoreOverriddenMethods() throws Exception {
97
98 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
99
100 verifyWithInlineConfigParser(
101 getPath("InputIllegalThrowsIgnoreOverriddenMethods.java"), expected);
102 }
103
104
105
106
107
108 @Test
109 public void testNotIgnoreOverriddenMethods() throws Exception {
110
111 final String[] expected = {
112 "17:36: " + getCheckMessage(MSG_KEY, "RuntimeException"),
113 "22:51: " + getCheckMessage(MSG_KEY, "RuntimeException"),
114 };
115
116 verifyWithInlineConfigParser(
117 getPath("InputIllegalThrowsNotIgnoreOverriddenMethod.java"), expected);
118 }
119
120 @Test
121 public void testTokensNotNull() {
122 final IllegalThrowsCheck check = new IllegalThrowsCheck();
123 assertWithMessage("Acceptable tokens should not be null")
124 .that(check.getAcceptableTokens())
125 .isNotNull();
126 assertWithMessage("Default tokens should not be null")
127 .that(check.getDefaultTokens())
128 .isNotNull();
129 assertWithMessage("Required tokens should not be null")
130 .that(check.getRequiredTokens())
131 .isNotNull();
132 }
133
134 }