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.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26
27 import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31 import com.puppycrawl.tools.checkstyle.checks.metrics.NPathComplexityCheck;
32
33
34 public class XpathRegressionNPathComplexityTest extends AbstractXpathTestSupport {
35
36 private final String checkName = NPathComplexityCheck.class.getSimpleName();
37
38 @Override
39 protected String getCheckName() {
40 return checkName;
41 }
42
43 @Override
44 protected String getPackageLocation() {
45 return "org/checkstyle/suppressionxpathfilter/metrics/npathcomplexity";
46 }
47
48 @Test
49 public void testMethod() throws Exception {
50 final File fileToProcess =
51 new File(getPath("InputXpathNPathComplexityMethod.java"));
52
53 final DefaultConfiguration moduleConfig =
54 createModuleConfig(NPathComplexityCheck.class);
55 moduleConfig.addProperty("max", "0");
56
57 final String[] expectedViolation = {
58 "4:5: " + getCheckMessage(NPathComplexityCheck.class,
59 NPathComplexityCheck.MSG_KEY, 3, 0),
60 };
61
62 final List<String> expectedXpathQueries = Arrays.asList(
63 "/COMPILATION_UNIT/CLASS_DEF"
64 + "[./IDENT[@text='InputXpathNPathComplexityMethod']]/OBJBLOCK"
65 + "/METHOD_DEF[./IDENT[@text='test']]",
66 "/COMPILATION_UNIT/CLASS_DEF"
67 + "[./IDENT[@text='InputXpathNPathComplexityMethod']]/OBJBLOCK"
68 + "/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS",
69 "/COMPILATION_UNIT/CLASS_DEF"
70 + "[./IDENT[@text='InputXpathNPathComplexityMethod']]/OBJBLOCK"
71 + "/METHOD_DEF[./IDENT[@text='test']]/MODIFIERS/LITERAL_PUBLIC"
72 );
73
74 runVerifications(moduleConfig, fileToProcess, expectedViolation,
75 expectedXpathQueries);
76 }
77
78 @Test
79 public void testStaticBlock() throws Exception {
80 final File fileToProcess =
81 new File(getPath("InputXpathNPathComplexityStaticBlock.java"));
82
83 final DefaultConfiguration moduleConfig =
84 createModuleConfig(NPathComplexityCheck.class);
85 moduleConfig.addProperty("max", "0");
86
87 final String[] expectedViolation = {
88 "4:5: " + getCheckMessage(NPathComplexityCheck.class,
89 NPathComplexityCheck.MSG_KEY, 3, 0),
90 };
91
92 final List<String> expectedXpathQueries = Collections.singletonList(
93 "/COMPILATION_UNIT/CLASS_DEF"
94 + "[./IDENT[@text='InputXpathNPathComplexityStaticBlock']]"
95 + "/OBJBLOCK/STATIC_INIT"
96 );
97
98 runVerifications(moduleConfig, fileToProcess, expectedViolation,
99 expectedXpathQueries);
100 }
101 }