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.design;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.design.InnerTypeLastCheck.MSG_KEY;
24
25 import java.io.File;
26
27 import org.junit.jupiter.api.Test;
28
29 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32
33 public class InnerTypeLastCheckTest extends AbstractModuleTestSupport {
34
35 @Override
36 public String getPackageLocation() {
37 return "com/puppycrawl/tools/checkstyle/checks/design/innertypelast";
38 }
39
40 @Test
41 public void testGetRequiredTokens() {
42 final InnerTypeLastCheck checkObj = new InnerTypeLastCheck();
43 final int[] expected = {
44 TokenTypes.CLASS_DEF,
45 TokenTypes.INTERFACE_DEF,
46 TokenTypes.RECORD_DEF,
47 };
48 assertWithMessage("Default required tokens are invalid")
49 .that(checkObj.getRequiredTokens())
50 .isEqualTo(expected);
51 }
52
53 @Test
54 public void testMembersBeforeInner() throws Exception {
55 final String[] expected = {
56 "51:9: " + getCheckMessage(MSG_KEY),
57 "73:9: " + getCheckMessage(MSG_KEY),
58 "78:9: " + getCheckMessage(MSG_KEY),
59 "88:9: " + getCheckMessage(MSG_KEY),
60 "106:17: " + getCheckMessage(MSG_KEY),
61 };
62 verifyWithInlineConfigParser(
63 getPath("InputInnerTypeLastClass.java"), expected);
64 }
65
66 @Test
67 public void testIfRootClassChecked() throws Exception {
68 final String[] expected = {
69 "20:5: " + getCheckMessage(MSG_KEY),
70 "23:5: " + getCheckMessage(MSG_KEY),
71 };
72 verifyWithInlineConfigParser(
73 getPath("InputInnerTypeLastClassRootClass.java"), expected);
74 }
75
76 @Test
77 public void testIfRootClassChecked2() throws Exception {
78 final String[] expected = {
79 "25:5: " + getCheckMessage(MSG_KEY),
80 };
81 verifyWithInlineConfigParser(
82 getPath("InputInnerTypeLastClassRootClass2.java"), expected);
83 }
84
85 @Test
86 public void testIfRootClassChecked3() throws Exception {
87 final DefaultConfiguration checkConfig =
88 createModuleConfig(InnerTypeLastCheck.class);
89 final String[] expected = {
90 "20:5: " + getCheckMessage(MSG_KEY),
91 "23:5: " + getCheckMessage(MSG_KEY),
92 "20:5: " + getCheckMessage(MSG_KEY),
93 "23:5: " + getCheckMessage(MSG_KEY),
94 };
95 verify(createChecker(checkConfig), new File[] {
96 new File(getPath("InputInnerTypeLastClassRootClass.java")),
97 new File(getPath("InputInnerTypeLastClassRootClass.java")),
98 }, getPath("InputInnerTypeLastClassRootClass.java"), expected);
99 }
100
101 @Test
102 public void testInnerTypeBeforeCtor() throws Exception {
103 final String[] expected = {
104 "14:5: " + getCheckMessage(MSG_KEY),
105 "24:5: " + getCheckMessage(MSG_KEY),
106 "34:5: " + getCheckMessage(MSG_KEY),
107 };
108 verifyWithInlineConfigParser(
109 getPath("InputInnerTypeLastClassCtorsInitBlocks.java"), expected);
110 }
111
112 @Test
113 public void testInnerTypeLastRecords() throws Exception {
114
115 final String[] expected = {
116 "18:9: " + getCheckMessage(MSG_KEY),
117 "23:5: " + getCheckMessage(MSG_KEY),
118 "33:9: " + getCheckMessage(MSG_KEY),
119 "44:13: " + getCheckMessage(MSG_KEY),
120 "51:13: " + getCheckMessage(MSG_KEY),
121 "56:9: " + getCheckMessage(MSG_KEY),
122 "59:9: " + getCheckMessage(MSG_KEY),
123 };
124 verifyWithInlineConfigParser(
125 getPath("InputInnerTypeLastRecords.java"), expected);
126 }
127
128 @Test
129 public void testInnerTypeLastCstyleArray() throws Exception {
130 final String[] expected = {
131 "12:5: " + getCheckMessage(MSG_KEY),
132 "14:5: " + getCheckMessage(MSG_KEY),
133 "16:5: " + getCheckMessage(MSG_KEY),
134 "18:5: " + getCheckMessage(MSG_KEY),
135 };
136 verifyWithInlineConfigParser(
137 getPath("InputInnerTypeLastArray.java"), expected);
138 }
139
140 @Test
141 public void testGetAcceptableTokens() {
142 final InnerTypeLastCheck obj = new InnerTypeLastCheck();
143 final int[] expected = {
144 TokenTypes.CLASS_DEF,
145 TokenTypes.INTERFACE_DEF,
146 TokenTypes.RECORD_DEF,
147 };
148 assertWithMessage("Default acceptable tokens are invalid")
149 .that(obj.getAcceptableTokens())
150 .isEqualTo(expected);
151 }
152
153 @Test
154 public void testInnerTypeLastCompactSourceFile() throws Exception {
155 final String[] expected = {
156 "12:1: " + getCheckMessage(MSG_KEY),
157 };
158 verifyWithInlineConfigParser(
159 getNonCompilablePath("compact/InputInnerTypeLastCompactSourceFile.java"), expected);
160 }
161
162 @Test
163 public void testInnerTypeLastCompactSourceFileNested() throws Exception {
164 final String[] expected = {
165 "15:5: " + getCheckMessage(MSG_KEY),
166 };
167 verifyWithInlineConfigParser(
168 getNonCompilablePath("compact/InputInnerTypeLastCompactSourceFileNested.java"),
169 expected);
170 }
171
172 }