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