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.modifier.ModifierOrderCheck;
31
32 public class XpathRegressionModifierOrderTest extends AbstractXpathTestSupport {
33
34 private static final Class<ModifierOrderCheck> CLAZZ = ModifierOrderCheck.class;
35
36 @Override
37 protected String getCheckName() {
38 return CLAZZ.getSimpleName();
39 }
40
41 @Test
42 public void testMethod() throws Exception {
43 final File fileToProcess = new File(
44 getPath("InputXpathModifierOrderMethod.java"));
45
46 final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
47
48 final String[] expectedViolation = {
49 "4:13: " + getCheckMessage(CLAZZ,
50 ModifierOrderCheck.MSG_ANNOTATION_ORDER, "@MethodAnnotation"),
51 };
52
53 final List<String> expectedXpathQueries = Arrays.asList(
54 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
55 + "[@text='InputXpathModifierOrderMethod']]"
56 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/MODIFIERS"
57 + "/ANNOTATION[./IDENT[@text='MethodAnnotation']]",
58 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
59 + "[@text='InputXpathModifierOrderMethod']]"
60 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='foo']]/MODIFIERS"
61 + "/ANNOTATION[./IDENT[@text='MethodAnnotation']]/AT");
62
63 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
64 }
65
66 @Test
67 public void testVariable() throws Exception {
68 final File fileToProcess = new File(
69 getPath("InputXpathModifierOrderVariable.java"));
70
71 final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
72
73 final String[] expectedViolation = {
74 "3:12: " + getCheckMessage(CLAZZ,
75 ModifierOrderCheck.MSG_MODIFIER_ORDER, "private"),
76 };
77
78 final List<String> expectedXpathQueries = Collections.singletonList(
79 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
80 + "[@text='InputXpathModifierOrderVariable']]"
81 + "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='var']]/MODIFIERS/LITERAL_PRIVATE");
82
83 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
84 }
85
86 @Test
87 public void testAnnotation() throws Exception {
88 final File fileToProcess = new File(
89 getPath("InputXpathModifierOrderAnnotation.java"));
90
91 final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
92
93 final String[] expectedViolation = {
94 "3:8: " + getCheckMessage(CLAZZ,
95 ModifierOrderCheck.MSG_ANNOTATION_ORDER, "@InterfaceAnnotation"),
96 };
97
98 final List<String> expectedXpathQueries = Arrays.asList(
99 "/COMPILATION_UNIT/ANNOTATION_DEF[./IDENT"
100 + "[@text='InputXpathModifierOrderAnnotation']]"
101 + "/MODIFIERS/ANNOTATION[./IDENT[@text='InterfaceAnnotation']]",
102 "/COMPILATION_UNIT/ANNOTATION_DEF[./IDENT"
103 + "[@text='InputXpathModifierOrderAnnotation']]"
104 + "/MODIFIERS/ANNOTATION[./IDENT[@text='InterfaceAnnotation']]/AT");
105
106 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
107 }
108
109 }