View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.coding;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck.MSG_ACCESS;
24  import static com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck.MSG_CONSTRUCTOR;
25  import static com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck.MSG_INSTANCE;
26  import static com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck.MSG_STATIC;
27  
28  import java.util.SortedSet;
29  
30  import org.junit.jupiter.api.Test;
31  
32  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
33  import com.puppycrawl.tools.checkstyle.DetailAstImpl;
34  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35  import com.puppycrawl.tools.checkstyle.api.Violation;
36  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
37  
38  public class DeclarationOrderCheckTest
39      extends AbstractModuleTestSupport {
40  
41      @Override
42      protected String getPackageLocation() {
43          return "com/puppycrawl/tools/checkstyle/checks/coding/declarationorder";
44      }
45  
46      @Test
47      public void testDefault() throws Exception {
48  
49          final String[] expected = {
50              "16:5: " + getCheckMessage(MSG_ACCESS),
51              "21:5: " + getCheckMessage(MSG_ACCESS),
52              "26:5: " + getCheckMessage(MSG_ACCESS),
53              "29:5: " + getCheckMessage(MSG_ACCESS),
54              "35:5: " + getCheckMessage(MSG_STATIC),
55              "42:9: " + getCheckMessage(MSG_ACCESS),
56              "60:9: " + getCheckMessage(MSG_STATIC),
57              "69:5: " + getCheckMessage(MSG_CONSTRUCTOR),
58              "95:5: " + getCheckMessage(MSG_INSTANCE),
59              "107:9: " + getCheckMessage(MSG_ACCESS),
60              "115:9: " + getCheckMessage(MSG_STATIC),
61              "121:5: " + getCheckMessage(MSG_ACCESS),
62              "126:5: " + getCheckMessage(MSG_ACCESS),
63              "131:5: " + getCheckMessage(MSG_ACCESS),
64              "134:5: " + getCheckMessage(MSG_ACCESS),
65              "140:5: " + getCheckMessage(MSG_STATIC),
66              "147:9: " + getCheckMessage(MSG_ACCESS),
67              "158:9: " + getCheckMessage(MSG_STATIC),
68              "167:5: " + getCheckMessage(MSG_CONSTRUCTOR),
69              "193:5: " + getCheckMessage(MSG_INSTANCE),
70              "198:9: " + getCheckMessage(MSG_ACCESS),
71          };
72          verifyWithInlineConfigParser(
73                  getPath("InputDeclarationOrder.java"), expected);
74      }
75  
76      @Test
77      public void testOnlyConstructors() throws Exception {
78  
79          final String[] expected = {
80              "53:9: " + getCheckMessage(MSG_STATIC),
81              "62:5: " + getCheckMessage(MSG_CONSTRUCTOR),
82              "88:5: " + getCheckMessage(MSG_INSTANCE),
83              "107:9: " + getCheckMessage(MSG_STATIC),
84              "143:9: " + getCheckMessage(MSG_STATIC),
85              "152:5: " + getCheckMessage(MSG_CONSTRUCTOR),
86              "178:5: " + getCheckMessage(MSG_INSTANCE),
87          };
88          verifyWithInlineConfigParser(
89                  getPath("InputDeclarationOrderOnlyConstructors.java"), expected);
90      }
91  
92      @Test
93      public void testOnlyModifiers() throws Exception {
94  
95          final String[] expected = {
96              "16:5: " + getCheckMessage(MSG_ACCESS),
97              "21:5: " + getCheckMessage(MSG_ACCESS),
98              "26:5: " + getCheckMessage(MSG_ACCESS),
99              "29:5: " + getCheckMessage(MSG_ACCESS),
100             "35:5: " + getCheckMessage(MSG_STATIC),
101             "42:9: " + getCheckMessage(MSG_ACCESS),
102             "60:9: " + getCheckMessage(MSG_STATIC),
103             "94:5: " + getCheckMessage(MSG_INSTANCE),
104             "106:9: " + getCheckMessage(MSG_ACCESS),
105             "114:9: " + getCheckMessage(MSG_STATIC),
106             "120:5: " + getCheckMessage(MSG_ACCESS),
107             "125:5: " + getCheckMessage(MSG_ACCESS),
108             "130:5: " + getCheckMessage(MSG_ACCESS),
109             "133:5: " + getCheckMessage(MSG_ACCESS),
110             "139:5: " + getCheckMessage(MSG_STATIC),
111             "146:9: " + getCheckMessage(MSG_ACCESS),
112             "157:9: " + getCheckMessage(MSG_STATIC),
113             "191:5: " + getCheckMessage(MSG_INSTANCE),
114             "196:9: " + getCheckMessage(MSG_ACCESS),
115         };
116         verifyWithInlineConfigParser(
117                 getPath("InputDeclarationOrderOnlyModifiers.java"), expected);
118     }
119 
120     @Test
121     public void testTokensNotNull() {
122         final DeclarationOrderCheck check = new DeclarationOrderCheck();
123         assertWithMessage("Acceptable tokens should not be null")
124             .that(check.getAcceptableTokens())
125             .isNotNull();
126         assertWithMessage("Default tokens should not be null")
127             .that(check.getDefaultTokens())
128             .isNotNull();
129         assertWithMessage("Required tokens should not be null")
130             .that(check.getRequiredTokens())
131             .isNotNull();
132     }
133 
134     @Test
135     public void testParents() {
136         final DetailAstImpl parent = new DetailAstImpl();
137         parent.setType(TokenTypes.STATIC_INIT);
138         final DetailAstImpl method = new DetailAstImpl();
139         method.setType(TokenTypes.METHOD_DEF);
140         parent.setFirstChild(method);
141         final DetailAstImpl ctor = new DetailAstImpl();
142         ctor.setType(TokenTypes.CTOR_DEF);
143         method.setNextSibling(ctor);
144 
145         final DeclarationOrderCheck check = new DeclarationOrderCheck();
146 
147         check.visitToken(method);
148         final SortedSet<Violation> violations1 = check.getViolations();
149 
150         assertWithMessage("No exception violations expected")
151             .that(violations1)
152             .isEmpty();
153 
154         check.visitToken(ctor);
155         final SortedSet<Violation> violations2 = check.getViolations();
156 
157         assertWithMessage("No exception violations expected")
158             .that(violations2)
159             .isEmpty();
160     }
161 
162     @Test
163     public void testImproperToken() {
164         final DetailAstImpl parent = new DetailAstImpl();
165         parent.setType(TokenTypes.STATIC_INIT);
166         final DetailAstImpl array = new DetailAstImpl();
167         array.setType(TokenTypes.ARRAY_INIT);
168         parent.setFirstChild(array);
169 
170         final DeclarationOrderCheck check = new DeclarationOrderCheck();
171 
172         check.visitToken(array);
173         final SortedSet<Violation> violations = check.getViolations();
174 
175         assertWithMessage("No exception violations expected")
176             .that(violations)
177             .isEmpty();
178     }
179 
180     @Test
181     public void testForwardReference() throws Exception {
182         final String[] expected = {
183             "20:5: " + getCheckMessage(MSG_ACCESS),
184             "21:5: " + getCheckMessage(MSG_ACCESS),
185             "22:5: " + getCheckMessage(MSG_ACCESS),
186             "23:5: " + getCheckMessage(MSG_ACCESS),
187             "24:5: " + getCheckMessage(MSG_ACCESS),
188             "25:5: " + getCheckMessage(MSG_ACCESS),
189             "31:5: " + getCheckMessage(MSG_ACCESS),
190             "49:5: " + getCheckMessage(MSG_STATIC),
191             "69:5: " + getCheckMessage(MSG_ACCESS),
192         };
193         verifyWithInlineConfigParser(
194                 getPath("InputDeclarationOrderForwardReference.java"), expected);
195     }
196 
197     @Test
198     public void testDeclarationOrderRecordsAndCompactCtors() throws Exception {
199         final String[] expected = {
200             "21:9: " + getCheckMessage(MSG_CONSTRUCTOR),
201             "24:9: " + getCheckMessage(MSG_STATIC),
202             "33:9: " + getCheckMessage(MSG_CONSTRUCTOR),
203             "36:9: " + getCheckMessage(MSG_STATIC),
204             "43:9: " + getCheckMessage(MSG_STATIC),
205         };
206         verifyWithInlineConfigParser(
207                 getNonCompilablePath("InputDeclarationOrderRecordsAndCompactCtors.java"),
208             expected);
209     }
210 
211     @Test
212     public void testDeclarationOrderInterfaceMemberScopeIsPublic() throws Exception {
213         final String[] expected = {
214             "21:3: " + getCheckMessage(MSG_STATIC),
215         };
216         verifyWithInlineConfigParser(
217                 getPath("InputDeclarationOrderInterfaceMemberScopeIsPublic.java"),
218             expected);
219     }
220 
221     @Test
222     public void testVariableAccess() throws Exception {
223         final String[] expected = {
224             "23:5: " + getCheckMessage(MSG_ACCESS),
225         };
226         verifyWithInlineConfigParser(
227                 getPath("InputDeclarationOrderVariableAccess.java"), expected);
228     }
229 
230     @Test
231     public void testAvoidDuplicatesForStaticFinalFields() throws Exception {
232         final String[] expected = {
233             "14:5: " + getCheckMessage(MSG_STATIC),
234         };
235         verifyWithInlineConfigParser(
236                 getPath("InputDeclarationOrderAvoidDuplicatesInStaticFinalFields.java"),
237                 expected);
238     }
239 
240     @Test
241     public void test() throws Exception {
242         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
243         verifyWithInlineConfigParser(
244                 getPath("InputDeclarationOrder2.java"),
245                 expected);
246     }
247 
248 }