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.ClassFanOutComplexityCheck;
30
31 public class XpathRegressionClassFanOutComplexityTest extends AbstractXpathTestSupport {
32
33 private final String checkName = ClassFanOutComplexityCheck.class.getSimpleName();
34
35 @Override
36 protected String getCheckName() {
37 return checkName;
38 }
39
40 @Override
41 protected String getPackageLocation() {
42 return "org/checkstyle/suppressionxpathfilter/metrics/classfanoutcomplexity";
43 }
44
45 @Test
46 public void testInClass() throws Exception {
47 final File fileToProcess = new File(
48 getPath("InputXpathClassFanOutComplexityClass.java")
49 );
50 final DefaultConfiguration moduleConfig =
51 createModuleConfig(ClassFanOutComplexityCheck.class);
52 moduleConfig.addProperty("max", "0");
53
54 final String[] expectedViolation = {
55 "6:1: " + getCheckMessage(ClassFanOutComplexityCheck.class,
56 ClassFanOutComplexityCheck.MSG_KEY, 4, 0),
57 };
58
59 final List<String> expectedXpathQueries = List.of(
60 "/COMPILATION_UNIT/CLASS_DEF"
61 + "[./IDENT[@text='InputXpathClassFanOutComplexityClass']]",
62 "/COMPILATION_UNIT/CLASS_DEF"
63 + "[./IDENT[@text='InputXpathClassFanOutComplexityClass']]/MODIFIERS",
64 "/COMPILATION_UNIT/CLASS_DEF"
65 + "[./IDENT[@text='InputXpathClassFanOutComplexityClass']]/MODIFIERS/LITERAL_PUBLIC"
66
67 );
68 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
69 }
70
71 @Test
72 public void testInInterface() throws Exception {
73 final File fileToProcess = new File(
74 getPath("InputXpathClassFanOutComplexityInterface.java")
75 );
76 final DefaultConfiguration moduleConfig =
77 createModuleConfig(ClassFanOutComplexityCheck.class);
78 moduleConfig.addProperty("max", "0");
79
80 final String[] expectedViolation = {
81 "7:1: " + getCheckMessage(ClassFanOutComplexityCheck.class,
82 ClassFanOutComplexityCheck.MSG_KEY, 1, 0),
83 };
84
85 final List<String> expectedXpathQueries = List.of(
86 "/COMPILATION_UNIT/INTERFACE_DEF"
87 + "[./IDENT[@text='BadInterface']]",
88 "/COMPILATION_UNIT/INTERFACE_DEF"
89 + "[./IDENT[@text='BadInterface']]/MODIFIERS",
90 "/COMPILATION_UNIT/INTERFACE_DEF"
91 + "[./IDENT[@text='BadInterface']]/LITERAL_INTERFACE"
92 );
93 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
94 }
95
96 @Test
97 public void testInEnum() throws Exception {
98 final File fileToProcess = new File(
99 getPath("InputXpathClassFanOutComplexityEnum.java")
100 );
101 final DefaultConfiguration moduleConfig =
102 createModuleConfig(ClassFanOutComplexityCheck.class);
103 moduleConfig.addProperty("max", "0");
104
105 final String[] expectedViolation = {
106 "9:1: " + getCheckMessage(ClassFanOutComplexityCheck.class,
107 ClassFanOutComplexityCheck.MSG_KEY, 1, 0),
108 };
109
110 final List<String> expectedXpathQueries = List.of(
111 "/COMPILATION_UNIT/ENUM_DEF"
112 + "[./IDENT[@text='MyEnum']]",
113 "/COMPILATION_UNIT/ENUM_DEF"
114 + "[./IDENT[@text='MyEnum']]/MODIFIERS",
115 "/COMPILATION_UNIT/ENUM_DEF"
116 + "[./IDENT[@text='MyEnum']]/ENUM"
117 );
118 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
119 }
120 }