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.coding;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.coding.PackageDeclarationCheck.MSG_KEY_MISMATCH;
24 import static com.puppycrawl.tools.checkstyle.checks.coding.PackageDeclarationCheck.MSG_KEY_MISSING;
25
26 import java.io.File;
27
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31 import com.puppycrawl.tools.checkstyle.Checker;
32 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
33 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
34 import de.thetaphi.forbiddenapis.SuppressForbidden;
35
36 public class PackageDeclarationCheckTest extends AbstractModuleTestSupport {
37
38 @Override
39 protected String getPackageLocation() {
40 return "com/puppycrawl/tools/checkstyle/checks/coding/packagedeclaration";
41 }
42
43 @Test
44 public void testDefaultNoPackage() throws Exception {
45
46 final String[] expected = {
47 "8:1: " + getCheckMessage(MSG_KEY_MISSING),
48 };
49
50 verifyWithInlineConfigParser(
51 getPath("InputPackageDeclarationNoPackage.java"), expected);
52 }
53
54 @Test
55 public void testDefaultWithPackage() throws Exception {
56
57 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
58
59 verifyWithInlineConfigParser(
60 getPath("InputPackageDeclarationPlain.java"), expected);
61 }
62
63 @Test
64 public void testOnFileWithCommentOnly() throws Exception {
65
66 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
67
68 verifyWithInlineConfigParser(
69 getPath("InputPackageDeclarationWithCommentOnly.java"), expected);
70 }
71
72 @Test
73 public void testFileForDiffDirectoryMismatch() throws Exception {
74
75 final String[] expected = {
76 "8:1: " + getCheckMessage(MSG_KEY_MISMATCH),
77 };
78
79 verifyWithInlineConfigParser(
80 getNonCompilablePath("InputPackageDeclarationDiffDirectory.java"), expected);
81 }
82
83 @Test
84 public void testFileForDirectoryMismatchAtParent() throws Exception {
85
86 final String[] expected = {
87 "8:1: " + getCheckMessage(MSG_KEY_MISMATCH),
88 };
89
90 verifyWithInlineConfigParser(
91 getNonCompilablePath("InputPackageDeclarationDiffDirectoryAtParent.java"),
92 expected);
93 }
94
95 @Test
96 public void testFileForDirectoryMismatchAtSubpackage() throws Exception {
97
98 final String[] expected = {
99 "8:1: " + getCheckMessage(MSG_KEY_MISMATCH),
100 };
101
102 verifyWithInlineConfigParser(
103 getNonCompilablePath("InputPackageDeclarationDiffDirectoryAtSubpackage.java"),
104 expected);
105 }
106
107 @Test
108 public void testFileIgnoreDiffDirectoryMismatch() throws Exception {
109 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
110
111 verifyWithInlineConfigParser(
112 getNonCompilablePath("InputPackageDeclarationDiffDirectory2.java"),
113 expected);
114 }
115
116 @Test
117 public void testFileIgnoreDirectoryMismatchAtParent() throws Exception {
118 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
119
120 verifyWithInlineConfigParser(
121 getNonCompilablePath("InputPackageDeclarationDiffDirectoryAtParent2.java"),
122 expected);
123 }
124
125 @Test
126 public void testFileIgnoreDirectoryMismatchAtSubpackage() throws Exception {
127 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
128
129 verifyWithInlineConfigParser(
130 getNonCompilablePath("InputPackageDeclarationDiffDirectoryAtSubpackage2.java"),
131 expected);
132 }
133
134 @Test
135 public void testNoPackage() throws Exception {
136 final String[] expected = {
137 "9:1: " + getCheckMessage(MSG_KEY_MISSING),
138 };
139
140 verifyWithInlineConfigParser(
141 getNonCompilablePath("InputPackageDeclarationNoPackage.java"),
142 expected);
143 }
144
145 @SuppressForbidden
146 @Test
147 public void testEmptyFile() throws Exception {
148 final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);
149 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
150
151 verify(checkConfig,
152 getNonCompilablePath("InputPackageDeclarationEmptyFile.java"),
153 expected);
154 }
155
156 @Test
157 public void testTokensNotNull() {
158 final PackageDeclarationCheck check = new PackageDeclarationCheck();
159 assertWithMessage("Acceptable tokens should not be null")
160 .that(check.getAcceptableTokens())
161 .isNotNull();
162 assertWithMessage("Default tokens should not be null")
163 .that(check.getDefaultTokens())
164 .isNotNull();
165 assertWithMessage("Required tokens should not be null")
166 .that(check.getRequiredTokens())
167 .isNotNull();
168 }
169
170 @Test
171 public void testBeginTreeClear() throws Exception {
172 final DefaultConfiguration checkConfig =
173 createModuleConfig(PackageDeclarationCheck.class);
174 final String[] expected = {
175 "8:1: " + getCheckMessage(MSG_KEY_MISSING),
176 };
177 final Checker checker = createChecker(checkConfig);
178 final String fileName1 = getPath("InputPackageDeclarationPlain.java");
179 final String fileName2 = getPath("InputPackageDeclarationNoPackage.java");
180 final File[] files = {
181 new File(fileName1),
182 new File(fileName2),
183 };
184 verify(checker, files, fileName2, expected);
185 }
186 }