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 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.coding.AvoidInlineConditionalsCheck;
32
33 public class XpathRegressionAvoidInlineConditionalsTest extends AbstractXpathTestSupport {
34
35 @Override
36 protected String getCheckName() {
37 return AvoidInlineConditionalsCheck.class.getSimpleName();
38 }
39
40 @Override
41 protected String getPackageLocation() {
42 return "org/checkstyle/suppressionxpathfilter/coding/avoidinlineconditionals";
43 }
44
45 @Test
46 public void testInlineConditionalsVariableDef() throws Exception {
47 final File fileToProcess = new File(
48 getPath("InputXpathAvoidInlineConditionalsVariableDef.java"));
49
50 final DefaultConfiguration moduleConfig =
51 createModuleConfig(AvoidInlineConditionalsCheck.class);
52
53 final String[] expectedViolation = {
54 "5:50: " + getCheckMessage(AvoidInlineConditionalsCheck.class,
55 AvoidInlineConditionalsCheck.MSG_KEY),
56 };
57
58 final List<String> expectedXpathQueries = Arrays.asList(
59 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
60 + "InputXpathAvoidInlineConditionalsVariableDef']]"
61 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='substring']]/SLIST"
62 + "/VARIABLE_DEF[./IDENT[@text='b']]/ASSIGN/EXPR",
63 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
64 + "InputXpathAvoidInlineConditionalsVariableDef']]"
65 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='substring']]/SLIST"
66 + "/VARIABLE_DEF[./IDENT[@text='b']]/ASSIGN/EXPR/QUESTION"
67 );
68
69 runVerifications(moduleConfig, fileToProcess, expectedViolation,
70 expectedXpathQueries);
71 }
72
73 @Test
74 public void testInlineConditionalsAssign() throws Exception {
75 final File fileToProcess = new File(
76 getPath("InputXpathAvoidInlineConditionalsAssign.java"));
77
78 final DefaultConfiguration moduleConfig =
79 createModuleConfig(AvoidInlineConditionalsCheck.class);
80
81 final String[] expectedViolation = {
82 "7:43: " + getCheckMessage(AvoidInlineConditionalsCheck.class,
83 AvoidInlineConditionalsCheck.MSG_KEY),
84 };
85
86 final List<String> expectedXpathQueries = Collections.singletonList(
87 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
88 + "InputXpathAvoidInlineConditionalsAssign']]"
89 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='setB']]/SLIST"
90 + "/EXPR/ASSIGN[./IDENT[@text='b']]/QUESTION"
91 );
92
93 runVerifications(moduleConfig, fileToProcess, expectedViolation,
94 expectedXpathQueries);
95 }
96
97 @Test
98 public void testInlineConditionalsAssert() throws Exception {
99 final File fileToProcess = new File(
100 getPath("InputXpathAvoidInlineConditionalsAssert.java"));
101
102 final DefaultConfiguration moduleConfig =
103 createModuleConfig(AvoidInlineConditionalsCheck.class);
104
105 final String[] expectedViolation = {
106 "8:31: " + getCheckMessage(AvoidInlineConditionalsCheck.class,
107 AvoidInlineConditionalsCheck.MSG_KEY),
108 };
109
110 final List<String> expectedXpathQueries = Arrays.asList(
111 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
112 + "InputXpathAvoidInlineConditionalsAssert']]"
113 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='assertA']]/SLIST"
114 + "/LITERAL_ASSERT/EXPR[./QUESTION/METHOD_CALL/DOT/IDENT[@text='a']]",
115 "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
116 + "InputXpathAvoidInlineConditionalsAssert']]"
117 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='assertA']]/SLIST"
118 + "/LITERAL_ASSERT/EXPR/QUESTION"
119 );
120
121 runVerifications(moduleConfig, fileToProcess, expectedViolation,
122 expectedXpathQueries);
123 }
124
125 }