View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck.MSG_LEGACY_PACKAGE_HTML;
23  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck.MSG_PACKAGE_INFO;
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.fail;
26  import static org.mockito.Mockito.spy;
27  import static org.mockito.Mockito.when;
28  
29  import java.io.File;
30  import java.util.Collections;
31  
32  import org.junit.Test;
33  
34  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
36  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
37  import com.puppycrawl.tools.checkstyle.api.Configuration;
38  import com.puppycrawl.tools.checkstyle.api.FileText;
39  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
40  
41  public class JavadocPackageCheckTest
42      extends AbstractModuleTestSupport {
43  
44      @Override
45      protected String getPackageLocation() {
46          return "com/puppycrawl/tools/checkstyle/checks/javadoc/javadocpackage";
47      }
48  
49      @Test
50      public void testMissing() throws Exception {
51          final Configuration checkConfig = createModuleConfig(JavadocPackageCheck.class);
52          final String[] expected = {
53              "0: " + getCheckMessage(MSG_PACKAGE_INFO),
54          };
55          verify(
56              createChecker(checkConfig),
57              getPath("InputJavadocPackageBadCls.java"),
58              getPath("InputJavadocPackageBadCls.java"),
59              expected);
60      }
61  
62      @Test
63      public void testMissingWithAllowLegacy() throws Exception {
64          final DefaultConfiguration checkConfig = createModuleConfig(JavadocPackageCheck.class);
65          checkConfig.addAttribute("allowLegacy", "true");
66          final String[] expected = {
67              "0: " + getCheckMessage(MSG_PACKAGE_INFO),
68          };
69          verify(
70              createChecker(checkConfig),
71              getPath("InputJavadocPackageBadCls.java"),
72              getPath("InputJavadocPackageBadCls.java"),
73              expected);
74      }
75  
76      @Test
77      public void testWithMultipleFiles() throws Exception {
78          final Configuration checkConfig = createModuleConfig(JavadocPackageCheck.class);
79          final String path1 = getPath("InputJavadocPackageNoJavadoc.java");
80          final String path2 = getPath("InputJavadocPackageBadTag.java");
81          final String[] expected = {
82              "0: " + getCheckMessage(MSG_PACKAGE_INFO),
83          };
84          verify(
85              createChecker(checkConfig),
86              new File[] {new File(path1), new File(path2)},
87              path1,
88              expected);
89      }
90  
91      @Test
92      public void testBoth() throws Exception {
93          final Configuration checkConfig = createModuleConfig(JavadocPackageCheck.class);
94          final String[] expected = {
95              "0: " + getCheckMessage(MSG_LEGACY_PACKAGE_HTML),
96          };
97          verify(createChecker(checkConfig),
98              getPath("bothfiles" + File.separator + "InputJavadocPackageBothIgnored.java"),
99              getPath("bothfiles"
100             + File.separator + "InputJavadocPackageBothIgnored.java"), expected);
101     }
102 
103     @Test
104     public void testHtmlDisallowed() throws Exception {
105         final Configuration checkConfig = createModuleConfig(JavadocPackageCheck.class);
106         final String[] expected = {
107             "0: " + getCheckMessage(MSG_PACKAGE_INFO),
108         };
109         verify(createChecker(checkConfig),
110             getPath("pkghtml" + File.separator + "InputJavadocPackageHtmlIgnored.java"),
111             getPath("pkghtml" + File.separator + "InputJavadocPackageHtmlIgnored.java"), expected);
112     }
113 
114     @Test
115     public void testHtmlAllowed() throws Exception {
116         final DefaultConfiguration checkConfig = createModuleConfig(JavadocPackageCheck.class);
117         checkConfig.addAttribute("allowLegacy", "true");
118         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
119         verify(createChecker(checkConfig),
120             getPath("pkghtml" + File.separator + "InputJavadocPackageHtmlIgnored.java"),
121             getPath("pkghtml" + File.separator + "package-info.java"), expected);
122     }
123 
124     @Test
125     public void testAnnotation() throws Exception {
126         final DefaultConfiguration checkConfig = createModuleConfig(JavadocPackageCheck.class);
127         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
128         verify(createChecker(checkConfig),
129             getPath("annotation"
130                     + File.separator + "package-info.java"),
131             getPath("annotation"
132                     + File.separator + "package-info.java"), expected);
133     }
134 
135     /**
136      * Test require readable file with no parent to be used.
137      * Usage of Mockito.spy() is the only way to satisfy these requirements
138      * without the need to create new file in current working directory.
139      *
140      * @throws Exception if error occurs
141      */
142     @Test
143     public void testWithFileWithoutParent() throws Exception {
144         final DefaultConfiguration moduleConfig = createModuleConfig(JavadocPackageCheck.class);
145         final File fileWithoutParent = spy(new File(getPath("noparentfile"
146                     + File.separator + "package-info.java")));
147         when(fileWithoutParent.getParent()).thenReturn(null);
148         when(fileWithoutParent.getParentFile()).thenReturn(null);
149         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
150         verify(createChecker(moduleConfig),
151                 new File[] {fileWithoutParent},
152                 getPath("annotation"
153                     + File.separator + "package-info.java"), expected);
154     }
155 
156     /**
157      * Using direct call to check here because there is no other way
158      * to reproduce exception with invalid canonical path.
159      */
160     @Test
161     public void testCheckstyleExceptionIfFailedToGetCanonicalPathToFile() {
162         final JavadocPackageCheck check = new JavadocPackageCheck();
163         final File fileWithInvalidPath = new File("\u0000\u0000\u0000");
164         final FileText mockFileText = new FileText(fileWithInvalidPath, Collections.emptyList());
165         final String expectedExceptionMessage =
166                 "Exception while getting canonical path to file " + fileWithInvalidPath.getPath();
167         try {
168             check.processFiltered(fileWithInvalidPath, mockFileText);
169             fail("CheckstyleException expected to be thrown");
170         }
171         catch (CheckstyleException ex) {
172             assertEquals("Invalid exception message. Expected: " + expectedExceptionMessage,
173                     expectedExceptionMessage, ex.getMessage());
174         }
175     }
176 
177 }