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.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 testItOne() 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 };
59 verifyWithInlineConfigParser(
60 getPath("InputModifierOrderItOne.java"), expected);
61 }
62
63 @Test
64 public void testItTwo() throws Exception {
65 final String[] expected = {
66
67 "15:10: " + getCheckMessage(MSG_MODIFIER_ORDER, "final"),
68 "57:14: " + getCheckMessage(MSG_MODIFIER_ORDER, "default"),
69 };
70 verifyWithInlineConfigParser(
71 getPath("InputModifierOrderItTwo.java"), expected);
72 }
73
74 @Test
75 public void testDefaultMethods()
76 throws Exception {
77 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
78 verifyWithInlineConfigParser(
79 getPath("InputModifierOrderDefaultMethods.java"), expected);
80 }
81
82 @Test
83 public void testGetDefaultTokens() {
84 final ModifierOrderCheck modifierOrderCheckObj = new ModifierOrderCheck();
85 final int[] actual = modifierOrderCheckObj.getDefaultTokens();
86 final int[] expected = {TokenTypes.MODIFIERS};
87 final int[] unexpectedArray = {
88 TokenTypes.MODIFIERS,
89 TokenTypes.OBJBLOCK,
90 };
91 assertWithMessage("Default tokens are invalid")
92 .that(actual)
93 .isEqualTo(expected);
94 final int[] unexpectedEmptyArray = CommonUtil.EMPTY_INT_ARRAY;
95 assertWithMessage("Default tokens should not be empty array")
96 .that(actual)
97 .isNotEqualTo(unexpectedEmptyArray);
98 assertWithMessage("Invalid default tokens")
99 .that(actual)
100 .isNotEqualTo(unexpectedArray);
101 assertWithMessage("Default tokens should not be null")
102 .that(actual)
103 .isNotNull();
104 }
105
106 @Test
107 public void testGetAcceptableTokens() {
108 final ModifierOrderCheck modifierOrderCheckObj = new ModifierOrderCheck();
109 final int[] actual = modifierOrderCheckObj.getAcceptableTokens();
110 final int[] expected = {TokenTypes.MODIFIERS};
111 final int[] unexpectedArray = {
112 TokenTypes.MODIFIERS,
113 TokenTypes.OBJBLOCK,
114 };
115 assertWithMessage("Default acceptable tokens are invalid")
116 .that(actual)
117 .isEqualTo(expected);
118 final int[] unexpectedEmptyArray = CommonUtil.EMPTY_INT_ARRAY;
119 assertWithMessage("Default tokens should not be empty array")
120 .that(actual)
121 .isNotEqualTo(unexpectedEmptyArray);
122 assertWithMessage("Invalid acceptable tokens")
123 .that(actual)
124 .isNotEqualTo(unexpectedArray);
125 assertWithMessage("Acceptable tokens should not be null")
126 .that(actual)
127 .isNotNull();
128 }
129
130 @Test
131 public void testSkipTypeAnnotationsOne() throws Exception {
132 final String[] expected = {
133 "101:13: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MethodAnnotation"),
134 };
135 verifyWithInlineConfigParser(
136 getPath("InputModifierOrderTypeAnnotationsOne.java"),
137 expected);
138 }
139
140 @Test
141 public void testSkipTypeAnnotationsTwo() throws Exception {
142 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
143 verifyWithInlineConfigParser(
144 getPath("InputModifierOrderTypeAnnotationsTwo.java"),
145 expected);
146 }
147
148 @Test
149 public void testAnnotationOnAnnotationDeclaration() throws Exception {
150 final String[] expected = {
151 "9:8: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@InterfaceAnnotation"),
152 };
153 verifyWithInlineConfigParser(
154 getPath("InputModifierOrderAnnotationDeclaration.java"), expected);
155 }
156
157 @Test
158 public void testModifierOrderSealedAndNonSealed() throws Exception {
159 final String[] expected = {
160 "10:8: " + getCheckMessage(MSG_MODIFIER_ORDER, "public"),
161 "26:12: " + getCheckMessage(MSG_MODIFIER_ORDER, "private"),
162 "44:10: " + getCheckMessage(MSG_MODIFIER_ORDER, "sealed"),
163 "50:11: " + getCheckMessage(MSG_MODIFIER_ORDER, "public"),
164 "53:14: " + getCheckMessage(MSG_MODIFIER_ORDER, "static"),
165 "58:10: " + getCheckMessage(MSG_MODIFIER_ORDER, "non-sealed"),
166 };
167 verifyWithInlineConfigParser(
168 getNonCompilablePath("InputModifierOrderSealedAndNonSealed.java"), expected);
169 }
170
171 @Test
172 public void testModifierOrderSealedAndNonSealedNoViolation() throws Exception {
173 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
174 verifyWithInlineConfigParser(
175 getNonCompilablePath("InputModifierOrderSealedAndNonSealedNoViolation.java"),
176 expected);
177 }
178
179 }