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.design;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.design.ThrowsCountCheck.MSG_KEY;
24
25 import org.antlr.v4.runtime.CommonToken;
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29 import com.puppycrawl.tools.checkstyle.DetailAstImpl;
30 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
32
33 public class ThrowsCountCheckTest extends AbstractModuleTestSupport {
34
35 @Override
36 public String getPackageLocation() {
37 return "com/puppycrawl/tools/checkstyle/checks/design/throwscount";
38 }
39
40 @Test
41 public void testThrowsCountDefault() throws Exception {
42
43 final String[] expected = {
44 "26:20: " + getCheckMessage(MSG_KEY, 5, 4),
45 "32:20: " + getCheckMessage(MSG_KEY, 5, 4),
46 "38:20: " + getCheckMessage(MSG_KEY, 6, 4),
47 "66:43: " + getCheckMessage(MSG_KEY, 5, 4),
48 };
49
50 verifyWithInlineConfigParser(
51 getPath("InputThrowsCountDefault.java"), expected);
52 }
53
54 @Test
55 public void testThrowsCountCustomMaxCount() throws Exception {
56
57 final String[] expected = {
58 "36:20: " + getCheckMessage(MSG_KEY, 6, 5),
59 };
60
61 verifyWithInlineConfigParser(
62 getPath("InputThrowsCountCustomMaxCount.java"), expected);
63 }
64
65 @Test
66 public void testGetAcceptableTokens() {
67 final ThrowsCountCheck obj = new ThrowsCountCheck();
68 final int[] expected = {TokenTypes.LITERAL_THROWS};
69 assertWithMessage("Default acceptable tokens are invalid")
70 .that(obj.getAcceptableTokens())
71 .isEqualTo(expected);
72 }
73
74 @Test
75 public void testGetRequiredTokens() {
76 final ThrowsCountCheck obj = new ThrowsCountCheck();
77 final int[] expected = {TokenTypes.LITERAL_THROWS};
78 assertWithMessage("Default required tokens are invalid")
79 .that(obj.getRequiredTokens())
80 .isEqualTo(expected);
81 }
82
83 @Test
84 public void testWrongTokenType() {
85 final ThrowsCountCheck obj = new ThrowsCountCheck();
86 final DetailAstImpl ast = new DetailAstImpl();
87 ast.initialize(new CommonToken(TokenTypes.CLASS_DEF, "class"));
88 final IllegalStateException exc =
89 TestUtil.getExpectedThrowable(
90 IllegalStateException.class, () -> {
91 obj.visitToken(ast);
92 });
93 assertWithMessage("Invalid exception message")
94 .that(exc.getMessage())
95 .isEqualTo(ast.toString());
96 }
97
98 @Test
99 public void testThrowsCountNotIgnorePrivateMethods() throws Exception {
100 final String[] expected = {
101 "26:20: " + getCheckMessage(MSG_KEY, 5, 4),
102 "32:20: " + getCheckMessage(MSG_KEY, 5, 4),
103 "38:20: " + getCheckMessage(MSG_KEY, 6, 4),
104 "47:28: " + getCheckMessage(MSG_KEY, 5, 4),
105 "67:43: " + getCheckMessage(MSG_KEY, 5, 4),
106 };
107 verifyWithInlineConfigParser(
108 getPath("InputThrowsCountNotIgnorePrivateMethods.java"), expected);
109 }
110
111 @Test
112 public void testThrowsCountMethodWithAnnotation() throws Exception {
113 final String[] expected = {
114 "27:26: " + getCheckMessage(MSG_KEY, 5, 4),
115 };
116 verifyWithInlineConfigParser(
117 getPath("InputThrowsCountMethodWithAnnotation.java"), expected);
118 }
119
120 @Test
121 public void testThrowsCountMaxAllowZero() throws Exception {
122 final String[] expected = {
123 "17:20: " + getCheckMessage(MSG_KEY, 1, 0),
124 "21:20: " + getCheckMessage(MSG_KEY, 1, 0),
125 "25:20: " + getCheckMessage(MSG_KEY, 5, 0),
126 "31:20: " + getCheckMessage(MSG_KEY, 5, 0),
127 "37:20: " + getCheckMessage(MSG_KEY, 6, 0),
128 "46:28: " + getCheckMessage(MSG_KEY, 5, 0),
129 "67:43: " + getCheckMessage(MSG_KEY, 5, 0),
130 };
131 verifyWithInlineConfigParser(
132 getPath("InputThrowsCountMaxAllowZero.java"), expected);
133 }
134
135 }