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