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.sizes.MethodLengthCheck.MSG_KEY;
23
24 import java.io.File;
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.junit.jupiter.api.Test;
29
30 import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31 import com.puppycrawl.tools.checkstyle.checks.sizes.MethodLengthCheck;
32
33 public class XpathRegressionMethodLengthTest extends AbstractXpathTestSupport {
34
35 private final String checkName = MethodLengthCheck.class.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(getPath("InputXpathMethodLengthSimple.java"));
46
47 final DefaultConfiguration moduleConfig = createModuleConfig(MethodLengthCheck.class);
48 moduleConfig.addProperty("max", "10");
49
50 final String[] expectedViolations = {
51 "4:5: " + getCheckMessage(MethodLengthCheck.class, MSG_KEY,
52 11, 10, "InputXpathMethodLengthSimple"),
53 };
54
55 final List<String> expectedXpathQueries = Arrays.asList(
56 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
57 + "[@text='InputXpathMethodLengthSimple']]"
58 + "/OBJBLOCK/CTOR_DEF[./IDENT"
59 + "[@text='InputXpathMethodLengthSimple']]",
60 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
61 + "[@text='InputXpathMethodLengthSimple']]"
62 + "/OBJBLOCK/CTOR_DEF[./IDENT"
63 + "[@text='InputXpathMethodLengthSimple']]/MODIFIERS",
64 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
65 + "[@text='InputXpathMethodLengthSimple']]"
66 + "/OBJBLOCK/CTOR_DEF[./IDENT"
67 + "[@text='InputXpathMethodLengthSimple']]"
68 + "/MODIFIERS/LITERAL_PROTECTED"
69 );
70
71 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
72 }
73
74 @Test
75 public void testNoEmptyLines() throws Exception {
76 final File fileToProcess =
77 new File(getPath("InputXpathMethodLengthNoEmptyLines.java"));
78
79 final DefaultConfiguration moduleConfig = createModuleConfig(MethodLengthCheck.class);
80 moduleConfig.addProperty("max", "5");
81 moduleConfig.addProperty("countEmpty", "false");
82 moduleConfig.addProperty("tokens", "METHOD_DEF");
83
84 final String[] expectedViolations = {
85 "15:5: " + getCheckMessage(MethodLengthCheck.class, MSG_KEY, 6, 5, "methodOne"),
86 };
87
88 final List<String> expectedXpathQueries = Arrays.asList(
89 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
90 + "[@text='InputXpathMethodLengthNoEmptyLines']]"
91 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='methodOne']]",
92 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
93 + "[@text='InputXpathMethodLengthNoEmptyLines']]"
94 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='methodOne']]/MODIFIERS",
95 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
96 + "[@text='InputXpathMethodLengthNoEmptyLines']]"
97 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='methodOne']]"
98 + "/MODIFIERS/LITERAL_PROTECTED"
99 );
100
101 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
102 }
103
104 @Test
105 public void testSingleToken() throws Exception {
106 final File fileToProcess = new File(
107 getPath("InputXpathMethodLengthSingleToken.java"));
108
109 final DefaultConfiguration moduleConfig = createModuleConfig(MethodLengthCheck.class);
110 moduleConfig.addProperty("max", "1");
111 moduleConfig.addProperty("tokens", "METHOD_DEF");
112
113 final String[] expectedViolations = {
114 "9:9: " + getCheckMessage(MethodLengthCheck.class, MSG_KEY, 3, 1, "methodOne"),
115 };
116
117 final List<String> expectedXpathQueries = Arrays.asList(
118 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
119 + "[@text='InputXpathMethodLengthSingleToken']]"
120 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='a']]/ASSIGN/EXPR/LITERAL_NEW"
121 + "[./IDENT[@text='InputXpathMethodLengthSingleToken']]"
122 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='methodOne']]",
123 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
124 + "[@text='InputXpathMethodLengthSingleToken']]"
125 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='a']]/ASSIGN/EXPR/LITERAL_NEW"
126 + "[./IDENT[@text='InputXpathMethodLengthSingleToken']]"
127 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='methodOne']]/MODIFIERS",
128 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
129 + "[@text='InputXpathMethodLengthSingleToken']]"
130 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='a']]/ASSIGN/EXPR/LITERAL_NEW"
131 + "[./IDENT[@text='InputXpathMethodLengthSingleToken']]"
132 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='methodOne']]"
133 + "/MODIFIERS/LITERAL_PUBLIC"
134 );
135
136 runVerifications(moduleConfig, fileToProcess, expectedViolations, expectedXpathQueries);
137 }
138 }