1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.checkstyle.suppressionxpathfilter;
21
22 import static com.puppycrawl.tools.checkstyle.checks.naming.PackageNameCheck.MSG_KEY;
23
24 import java.io.File;
25 import java.util.Collections;
26 import java.util.List;
27
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31 import com.puppycrawl.tools.checkstyle.checks.naming.PackageNameCheck;
32
33 public class XpathRegressionPackageNameTest extends AbstractXpathTestSupport {
34
35 private final String checkName = PackageNameCheck.class.getSimpleName();
36
37 @Override
38 protected String getCheckName() {
39 return checkName;
40 }
41
42 @Test
43 public void testOne() throws Exception {
44
45 final File fileToProcess =
46 new File(getPath("InputXpathPackageNameOne.java"));
47
48 final String pattern = "[A-Z]+";
49
50 final DefaultConfiguration moduleConfig = createModuleConfig(PackageNameCheck.class);
51 moduleConfig.addProperty("format", pattern);
52
53 final String[] expectedViolation = {
54 "1:9: " + getCheckMessage(PackageNameCheck.class, MSG_KEY,
55 "org.checkstyle.suppressionxpathfilter.packagename",
56 pattern),
57 };
58
59 final List<String> expectedXpathQueries = Collections.singletonList(
60 "/COMPILATION_UNIT/PACKAGE_DEF/DOT"
61 + "[./IDENT[@text='packagename']]/DOT"
62 + "[./IDENT[@text='suppressionxpathfilter']]"
63 + "/DOT/IDENT[@text='org']"
64 );
65
66 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
67
68 }
69
70 @Test
71 public void testThree() throws Exception {
72
73 final File fileToProcess =
74 new File(getNonCompilablePath("InputXpathPackageNameThree.java"));
75
76 final String pattern = "^[a-z]+(\\.[a-z][a-z0-9]*)*$";
77
78 final DefaultConfiguration moduleConfig = createModuleConfig(PackageNameCheck.class);
79 moduleConfig.addProperty("format", pattern);
80 final String[] expectedViolations = {
81 "1:9: " + getCheckMessage(PackageNameCheck.class, MSG_KEY,
82 "org.checkstyle.suppressionxpathfilter.PACKAGENAME",
83 pattern),
84 };
85
86 final List<String> expectedXpathQueries = Collections.singletonList(
87 "/COMPILATION_UNIT/PACKAGE_DEF/DOT[./IDENT"
88 + "[@text='PACKAGENAME']]/DOT[./IDENT"
89 + "[@text='suppressionxpathfilter']]"
90 + "/DOT/IDENT[@text='org']"
91 );
92
93 runVerifications(moduleConfig, fileToProcess, expectedViolations,
94 expectedXpathQueries);
95 }
96
97 @Test
98 public void testTwo() throws Exception {
99
100 final File fileToProcess =
101 new File(getPath("InputXpathPackageNameTwo.java"));
102
103 final String pattern = "[A-Z]+";
104
105 final DefaultConfiguration moduleConfig = createModuleConfig(PackageNameCheck.class);
106 moduleConfig.addProperty("format", pattern);
107
108 final String[] expectedViolation = {
109 "2:9: " + getCheckMessage(PackageNameCheck.class, MSG_KEY,
110 "org.checkstyle.suppressionxpathfilter.packagename",
111 pattern),
112 };
113
114 final List<String> expectedXpathQueries = Collections.singletonList(
115 "/COMPILATION_UNIT/PACKAGE_DEF/DOT"
116 + "[./IDENT[@text='packagename']]/DOT"
117 + "[./IDENT[@text='suppressionxpathfilter']]"
118 + "/DOT/IDENT[@text='org']"
119 );
120
121 runVerifications(moduleConfig, fileToProcess, expectedViolation,
122 expectedXpathQueries);
123 }
124 }