View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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      * Using direct call to check here because there is no other way
112      * to reproduce exception with invalid canonical path.
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 }