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.Collections;
25 import java.util.List;
26
27 import org.junit.jupiter.api.Test;
28
29 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30 import com.puppycrawl.tools.checkstyle.checks.coding.IllegalTokenTextCheck;
31
32 public class XpathRegressionIllegalTokenTextTest extends AbstractXpathTestSupport {
33
34 private final String checkName = IllegalTokenTextCheck.class.getSimpleName();
35
36 @Override
37 protected String getCheckName() {
38 return checkName;
39 }
40
41 @Test
42 public void testField() throws Exception {
43 final File fileToProcess =
44 new File(getPath("InputXpathIllegalTokenTextField.java"));
45 final DefaultConfiguration moduleConfig =
46 createModuleConfig(IllegalTokenTextCheck.class);
47 moduleConfig.addProperty("format", "12345");
48 moduleConfig.addProperty("tokens", "NUM_INT");
49 final String[] expectedViolation = {
50 "4:33: " + getCheckMessage(IllegalTokenTextCheck.class,
51 IllegalTokenTextCheck.MSG_KEY, "12345"),
52 };
53 final List<String> expectedXpathQueries = Arrays.asList(
54 "/COMPILATION_UNIT"
55 + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenTextField']]"
56 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='illegalNumber']]"
57 + "/ASSIGN/EXPR[./NUM_INT[@text='12345']]",
58 "/COMPILATION_UNIT"
59 + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenTextField']]"
60 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='illegalNumber']]"
61 + "/ASSIGN/EXPR/NUM_INT[@text='12345']"
62 );
63
64 runVerifications(moduleConfig, fileToProcess, expectedViolation,
65 expectedXpathQueries);
66 }
67
68 @Test
69 public void testMethod() throws Exception {
70 final File fileToProcess =
71 new File(getPath("InputXpathIllegalTokenTextMethod.java"));
72 final DefaultConfiguration moduleConfig =
73 createModuleConfig(IllegalTokenTextCheck.class);
74 moduleConfig.addProperty("format", "forbiddenText");
75 moduleConfig.addProperty("tokens", "STRING_LITERAL");
76 final String[] expectedViolation = {
77 "5:32: " + getCheckMessage(IllegalTokenTextCheck.class,
78 IllegalTokenTextCheck.MSG_KEY, "forbiddenText"),
79 };
80 final List<String> expectedXpathQueries = Arrays.asList(
81 "/COMPILATION_UNIT"
82 + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenTextMethod']]"
83 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myMethod']]"
84 + "/SLIST/VARIABLE_DEF[./IDENT[@text='illegalString']]"
85 + "/ASSIGN/EXPR[./STRING_LITERAL[@text='forbiddenText']]",
86 "/COMPILATION_UNIT"
87 + "/CLASS_DEF[./IDENT[@text='InputXpathIllegalTokenTextMethod']]"
88 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='myMethod']]"
89 + "/SLIST/VARIABLE_DEF[./IDENT[@text='illegalString']]"
90 + "/ASSIGN/EXPR/STRING_LITERAL[@text='forbiddenText']"
91 );
92
93 runVerifications(moduleConfig, fileToProcess, expectedViolation,
94 expectedXpathQueries);
95 }
96
97 @Test
98 public void testInterface() throws Exception {
99 final File fileToProcess =
100 new File(getPath("InputXpathIllegalTokenTextInterface.java"));
101 final DefaultConfiguration moduleConfig =
102 createModuleConfig(IllegalTokenTextCheck.class);
103 moduleConfig.addProperty("format", "invalidIdentifier");
104 moduleConfig.addProperty("tokens", "IDENT");
105 final String[] expectedViolation = {
106 "4:10: " + getCheckMessage(IllegalTokenTextCheck.class,
107 IllegalTokenTextCheck.MSG_KEY, "invalidIdentifier"),
108 };
109 final List<String> expectedXpathQueries = Collections.singletonList(
110 "/COMPILATION_UNIT"
111 + "/INTERFACE_DEF[./IDENT[@text='InputXpathIllegalTokenTextInterface']]"
112 + "/OBJBLOCK/METHOD_DEF/IDENT[@text='invalidIdentifier']"
113 );
114
115 runVerifications(moduleConfig, fileToProcess, expectedViolation,
116 expectedXpathQueries);
117 }
118
119 }