View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.annotation;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.annotation.MissingOverrideOnRecordAccessorCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
29  
30  public class MissingOverrideOnRecordAccessorCheckTest extends AbstractModuleTestSupport {
31  
32      @Override
33      public String getPackageLocation() {
34          return "com/puppycrawl/tools/checkstyle/checks/annotation/missingoverrideonrecordaccessor";
35      }
36  
37      @Test
38      public void testGetAcceptableTokens() {
39          final MissingOverrideOnRecordAccessorCheck check =
40              new MissingOverrideOnRecordAccessorCheck();
41          assertWithMessage("Acceptable tokens should equal required tokens")
42              .that(check.getAcceptableTokens())
43              .isEqualTo(check.getRequiredTokens());
44      }
45  
46      @Test
47      public void testRecordAccessorWithoutOverride() throws Exception {
48          final String[] expected = {
49              "13:5: " + getCheckMessage(MSG_KEY),
50          };
51          verifyWithInlineConfigParser(
52                  getPath("InputMissingOverrideOnRecordAccessorViolation.java"),
53                  expected);
54      }
55  
56      @Test
57      public void testRecordAccessorWithOverride() throws Exception {
58          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
59          verifyWithInlineConfigParser(
60                  getPath("InputMissingOverrideOnRecordAccessorValid.java"),
61                  expected);
62      }
63  
64      @Test
65      public void testMultipleComponentsWithoutOverride() throws Exception {
66          final String[] expected = {
67              "13:5: " + getCheckMessage(MSG_KEY),
68              "18:5: " + getCheckMessage(MSG_KEY),
69              "23:5: " + getCheckMessage(MSG_KEY),
70          };
71          verifyWithInlineConfigParser(
72                  getPath("InputMissingOverrideOnRecordAccessorMultiple.java"),
73                  expected);
74      }
75  
76      @Test
77      public void testRecordWithImplementsClause() throws Exception {
78          final String[] expected = {
79              "18:5: " + getCheckMessage(MSG_KEY),
80          };
81          verifyWithInlineConfigParser(
82                  getPath("InputMissingOverrideOnRecordAccessorWithImplements.java"),
83                  expected);
84      }
85  
86      @Test
87      public void testNonAccessorMethodInRecord() throws Exception {
88          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
89          verifyWithInlineConfigParser(
90                  getPath("InputMissingOverrideOnRecordAccessorNonAccessor.java"),
91                  expected);
92      }
93  
94      @Test
95      public void testMethodWithParameters() throws Exception {
96          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
97          verifyWithInlineConfigParser(
98                  getPath("InputMissingOverrideOnRecordAccessorWithParams.java"),
99                  expected);
100     }
101 
102     @Test
103     public void testNestedRecord() throws Exception {
104         final String[] expected = {
105             "20:9: " + getCheckMessage(MSG_KEY),
106         };
107         verifyWithInlineConfigParser(
108                 getPath("InputMissingOverrideOnRecordAccessorNested.java"),
109                 expected);
110     }
111 
112     @Test
113     public void testRecordInsideClass() throws Exception {
114         final String[] expected = {
115             "14:9: " + getCheckMessage(MSG_KEY),
116         };
117         verifyWithInlineConfigParser(
118                 getPath("InputMissingOverrideOnRecordAccessorInClass.java"),
119                 expected);
120     }
121 
122     @Test
123     public void testGenericRecord() throws Exception {
124         final String[] expected = {
125             "13:5: " + getCheckMessage(MSG_KEY),
126         };
127         verifyWithInlineConfigParser(
128                 getPath("InputMissingOverrideOnRecordAccessorGeneric.java"),
129                 expected);
130     }
131 
132     @Test
133     public void testRegularClass() throws Exception {
134         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
135         verifyWithInlineConfigParser(
136                 getPath("InputMissingOverrideOnRecordAccessorRegularClass.java"),
137                 expected);
138     }
139 
140     @Test
141     public void testSomeAccessorsAnnotated() throws Exception {
142         final String[] expected = {
143             "13:5: " + getCheckMessage(MSG_KEY),
144         };
145         verifyWithInlineConfigParser(
146                 getPath("InputMissingOverrideOnRecordAccessorSomeAnnotated.java"),
147                 expected);
148     }
149 }