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.coding;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.coding.PackageDeclarationCheck.MSG_KEY_MISMATCH;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.PackageDeclarationCheck.MSG_KEY_MISSING;
24  
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
31  
32  public class PackageDeclarationCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/coding/packagedeclaration";
37      }
38  
39      @Test
40      public void testDefaultNoPackage() throws Exception {
41          final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);
42  
43          final String[] expected = {
44              "4: " + getCheckMessage(MSG_KEY_MISSING),
45          };
46  
47          verify(checkConfig,
48                  getNonCompilablePath("InputPackageDeclarationNoPackage.java"), expected);
49      }
50  
51      @Test
52      public void testDefaultWithPackage() throws Exception {
53          final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);
54  
55          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
56  
57          verify(checkConfig, getPath("InputPackageDeclarationPlain.java"), expected);
58      }
59  
60      @Test
61      public void testOnFileWithCommentOnly() throws Exception {
62          final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);
63  
64          final String[] expected = {
65              "1: " + getCheckMessage(MSG_KEY_MISSING),
66          };
67  
68          verify(checkConfig, getPath("InputPackageDeclarationWithCommentOnly.java"), expected);
69      }
70  
71      @Test
72      public void testFileForDiffDirectoryMismatch() throws Exception {
73          final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);
74  
75          final String[] expected = {
76              "1: " + getCheckMessage(MSG_KEY_MISMATCH),
77          };
78  
79          verify(checkConfig, getPath("InputPackageDeclarationDiffDirectory.java"), expected);
80      }
81  
82      @Test
83      public void testFileForDirectoryMismatchAtParent() throws Exception {
84          final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);
85  
86          final String[] expected = {
87              "1: " + getCheckMessage(MSG_KEY_MISMATCH),
88          };
89  
90          verify(checkConfig, getPath("InputPackageDeclarationDiffDirectoryAtParent.java"), expected);
91      }
92  
93      @Test
94      public void testFileForDirectoryMismatchAtSubpackage() throws Exception {
95          final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);
96  
97          final String[] expected = {
98              "1: " + getCheckMessage(MSG_KEY_MISMATCH),
99          };
100 
101         verify(checkConfig,
102                 getPath("InputPackageDeclarationDiffDirectoryAtSubpackage.java"), expected);
103     }
104 
105     @Test
106     public void testFileIgnoreDiffDirectoryMismatch() throws Exception {
107         final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);
108         checkConfig.addAttribute("matchDirectoryStructure", "false");
109         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
110 
111         verify(checkConfig, getPath("InputPackageDeclarationDiffDirectory.java"), expected);
112     }
113 
114     @Test
115     public void testFileIgnoreDirectoryMismatchAtParent() throws Exception {
116         final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);
117         checkConfig.addAttribute("matchDirectoryStructure", "false");
118         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
119 
120         verify(checkConfig, getPath("InputPackageDeclarationDiffDirectoryAtParent.java"), expected);
121     }
122 
123     @Test
124     public void testFileIgnoreDirectoryMismatchAtSubpackage() throws Exception {
125         final DefaultConfiguration checkConfig = createModuleConfig(PackageDeclarationCheck.class);
126         checkConfig.addAttribute("matchDirectoryStructure", "false");
127         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
128 
129         verify(checkConfig,
130                 getPath("InputPackageDeclarationDiffDirectoryAtSubpackage.java"), expected);
131     }
132 
133     @Test
134     public void testTokensNotNull() {
135         final PackageDeclarationCheck check = new PackageDeclarationCheck();
136         Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
137         Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
138         Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
139     }
140 
141 }