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