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.RecordComponentNumberCheck.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.checks.naming.AccessModifierOption;
30 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
31 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
32
33 public class RecordComponentNumberCheckTest extends AbstractModuleTestSupport {
34
35 @Override
36 protected String getPackageLocation() {
37 return "com/puppycrawl/tools/checkstyle/checks/sizes/recordcomponentnumber";
38 }
39
40 @Test
41 public void testGetRequiredTokens() {
42 final RecordComponentNumberCheck checkObj = new RecordComponentNumberCheck();
43 final int[] actual = checkObj.getRequiredTokens();
44 final int[] expected = {
45 TokenTypes.RECORD_DEF,
46 };
47
48 assertWithMessage("Default required tokens are invalid")
49 .that(actual)
50 .isEqualTo(expected);
51
52 }
53
54 @Test
55 public void testGetAcceptableTokens() {
56 final RecordComponentNumberCheck checkObj = new RecordComponentNumberCheck();
57 final int[] actual = checkObj.getAcceptableTokens();
58 final int[] expected = {
59 TokenTypes.RECORD_DEF,
60 };
61
62 assertWithMessage("Default acceptable tokens are invalid")
63 .that(actual)
64 .isEqualTo(expected);
65 }
66
67 @Test
68 public void testDefault() throws Exception {
69
70 final int max = 8;
71
72 final String[] expected = {
73 "57:5: " + getCheckMessage(MSG_KEY, 14, max),
74 "70:9: " + getCheckMessage(MSG_KEY, 14, max),
75 "76:13: " + getCheckMessage(MSG_KEY, 14, max),
76 "82:17: " + getCheckMessage(MSG_KEY, 11, max),
77 "101:5: " + getCheckMessage(MSG_KEY, 15, max),
78 "122:5: " + getCheckMessage(MSG_KEY, 15, max),
79 "132:5: " + getCheckMessage(MSG_KEY, 15, max),
80 };
81
82 verifyWithInlineConfigParser(
83 getNonCompilablePath("InputRecordComponentNumber.java"), expected);
84 }
85
86 @Test
87 public void testRecordComponentNumberTopLevel1() throws Exception {
88
89 final int max = 8;
90
91 final String[] expected = {
92 "12:1: " + getCheckMessage(MSG_KEY, 15, max),
93 };
94
95 verifyWithInlineConfigParser(
96 getNonCompilablePath("InputRecordComponentNumberTopLevel1.java"),
97 expected);
98 }
99
100 @Test
101 public void testRecordComponentNumberTopLevel2() throws Exception {
102
103 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
104
105 verifyWithInlineConfigParser(
106 getNonCompilablePath("InputRecordComponentNumberTopLevel2.java"),
107 expected);
108 }
109
110 @Test
111 public void testRecordComponentNumberMax1() throws Exception {
112
113 final int max = 1;
114
115 final String[] expected = {
116 "28:5: " + getCheckMessage(MSG_KEY, 2, max),
117 "32:5: " + getCheckMessage(MSG_KEY, 3, max),
118 "36:5: " + getCheckMessage(MSG_KEY, 5, max),
119 "52:5: " + getCheckMessage(MSG_KEY, 7, max),
120 "57:5: " + getCheckMessage(MSG_KEY, 14, max),
121 "66:9: " + getCheckMessage(MSG_KEY, 3, max),
122 "70:9: " + getCheckMessage(MSG_KEY, 14, max),
123 "76:13: " + getCheckMessage(MSG_KEY, 14, max),
124 "82:17: " + getCheckMessage(MSG_KEY, 6, max),
125 "96:5: " + getCheckMessage(MSG_KEY, 4, max),
126 "100:5: " + getCheckMessage(MSG_KEY, 15, max),
127 "110:5: " + getCheckMessage(MSG_KEY, 3, max),
128 "114:5: " + getCheckMessage(MSG_KEY, 6, max),
129 "125:5: " + getCheckMessage(MSG_KEY, 2, max),
130 };
131
132 verifyWithInlineConfigParser(
133 getNonCompilablePath("InputRecordComponentNumberMax1.java"), expected);
134 }
135
136 @Test
137 public void testRecordComponentNumberMax20() throws Exception {
138 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
139
140 verifyWithInlineConfigParser(
141 getNonCompilablePath("InputRecordComponentNumberMax20.java"), expected);
142 }
143
144 @Test
145 public void testRecordComponentNumberPrivateModifier() throws Exception {
146
147 final int max = 8;
148
149 final String[] expected = {
150 "70:9: " + getCheckMessage(MSG_KEY, 14, max),
151 "76:13: " + getCheckMessage(MSG_KEY, 14, max),
152 "122:5: " + getCheckMessage(MSG_KEY, 15, max),
153 };
154
155 verifyWithInlineConfigParser(
156 getNonCompilablePath("InputRecordComponentNumberPrivateModifier.java"), expected);
157 }
158
159
160
161
162
163
164
165
166
167 @Test
168 public void testCloneInAccessModifiersProperty() throws Exception {
169 final AccessModifierOption[] input = {
170 AccessModifierOption.PACKAGE,
171 };
172 final RecordComponentNumberCheck check = new RecordComponentNumberCheck();
173 check.setAccessModifiers(input);
174
175 assertWithMessage("check creates its own instance of access modifier array")
176 .that(System.identityHashCode(
177 TestUtil.getClassDeclaredField(RecordComponentNumberCheck.class, "accessModifiers")
178 .get(check)))
179 .isNotEqualTo(System.identityHashCode(input));
180 }
181
182 }