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