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