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.Collections;
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.imports.ImportOrderCheck;
30
31 public class XpathRegressionImportOrderTest extends AbstractXpathTestSupport {
32
33 private final String checkName = ImportOrderCheck.class.getSimpleName();
34
35 @Override
36 protected String getCheckName() {
37 return checkName;
38 }
39
40 @Test
41 public void testOne() throws Exception {
42 final File fileToProcess =
43 new File(getPath("InputXpathImportOrderOne.java"));
44
45 final DefaultConfiguration moduleConfig =
46 createModuleConfig(ImportOrderCheck.class);
47
48 final String[] expectedViolation = {
49 "4:1: " + getCheckMessage(ImportOrderCheck.class,
50 ImportOrderCheck.MSG_ORDERING, "java.util.Set"),
51 };
52
53 final List<String> expectedXpathQueries = Collections.singletonList(
54 "/COMPILATION_UNIT/IMPORT"
55 );
56
57 runVerifications(moduleConfig, fileToProcess, expectedViolation,
58 expectedXpathQueries);
59 }
60
61 @Test
62 public void testTwo() throws Exception {
63 final File fileToProcess =
64 new File(getPath("InputXpathImportOrderTwo.java"));
65
66 final DefaultConfiguration moduleConfig =
67 createModuleConfig(ImportOrderCheck.class);
68
69 final String[] expectedViolation = {
70 "5:1: " + getCheckMessage(ImportOrderCheck.class,
71 ImportOrderCheck.MSG_SEPARATED_IN_GROUP, "java.util.Set"),
72 };
73
74 final List<String> expectedXpathQueries = Collections.singletonList(
75 "/COMPILATION_UNIT/IMPORT[./DOT/IDENT[@text='Set']]"
76 );
77
78 runVerifications(moduleConfig, fileToProcess, expectedViolation,
79 expectedXpathQueries);
80 }
81
82 @Test
83 public void testThree() throws Exception {
84 final File fileToProcess =
85 new File(getPath("InputXpathImportOrderThree.java"));
86
87 final DefaultConfiguration moduleConfig =
88 createModuleConfig(ImportOrderCheck.class);
89 moduleConfig.addProperty("groups", "/^java\\./,javax,org");
90 moduleConfig.addProperty("separated", "true");
91
92 final String[] expectedViolation = {
93 "4:1: " + getCheckMessage(ImportOrderCheck.class,
94 ImportOrderCheck.MSG_SEPARATION, "org.junit.*"),
95 };
96
97 final List<String> expectedXpathQueries = Collections.singletonList(
98 "/COMPILATION_UNIT/IMPORT[./DOT/DOT/IDENT[@text='org']]"
99 );
100
101 runVerifications(moduleConfig, fileToProcess, expectedViolation,
102 expectedXpathQueries);
103 }
104
105 @Test
106 public void testFour() throws Exception {
107 final File fileToProcess =
108 new File(getPath("InputXpathImportOrderFour.java"));
109
110 final DefaultConfiguration moduleConfig =
111 createModuleConfig(ImportOrderCheck.class);
112 moduleConfig.addProperty("option", "inflow");
113
114 final String[] expectedViolation = {
115 "5:1: " + getCheckMessage(ImportOrderCheck.class,
116 ImportOrderCheck.MSG_ORDERING, "java.lang.Math.PI"),
117 };
118
119 final List<String> expectedXpathQueries = Collections.singletonList(
120 "/COMPILATION_UNIT/STATIC_IMPORT"
121 );
122
123 runVerifications(moduleConfig, fileToProcess, expectedViolation,
124 expectedXpathQueries);
125 }
126
127 @Test
128 public void testFive() throws Exception {
129 final File fileToProcess =
130 new File(getPath("InputXpathImportOrderFive.java"));
131
132 final DefaultConfiguration moduleConfig =
133 createModuleConfig(ImportOrderCheck.class);
134 moduleConfig.addProperty("groups", "/^java\\./,javax,org");
135
136 final String[] expectedViolation = {
137 "5:1: " + getCheckMessage(ImportOrderCheck.class,
138 ImportOrderCheck.MSG_ORDERING, "java.util.Date"),
139 };
140
141 final List<String> expectedXpathQueries = Collections.singletonList(
142 "/COMPILATION_UNIT/IMPORT[./DOT/IDENT[@text='Date']]"
143 );
144
145 runVerifications(moduleConfig, fileToProcess, expectedViolation,
146 expectedXpathQueries);
147 }
148
149 }