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.List;
25
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29 import com.puppycrawl.tools.checkstyle.checks.design.MutableExceptionCheck;
30
31 public class XpathRegressionMutableExceptionTest extends AbstractXpathTestSupport {
32
33 private final String checkName = MutableExceptionCheck.class.getSimpleName();
34
35 @Override
36 protected String getCheckName() {
37 return checkName;
38 }
39
40 @Test
41 public void testDefault() throws Exception {
42 final File fileToProcess =
43 new File(getPath("InputXpathMutableExceptionDefault.java"));
44
45 final DefaultConfiguration moduleConfig =
46 createModuleConfig(MutableExceptionCheck.class);
47
48 final String[] expectedViolation = {
49 "5:9: " + getCheckMessage(MutableExceptionCheck.class,
50 MutableExceptionCheck.MSG_KEY, "code"),
51 };
52
53 final List<String> expectedXpathQueries = Arrays.asList(
54 "/COMPILATION_UNIT/CLASS_DEF"
55 + "[./IDENT[@text='InputXpathMutableExceptionDefault']]"
56 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='FooException']]"
57 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]",
58 "/COMPILATION_UNIT/CLASS_DEF"
59 + "[./IDENT[@text='InputXpathMutableExceptionDefault']]"
60 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='FooException']]"
61 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]/MODIFIERS",
62 "/COMPILATION_UNIT/CLASS_DEF"
63 + "[./IDENT[@text='InputXpathMutableExceptionDefault']]"
64 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='FooException']]"
65 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]/MODIFIERS/LITERAL_PRIVATE"
66 );
67
68 runVerifications(moduleConfig, fileToProcess, expectedViolation,
69 expectedXpathQueries);
70 }
71
72 @Test
73 public void testClassName() throws Exception {
74 final String classFormat = "^.*ExceptionClassName$";
75 final File fileToProcess =
76 new File(getPath("InputXpathMutableExceptionClassName.java"));
77
78 final DefaultConfiguration moduleConfig =
79 createModuleConfig(MutableExceptionCheck.class);
80 moduleConfig.addProperty("format", classFormat);
81
82 final String[] expectedViolation = {
83 "4:3: " + getCheckMessage(MutableExceptionCheck.class,
84 MutableExceptionCheck.MSG_KEY, "code"),
85 };
86
87 final List<String> expectedXpathQueries = Arrays.asList(
88 "/COMPILATION_UNIT/CLASS_DEF"
89 + "[./IDENT[@text='InputXpathMutableExceptionClassName']]"
90 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]",
91 "/COMPILATION_UNIT/CLASS_DEF"
92 + "[./IDENT[@text='InputXpathMutableExceptionClassName']]"
93 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]/MODIFIERS",
94 "/COMPILATION_UNIT/CLASS_DEF"
95 + "[./IDENT[@text='InputXpathMutableExceptionClassName']]"
96 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]/TYPE",
97 "/COMPILATION_UNIT/CLASS_DEF"
98 + "[./IDENT[@text='InputXpathMutableExceptionClassName']]"
99 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]/TYPE/LITERAL_INT"
100 );
101
102 runVerifications(moduleConfig, fileToProcess, expectedViolation,
103 expectedXpathQueries);
104 }
105
106 @Test
107 public void testExtendedClassName() throws Exception {
108 final String extendedClassNameFormat = "^.*Throwable$";
109 final File fileToProcess =
110 new File(getPath("InputXpathMutableExceptionExtendedClassName.java"));
111
112 final DefaultConfiguration moduleConfig =
113 createModuleConfig(MutableExceptionCheck.class);
114 moduleConfig.addProperty("extendedClassNameFormat", extendedClassNameFormat);
115
116 final String[] expectedViolation = {
117 "6:9: " + getCheckMessage(MutableExceptionCheck.class,
118 MutableExceptionCheck.MSG_KEY, "code"),
119 };
120
121 final List<String> expectedXpathQueries = Arrays.asList(
122 "/COMPILATION_UNIT/CLASS_DEF"
123 + "[./IDENT[@text='InputXpathMutableExceptionExtendedClassName']]"
124 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='FooException']]"
125 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]",
126 "/COMPILATION_UNIT/CLASS_DEF"
127 + "[./IDENT[@text='InputXpathMutableExceptionExtendedClassName']]"
128 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='FooException']]"
129 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]/MODIFIERS",
130 "/COMPILATION_UNIT/CLASS_DEF"
131 + "[./IDENT[@text='InputXpathMutableExceptionExtendedClassName']]"
132 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='FooException']]"
133 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='code']]/MODIFIERS/LITERAL_PRIVATE");
134
135 runVerifications(moduleConfig, fileToProcess, expectedViolation,
136 expectedXpathQueries);
137 }
138 }