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