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.modifier;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck.MSG_ANNOTATION_ORDER;
24  import static com.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck.MSG_MODIFIER_ORDER;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31  
32  public class ModifierOrderCheckTest
33      extends AbstractModuleTestSupport {
34  
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/modifier/modifierorder";
38      }
39  
40      @Test
41      public void testGetRequiredTokens() {
42          final ModifierOrderCheck checkObj = new ModifierOrderCheck();
43          final int[] expected = {TokenTypes.MODIFIERS};
44          assertWithMessage("Default required tokens are invalid")
45              .that(checkObj.getRequiredTokens())
46              .isEqualTo(expected);
47      }
48  
49      @Test
50      public void testIt() throws Exception {
51          final String[] expected = {
52              "15:10: " + getCheckMessage(MSG_MODIFIER_ORDER, "final"),
53              "19:12: " + getCheckMessage(MSG_MODIFIER_ORDER, "private"),
54              "25:14: " + getCheckMessage(MSG_MODIFIER_ORDER, "private"),
55              "35:13: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MyAnnotation2"),
56              "40:13: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MyAnnotation2"),
57              "50:35: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MyAnnotation4"),
58              "158:14: " + getCheckMessage(MSG_MODIFIER_ORDER, "default"),
59          };
60          verifyWithInlineConfigParser(
61                  getPath("InputModifierOrderIt.java"), expected);
62      }
63  
64      @Test
65      public void testDefaultMethods()
66              throws Exception {
67          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
68          verifyWithInlineConfigParser(
69                  getPath("InputModifierOrderDefaultMethods.java"), expected);
70      }
71  
72      @Test
73      public void testGetDefaultTokens() {
74          final ModifierOrderCheck modifierOrderCheckObj = new ModifierOrderCheck();
75          final int[] actual = modifierOrderCheckObj.getDefaultTokens();
76          final int[] expected = {TokenTypes.MODIFIERS};
77          final int[] unexpectedArray = {
78              TokenTypes.MODIFIERS,
79              TokenTypes.OBJBLOCK,
80          };
81          assertWithMessage("Default tokens are invalid")
82              .that(actual)
83              .isEqualTo(expected);
84          final int[] unexpectedEmptyArray = CommonUtil.EMPTY_INT_ARRAY;
85          assertWithMessage("Default tokens should not be empty array")
86              .that(actual)
87              .isNotEqualTo(unexpectedEmptyArray);
88          assertWithMessage("Invalid default tokens")
89              .that(actual)
90              .isNotEqualTo(unexpectedArray);
91          assertWithMessage("Default tokens should not be null")
92              .that(actual)
93              .isNotNull();
94      }
95  
96      @Test
97      public void testGetAcceptableTokens() {
98          final ModifierOrderCheck modifierOrderCheckObj = new ModifierOrderCheck();
99          final int[] actual = modifierOrderCheckObj.getAcceptableTokens();
100         final int[] expected = {TokenTypes.MODIFIERS};
101         final int[] unexpectedArray = {
102             TokenTypes.MODIFIERS,
103             TokenTypes.OBJBLOCK,
104         };
105         assertWithMessage("Default acceptable tokens are invalid")
106             .that(actual)
107             .isEqualTo(expected);
108         final int[] unexpectedEmptyArray = CommonUtil.EMPTY_INT_ARRAY;
109         assertWithMessage("Default tokens should not be empty array")
110             .that(actual)
111             .isNotEqualTo(unexpectedEmptyArray);
112         assertWithMessage("Invalid acceptable tokens")
113             .that(actual)
114             .isNotEqualTo(unexpectedArray);
115         assertWithMessage("Acceptable tokens should not be null")
116             .that(actual)
117             .isNotNull();
118     }
119 
120     @Test
121     public void testSkipTypeAnnotations() throws Exception {
122         final String[] expected = {
123             "110:13: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MethodAnnotation"),
124         };
125         verifyWithInlineConfigParser(
126                 getPath("InputModifierOrderTypeAnnotations.java"),
127             expected);
128     }
129 
130     @Test
131     public void testAnnotationOnAnnotationDeclaration() throws Exception {
132         final String[] expected = {
133             "9:8: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@InterfaceAnnotation"),
134         };
135         verifyWithInlineConfigParser(
136                 getPath("InputModifierOrderAnnotationDeclaration.java"), expected);
137     }
138 
139     @Test
140     public void testModifierOrderSealedAndNonSealed() throws Exception {
141         final String[] expected = {
142             "10:8: " + getCheckMessage(MSG_MODIFIER_ORDER, "public"),
143             "26:12: " + getCheckMessage(MSG_MODIFIER_ORDER, "private"),
144             "44:10: " + getCheckMessage(MSG_MODIFIER_ORDER, "sealed"),
145             "50:11: " + getCheckMessage(MSG_MODIFIER_ORDER, "public"),
146             "53:14: " + getCheckMessage(MSG_MODIFIER_ORDER, "static"),
147             "58:10: " + getCheckMessage(MSG_MODIFIER_ORDER, "non-sealed"),
148         };
149         verifyWithInlineConfigParser(
150                 getNonCompilablePath("InputModifierOrderSealedAndNonSealed.java"), expected);
151     }
152 
153     @Test
154     public void testModifierOrderSealedAndNonSealedNoViolation() throws Exception {
155         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
156         verifyWithInlineConfigParser(
157                 getNonCompilablePath("InputModifierOrderSealedAndNonSealedNoViolation.java"),
158                 expected);
159     }
160 
161 }