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.javadoc;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck.MSG_LEGACY_PACKAGE_HTML;
24 import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck.MSG_PACKAGE_INFO;
25
26 import java.io.File;
27 import java.util.Collections;
28 import java.util.List;
29
30 import org.junit.jupiter.api.Test;
31
32 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
33 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
34 import com.puppycrawl.tools.checkstyle.api.FileText;
35 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
36 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
37
38 public class JavadocPackageCheckTest
39 extends AbstractModuleTestSupport {
40
41 @Override
42 public String getPackageLocation() {
43 return "com/puppycrawl/tools/checkstyle/checks/javadoc/javadocpackage";
44 }
45
46 @Test
47 public void testMissing() throws Exception {
48 final String[] expected = {
49 "1: " + getCheckMessage(MSG_PACKAGE_INFO),
50 };
51 verifyWithInlineConfigParser(
52 getPath("InputJavadocPackageBadCls.java"),
53 expected);
54 }
55
56 @Test
57 public void testMissingWithAllowLegacy() throws Exception {
58 final String[] expected = {
59 "1: " + getCheckMessage(MSG_PACKAGE_INFO),
60 };
61 verifyWithInlineConfigParser(
62 getPath("InputJavadocPackageBadCls2.java"),
63 expected);
64 }
65
66 @Test
67 public void testWithMultipleFiles() throws Exception {
68 final String[] expected = {
69 "1: " + getCheckMessage(MSG_PACKAGE_INFO),
70 };
71 verifyWithInlineConfigParser(getPath("InputJavadocPackageNoJavadoc.java"),
72 getPath("InputJavadocPackageBadTag.java"), expected);
73 }
74
75 @Test
76 public void testBoth() throws Exception {
77 final String[] expected = {
78 "1: " + getCheckMessage(MSG_LEGACY_PACKAGE_HTML),
79 };
80 verifyWithInlineConfigParser(
81 getPath("bothfiles" + File.separator + "InputJavadocPackageBothIgnored.java"),
82 expected);
83 }
84
85 @Test
86 public void testHtmlDisallowed() throws Exception {
87 final String[] expected = {
88 "1: " + getCheckMessage(MSG_PACKAGE_INFO),
89 };
90 verifyWithInlineConfigParser(
91 getPath("packagehtml" + File.separator + "InputJavadocPackageHtmlIgnored.java"),
92 expected);
93 }
94
95 @Test
96 public void testHtmlAllowed() throws Exception {
97 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
98 verifyWithInlineConfigParser(
99 getPath("packagehtml" + File.separator + "InputJavadocPackageHtmlIgnored2.java"),
100 getPath("noparentfile" + File.separator + "package-info.java"),
101 expected);
102 }
103
104 @Test
105 public void testAnnotation() throws Exception {
106 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
107 verifyWithInlineConfigParser(
108 getPath("annotation"
109 + File.separator + "package-info.java"), expected);
110 }
111
112
113
114
115
116 @Test
117 public void testCheckstyleExceptionIfFailedToGetCanonicalPathToFile() {
118 final JavadocPackageCheck check = new JavadocPackageCheck();
119 final File fileWithInvalidPath = new File("\u0000\u0000\u0000");
120 final FileText mockFileText = new FileText(fileWithInvalidPath, Collections.emptyList());
121 final String expectedExceptionMessage =
122 "Exception while getting canonical path to file " + fileWithInvalidPath.getPath();
123 final CheckstyleException exc =
124 TestUtil.getExpectedThrowable(
125 CheckstyleException.class, () -> {
126 check.processFiltered(
127 fileWithInvalidPath, mockFileText);
128 });
129 assertWithMessage(
130 "Invalid exception message. Expected: %s",
131 expectedExceptionMessage)
132 .that(exc.getMessage())
133 .isEqualTo(expectedExceptionMessage);
134 }
135
136 @Test
137 public void testNonJava() throws Exception {
138 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
139 verifyWithInlineConfigParser(
140 getPath("InputJavadocPackageNotJava.txt"),
141 expected);
142 }
143
144 @Test
145 public void testWithFileWithoutParent() throws Exception {
146 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
147 verifyWithInlineConfigParser(
148 getPath("annotation" + File.separator + "package-info.java"),
149 expected);
150 }
151
152 @Test
153 public void testModuleInfoFile() throws Exception {
154 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
155 verifyWithInlineConfigParser(
156 getNonCompilablePath("module-info/default/module-info.java"),
157 expected);
158 }
159
160 @Test
161 public void testModuleInfoDoesNotSkipSiblingJavaFile() throws Exception {
162 final List<String> expectedFromModuleInfo = List.of();
163 final List<String> expectedFromSibling = List.of(
164 "1: " + getCheckMessage(MSG_PACKAGE_INFO));
165 verifyWithInlineConfigParser(
166 getNonCompilablePath("module-info/withfile/module-info.java"),
167 getNonCompilablePath(
168 "module-info/withfile/InputJavadocPackageModuleInfoWithFile.java"),
169 expectedFromModuleInfo,
170 expectedFromSibling);
171 }
172
173 }