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 java.io.File;
23 import java.util.List;
24
25 import org.junit.jupiter.api.Test;
26
27 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
28 import com.puppycrawl.tools.checkstyle.checks.metrics.JavaNCSSCheck;
29
30
31 public class XpathRegressionJavaNCSSTest extends AbstractXpathTestSupport {
32
33 private final String checkName = JavaNCSSCheck.class.getSimpleName();
34
35 @Override
36 protected String getCheckName() {
37 return checkName;
38 }
39
40 @Test
41 public void testOne() throws Exception {
42 final File fileToProcess =
43 new File(getPath("InputXpathJavaNCSSOne.java"));
44
45 final DefaultConfiguration moduleConfig =
46 createModuleConfig(JavaNCSSCheck.class);
47
48 final String[] expectedViolation = {
49 "5:5: " + getCheckMessage(JavaNCSSCheck.class,
50 JavaNCSSCheck.MSG_METHOD, 51, 50),
51 };
52
53 final List<String> expectedXpathQueries = List.of(
54 "/COMPILATION_UNIT/CLASS_DEF"
55 + "[./IDENT[@text='InputXpathJavaNCSSOne']]"
56 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]",
57
58 "/COMPILATION_UNIT/CLASS_DEF"
59 + "[./IDENT[@text='InputXpathJavaNCSSOne']]"
60 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS",
61
62 "/COMPILATION_UNIT/CLASS_DEF"
63 + "[./IDENT[@text='InputXpathJavaNCSSOne']]"
64 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS/LITERAL_PUBLIC"
65 );
66
67 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
68 }
69
70 @Test
71 public void testTwo() throws Exception {
72 final File fileToProcess =
73 new File(getPath("InputXpathJavaNCSSTwo.java"));
74
75 final DefaultConfiguration moduleConfig =
76 createModuleConfig(JavaNCSSCheck.class);
77
78 moduleConfig.addProperty("classMaximum", "50");
79
80 final String[] expectedViolation = {
81 "3:1: " + getCheckMessage(JavaNCSSCheck.class,
82 JavaNCSSCheck.MSG_CLASS, 51, 50),
83 };
84
85 final List<String> expectedXpathQueries = List.of(
86 "/COMPILATION_UNIT/CLASS_DEF"
87 + "[./IDENT[@text='InputXpathJavaNCSSTwo']]",
88
89 "/COMPILATION_UNIT/CLASS_DEF"
90 + "[./IDENT[@text='InputXpathJavaNCSSTwo']]/MODIFIERS",
91
92 "/COMPILATION_UNIT/CLASS_DEF"
93 + "[./IDENT[@text='InputXpathJavaNCSSTwo']]"
94 + "/MODIFIERS/LITERAL_PUBLIC"
95 );
96
97 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
98 }
99
100 @Test
101 public void testThree() throws Exception {
102 final File fileToProcess =
103 new File(getPath("InputXpathJavaNCSSThree.java"));
104
105 final DefaultConfiguration moduleConfig =
106 createModuleConfig(JavaNCSSCheck.class);
107
108 moduleConfig.addProperty("fileMaximum", "50");
109
110 final String[] expectedViolation = {
111 "1:1: " + getCheckMessage(JavaNCSSCheck.class,
112 JavaNCSSCheck.MSG_FILE, 51, 50),
113 };
114
115 final List<String> expectedXpathQueries = List.of(
116 "/COMPILATION_UNIT",
117 "/COMPILATION_UNIT/PACKAGE_DEF"
118 );
119
120 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
121 }
122 }