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