View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.jupiter.api.Test;
26  
27  import com.google.checkstyle.test.base.AbstractGoogleModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.api.Configuration;
29  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
30  
31  public class PackageNameTest extends AbstractGoogleModuleTestSupport {
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          final Configuration checkConfig = getModuleConfig("PackageName");
47          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
48  
49          final String filePath = getPath("packagenames", "InputPackageNameGood.java");
50  
51          final Integer[] warnList = getLinesWithWarn(filePath);
52          verify(checkConfig, filePath, expected, warnList);
53      }
54  
55      @Test
56      public void testBadPackageName() throws Exception {
57          final String packagePath =
58                  "com.google.checkstyle.test.chapter5naming.rule521packageNamesCamelCase";
59          final Configuration checkConfig = getModuleConfig("PackageName");
60          final String format = checkConfig.getProperty("format");
61          final String msg = getCheckMessage(checkConfig.getMessages(), MSG_KEY, packagePath, format);
62  
63          final String[] expected = {
64              "1:9: " + msg,
65          };
66  
67          final String filePath = getPath("packageNamesCamelCase", "InputPackageNameBad.java");
68  
69          final Integer[] warnList = getLinesWithWarn(filePath);
70          verify(checkConfig, filePath, expected, warnList);
71      }
72  
73      @Test
74      public void testBadPackageName2() throws Exception {
75          final String packagePath = "com.google.checkstyle.test.chapter5naming.rule521_packagenames";
76          final Configuration checkConfig = getModuleConfig("PackageName");
77          final String format = checkConfig.getProperty("format");
78          final String msg = getCheckMessage(checkConfig.getMessages(), MSG_KEY, packagePath, format);
79  
80          final String[] expected = {
81              "1:9: " + msg,
82          };
83  
84          final String filePath = getPath("_packagenames", "InputBadPackageName2.java");
85  
86          final Integer[] warnList = getLinesWithWarn(filePath);
87          verify(checkConfig, filePath, expected, warnList);
88      }
89  
90      @Test
91      public void testBadPackageName3() throws Exception {
92          final String packagePath = "com.google.checkstyle.test.chapter5naming.rule521$packagenames";
93          final Configuration checkConfig = getModuleConfig("PackageName");
94          final String format = checkConfig.getProperty("format");
95          final String msg = getCheckMessage(checkConfig.getMessages(), MSG_KEY, packagePath, format);
96  
97          final String[] expected = {
98              "1:9: " + msg,
99          };
100 
101         final String filePath = getPath("$packagenames", "InputPackageBadName3.java");
102 
103         final Integer[] warnList = getLinesWithWarn(filePath);
104         verify(checkConfig, filePath, expected, warnList);
105     }
106 
107 }