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