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.Collections;
24 import java.util.List;
25
26 import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27 import org.junit.jupiter.api.Test;
28
29 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30 import com.puppycrawl.tools.checkstyle.checks.whitespace.NoWhitespaceBeforeCheck;
31
32 public class XpathRegressionNoWhitespaceBeforeTest extends AbstractXpathTestSupport {
33
34 private final String checkName = NoWhitespaceBeforeCheck.class.getSimpleName();
35
36 @Override
37 protected String getCheckName() {
38 return checkName;
39 }
40
41 @Override
42 protected String getPackageLocation() {
43 return "org/checkstyle/suppressionxpathfilter/whitespace/nowhitespacebefore";
44 }
45
46 @Test
47 public void testNoWhitespaceBefore() throws Exception {
48 final File fileToProcess =
49 new File(getPath("InputXpathNoWhitespaceBefore.java"));
50
51 final DefaultConfiguration moduleConfig =
52 createModuleConfig(NoWhitespaceBeforeCheck.class);
53
54 final String[] expectedViolation = {
55 "4:13: " + getCheckMessage(NoWhitespaceBeforeCheck.class,
56 NoWhitespaceBeforeCheck.MSG_KEY, ";"),
57 };
58
59 final List<String> expectedXpathQueries = Collections.singletonList(
60 "/COMPILATION_UNIT/CLASS_DEF"
61 + "[./IDENT[@text='InputXpathNoWhitespaceBefore']]/OBJBLOCK"
62 + "/VARIABLE_DEF[./IDENT[@text='bad']]/SEMI"
63 );
64
65 runVerifications(moduleConfig, fileToProcess, expectedViolation,
66 expectedXpathQueries);
67 }
68
69 @Test
70 public void testTokens() throws Exception {
71 final File fileToProcess =
72 new File(getPath("InputXpathNoWhitespaceBeforeTokens.java"));
73
74 final DefaultConfiguration moduleConfig =
75 createModuleConfig(NoWhitespaceBeforeCheck.class);
76 moduleConfig.addProperty("tokens", "DOT");
77
78 final String[] expectedViolation = {
79 "4:17: " + getCheckMessage(NoWhitespaceBeforeCheck.class,
80 NoWhitespaceBeforeCheck.MSG_KEY, "."),
81 };
82
83 final List<String> expectedXpathQueries = Collections.singletonList(
84 "/COMPILATION_UNIT/CLASS_DEF"
85 + "[./IDENT[@text='InputXpathNoWhitespaceBeforeTokens']]"
86 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
87 + "/TYPE/DOT[./IDENT[@text='String']]"
88 + "/DOT[./IDENT[@text='java']]"
89 );
90
91 runVerifications(moduleConfig, fileToProcess, expectedViolation,
92 expectedXpathQueries);
93 }
94
95 @Test
96 public void testAllowLineBreaks() throws Exception {
97 final File fileToProcess =
98 new File(getPath("InputXpathNoWhitespaceBeforeLineBreaks.java"));
99
100 final DefaultConfiguration moduleConfig =
101 createModuleConfig(NoWhitespaceBeforeCheck.class);
102 moduleConfig.addProperty("allowLineBreaks", "false");
103
104 final String[] expectedViolation = {
105 "6:13: " + getCheckMessage(NoWhitespaceBeforeCheck.class,
106 NoWhitespaceBeforeCheck.MSG_KEY, ","),
107 };
108
109 final List<String> expectedXpathQueries = Collections.singletonList(
110 "/COMPILATION_UNIT/CLASS_DEF"
111 + "[./IDENT[@text='InputXpathNoWhitespaceBeforeLineBreaks']]"
112 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]"
113 + "/SLIST/VARIABLE_DEF[./IDENT[@text='array']]"
114 + "/ASSIGN/ARRAY_INIT/COMMA"
115 );
116
117 runVerifications(moduleConfig, fileToProcess, expectedViolation,
118 expectedXpathQueries);
119 }
120 }