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;
21
22 import static com.puppycrawl.tools.checkstyle.checks.FinalParametersCheck.MSG_KEY;
23
24 import org.junit.jupiter.api.Test;
25
26 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
27 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
28
29 public class FinalParametersCheckTest extends AbstractModuleTestSupport {
30
31 @Override
32 protected String getPackageLocation() {
33 return "com/puppycrawl/tools/checkstyle/checks/finalparameters";
34 }
35
36 @Test
37 public void testDefaultTokens() throws Exception {
38 final String[] expected = {
39 "27:26: " + getCheckMessage(MSG_KEY, "s"),
40 "42:26: " + getCheckMessage(MSG_KEY, "i"),
41 "47:26: " + getCheckMessage(MSG_KEY, "s"),
42 "57:17: " + getCheckMessage(MSG_KEY, "s"),
43 "73:17: " + getCheckMessage(MSG_KEY, "s"),
44 "79:17: " + getCheckMessage(MSG_KEY, "s"),
45 "94:45: " + getCheckMessage(MSG_KEY, "e"),
46 "97:36: " + getCheckMessage(MSG_KEY, "e"),
47 "114:18: " + getCheckMessage(MSG_KEY, "aParam"),
48 "117:18: " + getCheckMessage(MSG_KEY, "args"),
49 "120:18: " + getCheckMessage(MSG_KEY, "args"),
50 };
51 verifyWithInlineConfigParser(
52 getPath("InputFinalParameters.java"), expected);
53 }
54
55 @Test
56 public void testCtorToken() throws Exception {
57 final String[] expected = {
58 "28:27: " + getCheckMessage(MSG_KEY, "s"),
59 "43:27: " + getCheckMessage(MSG_KEY, "i"),
60 "48:27: " + getCheckMessage(MSG_KEY, "s"),
61 };
62 verifyWithInlineConfigParser(
63 getPath("InputFinalParameters2.java"), expected);
64 }
65
66 @Test
67 public void testMethodToken() throws Exception {
68 final String[] expected = {
69 "58:17: " + getCheckMessage(MSG_KEY, "s"),
70 "74:17: " + getCheckMessage(MSG_KEY, "s"),
71 "80:17: " + getCheckMessage(MSG_KEY, "s"),
72 "95:45: " + getCheckMessage(MSG_KEY, "e"),
73 "98:36: " + getCheckMessage(MSG_KEY, "e"),
74 "115:18: " + getCheckMessage(MSG_KEY, "aParam"),
75 "118:18: " + getCheckMessage(MSG_KEY, "args"),
76 "121:18: " + getCheckMessage(MSG_KEY, "args"),
77 };
78 verifyWithInlineConfigParser(
79 getPath("InputFinalParameters3.java"), expected);
80 }
81
82 @Test
83 public void testCatchToken() throws Exception {
84 final String[] expected = {
85 "130:16: " + getCheckMessage(MSG_KEY, "npe"),
86 "136:16: " + getCheckMessage(MSG_KEY, "e"),
87 "139:16: " + getCheckMessage(MSG_KEY, "e"),
88 };
89 verifyWithInlineConfigParser(
90 getPath("InputFinalParameters4.java"), expected);
91 }
92
93 @Test
94 public void testForEachClauseToken() throws Exception {
95 final String[] expected = {
96 "157:13: " + getCheckMessage(MSG_KEY, "s"),
97 "165:13: " + getCheckMessage(MSG_KEY, "s"),
98 };
99 verifyWithInlineConfigParser(
100 getPath("InputFinalParameters5.java"), expected);
101 }
102
103 @Test
104 public void testIgnorePrimitiveTypesParameters() throws Exception {
105 final String[] expected = {
106 "14:22: " + getCheckMessage(MSG_KEY, "k"),
107 "15:15: " + getCheckMessage(MSG_KEY, "s"),
108 "15:25: " + getCheckMessage(MSG_KEY, "o"),
109 "16:15: " + getCheckMessage(MSG_KEY, "array"),
110 "17:31: " + getCheckMessage(MSG_KEY, "s"),
111 "18:22: " + getCheckMessage(MSG_KEY, "l"),
112 "18:32: " + getCheckMessage(MSG_KEY, "s"),
113 };
114 verifyWithInlineConfigParser(
115 getPath("InputFinalParametersPrimitiveTypes.java"), expected);
116 }
117
118 @Test
119 public void testPrimitiveTypesParameters() throws Exception {
120 final String[] expected = {
121 "13:14: " + getCheckMessage(MSG_KEY, "i"),
122 "14:15: " + getCheckMessage(MSG_KEY, "i"),
123 "14:22: " + getCheckMessage(MSG_KEY, "k"),
124 "14:32: " + getCheckMessage(MSG_KEY, "s"),
125 "15:15: " + getCheckMessage(MSG_KEY, "s"),
126 "15:25: " + getCheckMessage(MSG_KEY, "o"),
127 "15:35: " + getCheckMessage(MSG_KEY, "l"),
128 "16:15: " + getCheckMessage(MSG_KEY, "array"),
129 "17:15: " + getCheckMessage(MSG_KEY, "i"),
130 "17:22: " + getCheckMessage(MSG_KEY, "x"),
131 "17:31: " + getCheckMessage(MSG_KEY, "s"),
132 "18:15: " + getCheckMessage(MSG_KEY, "x"),
133 "18:22: " + getCheckMessage(MSG_KEY, "l"),
134 "18:32: " + getCheckMessage(MSG_KEY, "s"),
135 };
136 verifyWithInlineConfigParser(
137 getPath("InputFinalParametersPrimitiveTypes2.java"), expected);
138 }
139
140 @Test
141 public void testReceiverParameters() throws Exception {
142 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
143 verifyWithInlineConfigParser(
144 getPath("InputFinalParametersReceiver.java"), expected);
145 }
146
147 @Test
148 public void testUnnamedParametersPropertyTrue() throws Exception {
149 final String[] expected = {
150 "25:18: " + getCheckMessage(MSG_KEY, "__"),
151 "30:18: " + getCheckMessage(MSG_KEY, "_e"),
152 "35:18: " + getCheckMessage(MSG_KEY, "e_"),
153 "46:14: " + getCheckMessage(MSG_KEY, "__"),
154 "49:14: " + getCheckMessage(MSG_KEY, "_i"),
155 "52:14: " + getCheckMessage(MSG_KEY, "i_"),
156 };
157 verifyWithInlineConfigParser(
158 getNonCompilablePath("InputFinalParametersUnnamedPropertyTrue.java"), expected);
159 }
160
161 @Test
162 public void testUnnamedParametersPropertyFalse() throws Exception {
163 final String[] expected = {
164 "20:18: " + getCheckMessage(MSG_KEY, "_"),
165 "25:18: " + getCheckMessage(MSG_KEY, "__"),
166 "30:18: " + getCheckMessage(MSG_KEY, "_e"),
167 "35:18: " + getCheckMessage(MSG_KEY, "e_"),
168 "46:14: " + getCheckMessage(MSG_KEY, "__"),
169 "43:14: " + getCheckMessage(MSG_KEY, "_"),
170 "49:14: " + getCheckMessage(MSG_KEY, "_i"),
171 "52:14: " + getCheckMessage(MSG_KEY, "i_"),
172 };
173 verifyWithInlineConfigParser(
174 getNonCompilablePath("InputFinalParametersUnnamedPropertyFalse.java"), expected);
175 }
176
177 }