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.naming;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.naming.AbstractNameCheck.MSG_INVALID_PATTERN;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
29 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30
31 public class CatchParameterNameCheckTest extends AbstractModuleTestSupport {
32
33 @Override
34 protected String getPackageLocation() {
35 return "com/puppycrawl/tools/checkstyle/checks/naming/catchparametername";
36 }
37
38 @Test
39 public void testTokens() {
40 final CatchParameterNameCheck catchParameterNameCheck = new CatchParameterNameCheck();
41 final int[] expected = {TokenTypes.PARAMETER_DEF};
42
43 assertWithMessage("Default required tokens are invalid")
44 .that(catchParameterNameCheck.getRequiredTokens())
45 .isEqualTo(expected);
46 assertWithMessage("Default acceptable tokens are invalid")
47 .that(catchParameterNameCheck.getAcceptableTokens())
48 .isEqualTo(expected);
49 }
50
51 @Test
52 public void testDefaultConfigurationOnCorrectFile() throws Exception {
53 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
54
55 verifyWithInlineConfigParser(
56 getPath("InputCatchParameterNameSimple.java"), expected);
57 }
58
59 @Test
60 public void testDefaultConfigurationOnFileWithViolations() throws Exception {
61 final String defaultFormat = "^(e|t|ex|[a-z][a-z][a-zA-Z]+|_)$";
62
63 final String[] expected = {
64 "25:28: " + getCheckMessage(MSG_INVALID_PATTERN, "exception1", defaultFormat),
65 "35:39: " + getCheckMessage(MSG_INVALID_PATTERN, "ie", defaultFormat),
66 "38:28: " + getCheckMessage(MSG_INVALID_PATTERN, "iException", defaultFormat),
67 "41:28: " + getCheckMessage(MSG_INVALID_PATTERN, "ok", defaultFormat),
68 "45:28: " + getCheckMessage(MSG_INVALID_PATTERN, "e1", defaultFormat),
69 "47:32: " + getCheckMessage(MSG_INVALID_PATTERN, "e2", defaultFormat),
70 "51:28: " + getCheckMessage(MSG_INVALID_PATTERN, "t1", defaultFormat),
71 "53:32: " + getCheckMessage(MSG_INVALID_PATTERN, "t2", defaultFormat),
72 };
73
74 verifyWithInlineConfigParser(
75 getPath("InputCatchParameterName.java"), expected);
76 }
77
78 @Test
79 public void testCustomFormatFromJavadoc() throws Exception {
80
81 final String[] expected = {
82 "13:28: " + getCheckMessage(MSG_INVALID_PATTERN, "e", "^[a-z][a-zA-Z0-9]+$"),
83 "31:28: " + getCheckMessage(MSG_INVALID_PATTERN, "t", "^[a-z][a-zA-Z0-9]+$"),
84 };
85
86 verifyWithInlineConfigParser(
87 getPath("InputCatchParameterName2.java"), expected);
88 }
89
90 @Test
91 public void testCustomFormatWithNoAnchors() throws Exception {
92
93 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
94
95 verifyWithInlineConfigParser(
96 getPath("InputCatchParameterName3.java"), expected);
97 }
98
99 @Test
100 public void testCatchParameterNameUnnamed() throws Exception {
101 final String defaultFormat = "^(e|t|ex|[a-z][a-z][a-zA-Z]+|_)$";
102
103 final String[] expected = {
104 "18:28: " + getCheckMessage(MSG_INVALID_PATTERN, "__", defaultFormat),
105 "24:28: " + getCheckMessage(MSG_INVALID_PATTERN, "_BAD", defaultFormat),
106 "27:28: " + getCheckMessage(MSG_INVALID_PATTERN, "BAD__", defaultFormat),
107 };
108
109 verifyWithInlineConfigParser(
110 getNonCompilablePath("InputCatchParameterNameUnnamed.java"), expected);
111 }
112 }