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 static com.puppycrawl.tools.checkstyle.checks.coding.IllegalInstantiationCheck.MSG_KEY;
23
24 import java.io.File;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31 import com.puppycrawl.tools.checkstyle.checks.coding.IllegalInstantiationCheck;
32
33 public class XpathRegressionIllegalInstantiationTest extends AbstractXpathTestSupport {
34 @Override
35 protected String getCheckName() {
36 return IllegalInstantiationCheck.class.getSimpleName();
37 }
38
39 @Test
40 public void testSimple() throws Exception {
41 final String fileName = "InputXpathIllegalInstantiationSimple.java";
42 final File fileToProcess = new File(getNonCompilablePath(fileName));
43
44 final DefaultConfiguration moduleConfig =
45 createModuleConfig(IllegalInstantiationCheck.class);
46 moduleConfig.addProperty("classes", "java.lang.Boolean");
47
48 final String[] expectedViolation = {
49 "8:21: " + getCheckMessage(IllegalInstantiationCheck.class, MSG_KEY,
50 "java.lang.Boolean"),
51 };
52
53 final List<String> expectedXpathQueries = Arrays.asList(
54 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
55 + "[@text='InputXpathIllegalInstantiationSimple']]"
56 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
57 + "VARIABLE_DEF[./IDENT[@text='x']]/ASSIGN/EXPR",
58 "/COMPILATION_UNIT/CLASS_DEF"
59 + "[./IDENT[@text='InputXpathIllegalInstantiationSimple']]"
60 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF"
61 + "[./IDENT[@text='x']]/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='Boolean']]"
62 );
63
64 runVerifications(moduleConfig, fileToProcess, expectedViolation,
65 expectedXpathQueries);
66 }
67
68 @Test
69 public void testAnonymous() throws Exception {
70 final String fileName = "InputXpathIllegalInstantiationAnonymous.java";
71 final File fileToProcess = new File(getNonCompilablePath(fileName));
72
73 final DefaultConfiguration moduleConfig =
74 createModuleConfig(IllegalInstantiationCheck.class);
75 moduleConfig.addProperty("classes", "java.lang.Integer");
76
77 final String[] expectedViolation = {
78 "10:25: " + getCheckMessage(IllegalInstantiationCheck.class, MSG_KEY,
79 "java.lang.Integer"),
80 };
81
82 final List<String> expectedXpathQueries = Arrays.asList(
83 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
84 + "[@text='InputXpathIllegalInstantiationAnonymous']]"
85 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]/OBJBLOCK/METHOD_DEF"
86 + "[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT[@text='e']]/ASSIGN/EXPR",
87 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
88 + "[@text='InputXpathIllegalInstantiationAnonymous']]"
89 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]/OBJBLOCK/METHOD_DEF"
90 + "[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT[@text='e']]"
91 + "/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='Integer']]"
92 );
93
94 runVerifications(moduleConfig, fileToProcess, expectedViolation,
95 expectedXpathQueries);
96 }
97
98 @Test
99 public void testInterface() throws Exception {
100 final String fileName = "InputXpathIllegalInstantiationInterface.java";
101 final File fileToProcess = new File(getNonCompilablePath(fileName));
102
103 final DefaultConfiguration moduleConfig =
104 createModuleConfig(IllegalInstantiationCheck.class);
105 moduleConfig.addProperty("classes", "java.lang.String");
106
107 final String[] expectedViolation = {
108 "10:24: " + getCheckMessage(IllegalInstantiationCheck.class, MSG_KEY,
109 "java.lang.String"),
110 };
111
112 final List<String> expectedXpathQueries = Arrays.asList(
113 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
114 + "[@text='InputXpathIllegalInstantiationInterface']]"
115 + "/OBJBLOCK/INTERFACE_DEF[./IDENT[@text='Inner']]/"
116 + "OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
117 + "VARIABLE_DEF[./IDENT[@text='s']]/ASSIGN/EXPR",
118 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
119 + "[@text='InputXpathIllegalInstantiationInterface']]"
120 + "/OBJBLOCK/INTERFACE_DEF[./IDENT[@text='Inner']]"
121 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF"
122 + "[./IDENT[@text='s']]/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='String']]"
123 );
124
125 runVerifications(moduleConfig, fileToProcess, expectedViolation,
126 expectedXpathQueries);
127 }
128 }