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 static com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanExpressionCheck.MSG_KEY;
23
24 import java.io.File;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28
29 import org.junit.jupiter.api.Test;
30
31 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32 import com.puppycrawl.tools.checkstyle.checks.coding.SimplifyBooleanExpressionCheck;
33
34 public class XpathRegressionSimplifyBooleanExpressionTest extends AbstractXpathTestSupport {
35
36 @Override
37 protected String getCheckName() {
38 return SimplifyBooleanExpressionCheck.class.getSimpleName();
39 }
40
41 @Test
42 public void testSimple() throws Exception {
43 final String fileName = "InputXpathSimplifyBooleanExpressionSimple.java";
44 final File fileToProcess = new File(getPath(fileName));
45
46 final DefaultConfiguration moduleConfig =
47 createModuleConfig(SimplifyBooleanExpressionCheck.class);
48
49 final String[] expectedViolations = {
50 "8:13: " + getCheckMessage(SimplifyBooleanExpressionCheck.class, MSG_KEY),
51 };
52
53 final List<String> expectedXpathQuery = Arrays.asList(
54 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
55 + "[@text='InputXpathSimplifyBooleanExpressionSimple']]"
56 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_IF/EXPR",
57 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
58 + "[@text='InputXpathSimplifyBooleanExpressionSimple']]"
59 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/LITERAL_IF/EXPR/LNOT"
60 );
61
62 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQuery);
63 }
64
65 @Test
66 public void testAnonymous() throws Exception {
67 final String fileName =
68 "InputXpathSimplifyBooleanExpressionAnonymous.java";
69 final File fileToProcess = new File(getPath(fileName));
70
71 final DefaultConfiguration moduleConfig =
72 createModuleConfig(SimplifyBooleanExpressionCheck.class);
73
74 final String[] expectedViolations = {
75 "8:19: " + getCheckMessage(SimplifyBooleanExpressionCheck.class, MSG_KEY),
76 };
77
78 final List<String> expectedXpathQuery = Arrays.asList(
79 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
80 + "[@text='InputXpathSimplifyBooleanExpressionAnonymous']]"
81 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]/OBJBLOCK/METHOD_DEF"
82 + "[./IDENT[@text='test']]/SLIST/LITERAL_IF/EXPR",
83 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
84 + "[@text='InputXpathSimplifyBooleanExpressionAnonymous']]"
85 + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]/OBJBLOCK/METHOD_DEF"
86 + "[./IDENT[@text='test']]/SLIST/LITERAL_IF/EXPR/EQUAL[./IDENT[@text='a']]"
87 );
88
89 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQuery);
90 }
91
92 @Test
93 public void testInterface() throws Exception {
94 final String fileName =
95 "InputXpathSimplifyBooleanExpressionInterface.java";
96 final File fileToProcess = new File(getPath(fileName));
97
98 final DefaultConfiguration moduleConfig =
99 createModuleConfig(SimplifyBooleanExpressionCheck.class);
100
101 final String[] expectedViolations = {
102 "7:20: " + getCheckMessage(SimplifyBooleanExpressionCheck.class, MSG_KEY),
103 };
104
105 final List<String> expectedXpathQuery = Collections.singletonList(
106 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
107 + "[@text='InputXpathSimplifyBooleanExpressionInterface']]"
108 + "/OBJBLOCK/INTERFACE_DEF[./IDENT[@text='Inner']]/OBJBLOCK/METHOD_DEF[./IDENT"
109 + "[@text='test']]/SLIST/LITERAL_IF/EXPR/LNOT/NOT_EQUAL[./IDENT[@text='b']]"
110 );
111
112 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQuery);
113 }
114 }