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.google.checkstyle.test.chapter5naming.rule521packagenames;
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import org.junit.Test;
26  
27  import com.google.checkstyle.test.base.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.Configuration;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
30  
31  public class PackageNameTest extends AbstractModuleTestSupport {
32  
33      private static final String MSG_KEY = "name.invalidPattern";
34  
35      @Override
36      protected String getPackageLocation() {
37          return "com/google/checkstyle/test/chapter5naming";
38      }
39  
40      private String getPath(String packageName, String fileName) throws IOException {
41          return getPath("rule521" + packageName + File.separator + fileName);
42      }
43  
44      @Test
45      public void testGoodPackageName() throws Exception {
46  
47          final Configuration checkConfig = getModuleConfig("PackageName");
48          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
49  
50          final String filePath = getPath("packagenames", "InputPackageNameGood.java");
51  
52          final Integer[] warnList = getLinesWithWarn(filePath);
53          verify(checkConfig, filePath, expected, warnList);
54      }
55  
56      @Test
57      public void testBadPackageName() throws Exception {
58  
59          final String packagePath =
60                  "com.google.checkstyle.test.chapter5naming.rule521packageNamesCamelCase";
61          final Configuration checkConfig = getModuleConfig("PackageName");
62          final String format = checkConfig.getAttribute("format");
63          final String msg = getCheckMessage(checkConfig.getMessages(), MSG_KEY, packagePath, format);
64  
65          final String[] expected = {
66              "1:9: " + msg,
67          };
68  
69          final String filePath = getPath("packageNamesCamelCase", "InputPackageNameBad.java");
70  
71          final Integer[] warnList = getLinesWithWarn(filePath);
72          verify(checkConfig, filePath, expected, warnList);
73      }
74  
75      @Test
76      public void testBadPackageName2() throws Exception {
77  
78          final String packagePath = "com.google.checkstyle.test.chapter5naming.rule521_packagenames";
79          final Configuration checkConfig = getModuleConfig("PackageName");
80          final String format = checkConfig.getAttribute("format");
81          final String msg = getCheckMessage(checkConfig.getMessages(), MSG_KEY, packagePath, format);
82  
83          final String[] expected = {
84              "1:9: " + msg,
85          };
86  
87          final String filePath = getPath("_packagenames", "InputBadPackageName2.java");
88  
89          final Integer[] warnList = getLinesWithWarn(filePath);
90          verify(checkConfig, filePath, expected, warnList);
91      }
92  
93      @Test
94      public void testBadPackageName3() throws Exception {
95  
96          final String packagePath = "com.google.checkstyle.test.chapter5naming.rule521$packagenames";
97          final Configuration checkConfig = getModuleConfig("PackageName");
98          final String format = checkConfig.getAttribute("format");
99          final String msg = getCheckMessage(checkConfig.getMessages(), MSG_KEY, packagePath, format);
100 
101         final String[] expected = {
102             "1:9: " + msg,
103         };
104 
105         final String filePath = getPath("$packagenames", "InputPackageBadName3.java");
106 
107         final Integer[] warnList = getLinesWithWarn(filePath);
108         verify(checkConfig, filePath, expected, warnList);
109     }
110 }