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("InputCatchParameterNameSimpleOne1.java"), expected);
57 }
58
59 @Test
60 public void testDefaultConfigurationOnCorrectFile2() throws Exception {
61 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
62
63 verifyWithInlineConfigParser(
64 getPath("InputCatchParameterNameSimple2.java"), expected);
65 }
66
67 @Test
68 public void testDefaultConfigurationOnFileWithViolations() throws Exception {
69 final String defaultFormat = "^(e|t|ex|[a-z][a-z][a-zA-Z]+|_)$";
70
71 final String[] expected = {
72 "25:28: " + getCheckMessage(MSG_INVALID_PATTERN, "exception1", defaultFormat),
73 "35:39: " + getCheckMessage(MSG_INVALID_PATTERN, "ie", defaultFormat),
74 "38:28: " + getCheckMessage(MSG_INVALID_PATTERN, "iException", defaultFormat),
75 "41:28: " + getCheckMessage(MSG_INVALID_PATTERN, "ok", defaultFormat),
76 "45:28: " + getCheckMessage(MSG_INVALID_PATTERN, "e1", defaultFormat),
77 "47:32: " + getCheckMessage(MSG_INVALID_PATTERN, "e2", defaultFormat),
78 "51:28: " + getCheckMessage(MSG_INVALID_PATTERN, "t1", defaultFormat),
79 "53:32: " + getCheckMessage(MSG_INVALID_PATTERN, "t2", defaultFormat),
80 };
81
82 verifyWithInlineConfigParser(
83 getPath("InputCatchParameterName.java"), expected);
84 }
85
86 @Test
87 public void testCustomFormatFromJavadoc() throws Exception {
88
89 final String[] expected = {
90 "13:28: " + getCheckMessage(MSG_INVALID_PATTERN, "e", "^[a-z][a-zA-Z0-9]+$"),
91 "31:28: " + getCheckMessage(MSG_INVALID_PATTERN, "t", "^[a-z][a-zA-Z0-9]+$"),
92 };
93
94 verifyWithInlineConfigParser(
95 getPath("InputCatchParameterName2.java"), expected);
96 }
97
98 @Test
99 public void testCustomFormatWithNoAnchors() throws Exception {
100
101 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
102
103 verifyWithInlineConfigParser(
104 getPath("InputCatchParameterName3.java"), expected);
105 }
106
107 @Test
108 public void testCatchParameterNameUnnamed() throws Exception {
109 final String defaultFormat = "^(e|t|ex|[a-z][a-z][a-zA-Z]+|_)$";
110
111 final String[] expected = {
112 "18:28: " + getCheckMessage(MSG_INVALID_PATTERN, "__", defaultFormat),
113 "24:28: " + getCheckMessage(MSG_INVALID_PATTERN, "_BAD", defaultFormat),
114 "27:28: " + getCheckMessage(MSG_INVALID_PATTERN, "BAD__", defaultFormat),
115 };
116
117 verifyWithInlineConfigParser(
118 getNonCompilablePath("InputCatchParameterNameUnnamed.java"), expected);
119 }
120 }