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