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.List;
25
26 import org.junit.jupiter.api.Test;
27
28 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29 import com.puppycrawl.tools.checkstyle.checks.coding.UnusedLambdaParameterShouldBeUnnamedCheck;
30
31 public class XpathRegressionUnusedLambdaParameterShouldBeUnnamedTest
32 extends AbstractXpathTestSupport {
33
34 private final String checkName = UnusedLambdaParameterShouldBeUnnamedCheck.class
35 .getSimpleName();
36
37 @Override
38 protected String getCheckName() {
39 return checkName;
40 }
41
42 @Test
43 public void testSimple() throws Exception {
44 final File fileToProcess =
45 new File(getNonCompilablePath(
46 "InputXpathUnusedLambdaParameterShouldBeUnnamedSimple.java"));
47
48 final DefaultConfiguration moduleConfig =
49 createModuleConfig(UnusedLambdaParameterShouldBeUnnamedCheck.class);
50
51 final String[] expectedViolation = {
52 "9:41: " + getCheckMessage(UnusedLambdaParameterShouldBeUnnamedCheck.class,
53 UnusedLambdaParameterShouldBeUnnamedCheck.MSG_UNUSED_LAMBDA_PARAMETER,
54 "x"),
55 };
56
57 final List<String> expectedXpathQueries = Arrays.asList(
58 "/COMPILATION_UNIT/CLASS_DEF["
59 + "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
60 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT["
61 + "@text='f']]/ASSIGN/LAMBDA/PARAMETERS",
62 "/COMPILATION_UNIT/CLASS_DEF["
63 + "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
64 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF["
65 + "./IDENT[@text='f']]/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF[./IDENT[@text='x']]",
66 "/COMPILATION_UNIT/CLASS_DEF["
67 + "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
68 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
69 + "[@text='f']]/ASSIGN/LAMBDA/PARAMETERS"
70 + "/PARAMETER_DEF[./IDENT[@text='x']]/MODIFIERS",
71 "/COMPILATION_UNIT/CLASS_DEF["
72 + "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
73 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
74 + "[@text='f']]/ASSIGN/LAMBDA/PARAMETERS/"
75 + "PARAMETER_DEF[./IDENT[@text='x']]/TYPE",
76 "/COMPILATION_UNIT/CLASS_DEF["
77 + "./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedSimple']]"
78 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
79 + "[@text='f']]/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF/IDENT[@text='x']"
80 );
81
82 runVerifications(moduleConfig, fileToProcess, expectedViolation,
83 expectedXpathQueries);
84 }
85
86 @Test
87 public void testNested() throws Exception {
88 final File fileToProcess =
89 new File(getNonCompilablePath(
90 "InputXpathUnusedLambdaParameterShouldBeUnnamedNested.java"));
91
92 final DefaultConfiguration moduleConfig =
93 createModuleConfig(UnusedLambdaParameterShouldBeUnnamedCheck.class);
94
95 final String[] expectedViolation = {
96 "10:45: " + getCheckMessage(UnusedLambdaParameterShouldBeUnnamedCheck.class,
97 UnusedLambdaParameterShouldBeUnnamedCheck.MSG_UNUSED_LAMBDA_PARAMETER,
98 "y"),
99 };
100
101 final List<String> expectedXpathQueries = Arrays.asList(
102 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
103 + "[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
104 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
105 + "[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF[./IDENT[@text='f']]"
106 + "/ASSIGN/LAMBDA/PARAMETERS",
107 "/COMPILATION_UNIT/CLASS_DEF"
108 + "[./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
109 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
110 + "[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF[./IDENT[@text='f']]"
111 + "/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF[./IDENT[@text='y']]",
112 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
113 + "[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
114 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
115 + "[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF[./IDENT[@text='f']]"
116 + "/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF[./IDENT[@text='y']]/MODIFIERS",
117 "/COMPILATION_UNIT/CLASS_DEF"
118 + "[./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
119 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT"
120 + "[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF[./IDENT[@text='f']]/ASSIGN"
121 + "/LAMBDA/PARAMETERS/PARAMETER_DEF[./IDENT[@text='y']]/TYPE",
122 "/COMPILATION_UNIT/CLASS_DEF"
123 + "[./IDENT[@text='InputXpathUnusedLambdaParameterShouldBeUnnamedNested']]"
124 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF["
125 + "./IDENT[@text='f1']]/ASSIGN/LAMBDA/SLIST/VARIABLE_DEF["
126 + "./IDENT[@text='f']]/ASSIGN/LAMBDA/PARAMETERS/PARAMETER_DEF/IDENT[@text='y']"
127 );
128 runVerifications(moduleConfig, fileToProcess, expectedViolation,
129 expectedXpathQueries);
130 }
131 }