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