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
32 public class ThrowsCountCheckTest extends AbstractModuleTestSupport {
33
34 @Override
35 protected String getPackageLocation() {
36 return "com/puppycrawl/tools/checkstyle/checks/design/throwscount";
37 }
38
39 @Test
40 public void testThrowsCountDefault() throws Exception {
41
42 final String[] expected = {
43 "26:20: " + getCheckMessage(MSG_KEY, 5, 4),
44 "32:20: " + getCheckMessage(MSG_KEY, 5, 4),
45 "38:20: " + getCheckMessage(MSG_KEY, 6, 4),
46 "66:43: " + getCheckMessage(MSG_KEY, 5, 4),
47 };
48
49 verifyWithInlineConfigParser(
50 getPath("InputThrowsCountDefault.java"), expected);
51 }
52
53 @Test
54 public void testThrowsCountCustomMaxCount() throws Exception {
55
56 final String[] expected = {
57 "36:20: " + getCheckMessage(MSG_KEY, 6, 5),
58 };
59
60 verifyWithInlineConfigParser(
61 getPath("InputThrowsCountCustomMaxCount.java"), expected);
62 }
63
64 @Test
65 public void testGetAcceptableTokens() {
66 final ThrowsCountCheck obj = new ThrowsCountCheck();
67 final int[] expected = {TokenTypes.LITERAL_THROWS};
68 assertWithMessage("Default acceptable tokens are invalid")
69 .that(obj.getAcceptableTokens())
70 .isEqualTo(expected);
71 }
72
73 @Test
74 public void testGetRequiredTokens() {
75 final ThrowsCountCheck obj = new ThrowsCountCheck();
76 final int[] expected = {TokenTypes.LITERAL_THROWS};
77 assertWithMessage("Default required tokens are invalid")
78 .that(obj.getRequiredTokens())
79 .isEqualTo(expected);
80 }
81
82 @Test
83 public void testWrongTokenType() {
84 final ThrowsCountCheck obj = new ThrowsCountCheck();
85 final DetailAstImpl ast = new DetailAstImpl();
86 ast.initialize(new CommonToken(TokenTypes.CLASS_DEF, "class"));
87 try {
88 obj.visitToken(ast);
89 assertWithMessage("IllegalStateException is expected").fail();
90 }
91 catch (IllegalStateException ex) {
92 assertWithMessage("Invalid exception message")
93 .that(ex.getMessage())
94 .isEqualTo(ast.toString());
95 }
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 }