View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 testDefaultOne() 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          };
79  
80          verifyWithInlineConfigParser(
81                  getPath("InputRecordComponentNumberOne.java"), expected);
82      }
83  
84      @Test
85      public void testDefaultTwo() throws Exception {
86  
87          final int max = 8;
88  
89          final String[] expected = {
90              "38:3: " + getCheckMessage(MSG_KEY, 15, max),
91              "48:3: " + getCheckMessage(MSG_KEY, 15, max),
92          };
93  
94          verifyWithInlineConfigParser(
95                  getPath("InputRecordComponentNumberTwo.java"), expected);
96      }
97  
98      @Test
99      public void testRecordComponentNumberTopLevel1() throws Exception {
100 
101         final int max = 8;
102 
103         final String[] expected = {
104             "12:1: " + getCheckMessage(MSG_KEY, 15, max),
105         };
106 
107         verifyWithInlineConfigParser(
108                 getPath("InputRecordComponentNumberTopLevel1.java"),
109                 expected);
110     }
111 
112     @Test
113     public void testRecordComponentNumberTopLevel2() throws Exception {
114 
115         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
116 
117         verifyWithInlineConfigParser(
118                 getPath("InputRecordComponentNumberTopLevel2.java"),
119                 expected);
120     }
121 
122     @Test
123     public void testRecordComponentNumberMax1() throws Exception {
124 
125         final int max = 1;
126 
127         final String[] expected = {
128             "28:5: " + getCheckMessage(MSG_KEY, 2, max),
129             "32:5: " + getCheckMessage(MSG_KEY, 3, max),
130             "36:5: " + getCheckMessage(MSG_KEY, 5, max),
131             "52:5: " + getCheckMessage(MSG_KEY, 7, max),
132             "57:5: " + getCheckMessage(MSG_KEY, 14, max),
133             "65:9: " + getCheckMessage(MSG_KEY, 3, max),
134             "69:9: " + getCheckMessage(MSG_KEY, 14, max),
135             "73:13: " + getCheckMessage(MSG_KEY, 14, max),
136             "77:17: " + getCheckMessage(MSG_KEY, 6, max),
137             "90:5: " + getCheckMessage(MSG_KEY, 4, max),
138             "92:5: " + getCheckMessage(MSG_KEY, 15, max),
139             "102:5: " + getCheckMessage(MSG_KEY, 3, max),
140             "106:5: " + getCheckMessage(MSG_KEY, 6, max),
141             "117:5: " + getCheckMessage(MSG_KEY, 2, max),
142         };
143 
144         verifyWithInlineConfigParser(
145                 getPath("InputRecordComponentNumberMax1.java"), expected);
146     }
147 
148     @Test
149     public void testRecordComponentNumberMax20() throws Exception {
150         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
151 
152         verifyWithInlineConfigParser(
153                 getPath("InputRecordComponentNumberMax20.java"), expected);
154     }
155 
156     @Test
157     public void testRecordComponentNumberPrivateModifierOne() throws Exception {
158 
159         final int max = 8;
160 
161         final String[] expected = {
162             "70:9: " + getCheckMessage(MSG_KEY, 14, max),
163             "76:13: " + getCheckMessage(MSG_KEY, 14, max),
164         };
165 
166         verifyWithInlineConfigParser(
167                 getPath("InputRecordComponentNumberPrivateModifierOne.java"), expected);
168     }
169 
170     @Test
171     public void testRecordComponentNumberPrivateModifierTwo() throws Exception {
172 
173         final int max = 8;
174 
175         final String[] expected = {
176             "70:9: " + getCheckMessage(MSG_KEY, 14, max),
177             "76:13: " + getCheckMessage(MSG_KEY, 14, max),
178         };
179 
180         verifyWithInlineConfigParser(
181                 getPath("InputRecordComponentNumberPrivateModifierOne.java"), expected);
182     }
183 
184     /**
185      * Checks that the check when given an array, creates it's own instance of
186      * the array and is not re-using and possible overwriting the one given to
187      * it. Without this, pitest says {@code Arrays.copyOf} is not needed, but it
188      * is needed for other style checks.
189      *
190      * @throws Exception if an error occurs.
191      */
192     @Test
193     public void testCloneInAccessModifiersProperty() throws Exception {
194         final AccessModifierOption[] input = {
195             AccessModifierOption.PACKAGE,
196         };
197         final RecordComponentNumberCheck check = new RecordComponentNumberCheck();
198         check.setAccessModifiers(input);
199 
200         assertWithMessage("check creates its own instance of access modifier array")
201             .that(System.identityHashCode(
202                 TestUtil.getClassDeclaredField(RecordComponentNumberCheck.class, "accessModifiers")
203                         .get(check)))
204             .isNotEqualTo(System.identityHashCode(input));
205     }
206 
207 }