1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.CommonUtil;
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 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.getAttribute("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.getAttribute("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.getAttribute("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 }