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.ConstructorsDeclarationGroupingCheck;
30
31 public class XpathRegressionConstructorsDeclarationGroupingTest extends AbstractXpathTestSupport {
32
33 private static final Class<ConstructorsDeclarationGroupingCheck> CLAZZ =
34 ConstructorsDeclarationGroupingCheck.class;
35
36 @Override
37 protected String getCheckName() {
38 return CLAZZ.getSimpleName();
39 }
40
41 @Test
42 public void testClass() throws Exception {
43 final File fileToProcess = new File(
44 getPath("InputXpathConstructorsDeclarationGroupingClass.java"));
45
46 final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
47
48 final String[] expectedViolation = {
49 "10:5: " + getCheckMessage(CLAZZ,
50 ConstructorsDeclarationGroupingCheck.MSG_KEY, 6),
51 };
52
53 final List<String> expectedXpathQueries = Arrays.asList(
54 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
55 + "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
56 + "/OBJBLOCK/CTOR_DEF[./IDENT"
57 + "[@text='InputXpathConstructorsDeclarationGroupingClass']]",
58 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
59 + "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
60 + "/OBJBLOCK/CTOR_DEF[./IDENT"
61 + "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
62 + "/MODIFIERS",
63 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
64 + "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
65 + "/OBJBLOCK/CTOR_DEF/IDENT"
66 + "[@text='InputXpathConstructorsDeclarationGroupingClass']"
67
68 );
69
70 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
71 }
72
73 @Test
74 public void testEnum() throws Exception {
75 final File fileToProcess = new File(
76 getPath("InputXpathConstructorsDeclarationGroupingEnum.java"));
77
78 final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
79
80 final String[] expectedViolation = {
81 "12:5: " + getCheckMessage(CLAZZ,
82 ConstructorsDeclarationGroupingCheck.MSG_KEY, 8),
83 };
84
85 final List<String> expectedXpathQueries = Arrays.asList(
86 "/COMPILATION_UNIT/ENUM_DEF[./IDENT"
87 + "[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
88 + "/OBJBLOCK/CTOR_DEF"
89 + "[./IDENT[@text='InputXpathConstructorsDeclarationGroupingEnum']]",
90
91 "/COMPILATION_UNIT/ENUM_DEF[./IDENT"
92 + "[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
93 + "/OBJBLOCK/CTOR_DEF"
94 + "[./IDENT[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
95 + "/MODIFIERS",
96
97 "/COMPILATION_UNIT/ENUM_DEF[./IDENT"
98 + "[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
99 + "/OBJBLOCK/CTOR_DEF/IDENT"
100 + "[@text='InputXpathConstructorsDeclarationGroupingEnum']"
101 );
102
103 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
104 }
105
106 @Test
107 public void testRecords() throws Exception {
108 final File fileToProcess = new File(
109 getNonCompilablePath("InputXpathConstructorsDeclarationGroupingRecords.java"));
110
111 final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
112
113 final String[] expectedViolation = {
114 "14:5: " + getCheckMessage(CLAZZ,
115 ConstructorsDeclarationGroupingCheck.MSG_KEY, 8),
116 };
117
118 final List<String> expectedXpathQueries = Arrays.asList(
119 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
120 + "[@text='InputXpathConstructorsDeclarationGroupingRecords']]"
121 + "/OBJBLOCK/RECORD_DEF[./IDENT[@text='MyRecord']]"
122 + "/OBJBLOCK/COMPACT_CTOR_DEF[./IDENT[@text='MyRecord']]",
123
124 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
125 + "[@text='InputXpathConstructorsDeclarationGroupingRecords']]"
126 + "/OBJBLOCK/RECORD_DEF[./IDENT[@text='MyRecord']]"
127 + "/OBJBLOCK/COMPACT_CTOR_DEF[./IDENT[@text='MyRecord']]/MODIFIERS",
128
129 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
130 + "[@text='InputXpathConstructorsDeclarationGroupingRecords']]"
131 + "/OBJBLOCK/RECORD_DEF[./IDENT[@text='MyRecord']]"
132 + "/OBJBLOCK/COMPACT_CTOR_DEF[./IDENT[@text='MyRecord']]"
133 + "/MODIFIERS/LITERAL_PUBLIC"
134 );
135
136 runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
137 }
138 }