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