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.sizes;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.sizes.ParameterNumberCheck.MSG_KEY;
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 ParameterNumberCheckTest
32 extends AbstractModuleTestSupport {
33
34 @Override
35 protected String getPackageLocation() {
36 return "com/puppycrawl/tools/checkstyle/checks/sizes/parameternumber";
37 }
38
39 @Test
40 public void testGetRequiredTokens() {
41 final ParameterNumberCheck checkObj = new ParameterNumberCheck();
42 assertWithMessage("ParameterNumberCheck#getRequiredTokens should return empty array "
43 + "by default")
44 .that(checkObj.getRequiredTokens())
45 .isEqualTo(CommonUtil.EMPTY_INT_ARRAY);
46 }
47
48 @Test
49 public void testGetAcceptableTokens() {
50 final ParameterNumberCheck paramNumberCheckObj =
51 new ParameterNumberCheck();
52 final int[] actual = paramNumberCheckObj.getAcceptableTokens();
53 final int[] expected = {
54 TokenTypes.METHOD_DEF,
55 TokenTypes.CTOR_DEF,
56 };
57
58 assertWithMessage("Default acceptable tokens are invalid")
59 .that(actual)
60 .isEqualTo(expected);
61 }
62
63 @Test
64 public void testDefault()
65 throws Exception {
66 final String[] expected = {
67 "198:10: " + getCheckMessage(MSG_KEY, 7, 9),
68 };
69 verifyWithInlineConfigParser(
70 getPath("InputParameterNumberSimple.java"), expected);
71 }
72
73 @Test
74 public void testNum()
75 throws Exception {
76 final String[] expected = {
77 "75:9: " + getCheckMessage(MSG_KEY, 2, 3),
78 "198:10: " + getCheckMessage(MSG_KEY, 2, 9),
79 };
80 verifyWithInlineConfigParser(
81 getPath("InputParameterNumberSimple2.java"), expected);
82 }
83
84 @Test
85 public void testMaxParam()
86 throws Exception {
87 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
88 verifyWithInlineConfigParser(
89 getPath("InputParameterNumberSimple3.java"), expected);
90 }
91
92 @Test
93 public void shouldLogActualParameterNumber()
94 throws Exception {
95 final String[] expected = {
96 "199:10: 7,9",
97 };
98 verifyWithInlineConfigParser(
99 getPath("InputParameterNumberSimple4.java"), expected);
100 }
101
102 @Test
103 public void testIgnoreOverriddenMethods()
104 throws Exception {
105 final String[] expected = {
106 "15:10: " + getCheckMessage(MSG_KEY, 7, 8),
107 "20:10: " + getCheckMessage(MSG_KEY, 7, 8),
108 };
109 verifyWithInlineConfigParser(
110 getPath("InputParameterNumber.java"), expected);
111 }
112
113 @Test
114 public void testIgnoreOverriddenMethodsFalse()
115 throws Exception {
116 final String[] expected = {
117 "15:10: " + getCheckMessage(MSG_KEY, 7, 8),
118 "20:10: " + getCheckMessage(MSG_KEY, 7, 8),
119 "28:10: " + getCheckMessage(MSG_KEY, 7, 8),
120 "33:10: " + getCheckMessage(MSG_KEY, 7, 8),
121 };
122 verifyWithInlineConfigParser(
123 getPath("InputParameterNumber2.java"), expected);
124 }
125
126 @Test
127 public void testIgnoreAnnotatedBy() throws Exception {
128 final String[] expected = {
129 "23:10: " + getCheckMessage(MSG_KEY, 2, 3),
130 "30:10: " + getCheckMessage(MSG_KEY, 2, 4),
131 "35:14: " + getCheckMessage(MSG_KEY, 2, 3),
132 "43:9: " + getCheckMessage(MSG_KEY, 2, 4),
133 "58:30: " + getCheckMessage(MSG_KEY, 2, 3),
134 "62:29: " + getCheckMessage(MSG_KEY, 2, 3),
135 "77:34: " + getCheckMessage(MSG_KEY, 2, 4),
136 "97:10: " + getCheckMessage(MSG_KEY, 2, 3),
137 "106:14: " + getCheckMessage(MSG_KEY, 2, 3),
138 };
139 verifyWithInlineConfigParser(
140 getPath("InputParameterNumberIgnoreAnnotatedBy.java"), expected);
141 }
142
143 @Test
144 public void testIgnoreAnnotatedByFullyQualifiedClassName() throws Exception {
145 final String[] expected = {
146 "15:10: " + getCheckMessage(MSG_KEY, 2, 3),
147 "17:10: " + getCheckMessage(MSG_KEY, 2, 3),
148 "23:10: " + getCheckMessage(MSG_KEY, 2, 3),
149 "27:10: " + getCheckMessage(MSG_KEY, 2, 3),
150 "41:14: " + getCheckMessage(MSG_KEY, 2, 3),
151 "45:14: " + getCheckMessage(MSG_KEY, 2, 3),
152 };
153 verifyWithInlineConfigParser(
154 getPath("InputParameterNumberIgnoreAnnotatedByFullyQualifiedClassName.java"),
155 expected);
156 }
157 }