View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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.CustomImportOrderCheck;
30  
31  public class XpathRegressionCustomImportOrderTest extends AbstractXpathTestSupport {
32  
33      private final String checkName = CustomImportOrderCheck.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("InputXpathCustomImportOrderOne.java"));
44  
45          final DefaultConfiguration moduleConfig =
46                  createModuleConfig(CustomImportOrderCheck.class);
47          moduleConfig.addProperty("customImportOrderRules", "STATIC###STANDARD_JAVA_PACKAGE");
48          moduleConfig.addProperty("sortImportsInGroupAlphabetically", "true");
49  
50          final String[] expectedViolation = {
51              "4:1: " + getCheckMessage(CustomImportOrderCheck.class,
52                          CustomImportOrderCheck.MSG_LEX, "java.lang.Math.PI",
53                          "java.util.Arrays.sort"),
54          };
55  
56          final List<String> expectedXpathQueries = Collections.singletonList(
57                  "/COMPILATION_UNIT/STATIC_IMPORT[./DOT/IDENT[@text='PI']]"
58          );
59  
60          runVerifications(moduleConfig, fileToProcess, expectedViolation,
61                  expectedXpathQueries);
62      }
63  
64      @Test
65      public void testTwo() throws Exception {
66          final File fileToProcess =
67                  new File(getPath("InputXpathCustomImportOrderTwo.java"));
68  
69          final DefaultConfiguration moduleConfig =
70                  createModuleConfig(CustomImportOrderCheck.class);
71          moduleConfig.addProperty("customImportOrderRules", "STATIC###STANDARD_JAVA_PACKAGE");
72  
73          final String[] expectedViolation = {
74              "5:1: " + getCheckMessage(CustomImportOrderCheck.class,
75                          CustomImportOrderCheck.MSG_LINE_SEPARATOR, "java.io.File"),
76          };
77  
78          final List<String> expectedXpathQueries = Collections.singletonList(
79                  "/COMPILATION_UNIT/IMPORT[./DOT/IDENT[@text='File']]"
80          );
81  
82          runVerifications(moduleConfig, fileToProcess, expectedViolation,
83                  expectedXpathQueries);
84      }
85  
86      @Test
87      public void testThree() throws Exception {
88          final File fileToProcess =
89                  new File(getPath("InputXpathCustomImportOrderThree.java"));
90  
91          final DefaultConfiguration moduleConfig =
92                  createModuleConfig(CustomImportOrderCheck.class);
93          moduleConfig.addProperty("customImportOrderRules", "STATIC###STANDARD_JAVA_PACKAGE");
94  
95          final String[] expectedViolation = {
96              "5:1: " + getCheckMessage(CustomImportOrderCheck.class,
97                          CustomImportOrderCheck.MSG_SEPARATED_IN_GROUP, "java.lang.Math.PI"),
98          };
99  
100         final List<String> expectedXpathQueries = Collections.singletonList(
101                 "/COMPILATION_UNIT/STATIC_IMPORT[./DOT/IDENT[@text='PI']]"
102         );
103 
104         runVerifications(moduleConfig, fileToProcess, expectedViolation,
105                 expectedXpathQueries);
106     }
107 
108     @Test
109     public void testFour() throws Exception {
110         final File fileToProcess =
111                 new File(getPath("InputXpathCustomImportOrderFour.java"));
112 
113         final DefaultConfiguration moduleConfig =
114                 createModuleConfig(CustomImportOrderCheck.class);
115         moduleConfig.addProperty("customImportOrderRules", "STATIC###STANDARD_JAVA_PACKAGE");
116 
117         final String[] expectedViolation = {
118             "5:1: " + getCheckMessage(CustomImportOrderCheck.class,
119                         CustomImportOrderCheck.MSG_NONGROUP_IMPORT,
120                         "com.puppycrawl.tools.checkstyle.api.DetailAST"),
121         };
122 
123         final List<String> expectedXpathQueries = Collections.singletonList(
124                 "/COMPILATION_UNIT/IMPORT[./DOT/IDENT[@text='DetailAST']]"
125         );
126 
127         runVerifications(moduleConfig, fileToProcess, expectedViolation,
128                 expectedXpathQueries);
129     }
130 
131     @Test
132     public void testFive() throws Exception {
133         final File fileToProcess =
134                 new File(getPath("InputXpathCustomImportOrderFive.java"));
135 
136         final DefaultConfiguration moduleConfig =
137                 createModuleConfig(CustomImportOrderCheck.class);
138         moduleConfig.addProperty("customImportOrderRules", "STATIC###STANDARD_JAVA_PACKAGE");
139 
140         final String[] expectedViolation = {
141             "7:1: " + getCheckMessage(CustomImportOrderCheck.class,
142                         CustomImportOrderCheck.MSG_NONGROUP_EXPECTED, "STATIC",
143                         "java.lang.Math.PI"),
144         };
145 
146         final List<String> expectedXpathQueries = Collections.singletonList(
147                 "/COMPILATION_UNIT/STATIC_IMPORT[./DOT/IDENT[@text='PI']]"
148         );
149 
150         runVerifications(moduleConfig, fileToProcess, expectedViolation,
151                 expectedXpathQueries);
152     }
153 
154     @Test
155     public void testSix() throws Exception {
156         final File fileToProcess =
157                 new File(getPath("InputXpathCustomImportOrderSix.java"));
158 
159         final DefaultConfiguration moduleConfig =
160                 createModuleConfig(CustomImportOrderCheck.class);
161         moduleConfig.addProperty("customImportOrderRules",
162             "STATIC###STANDARD_JAVA_PACKAGE###THIRD_PARTY_PACKAGE");
163 
164         final String[] expectedViolation = {
165             "6:1: " + getCheckMessage(CustomImportOrderCheck.class,
166                         CustomImportOrderCheck.MSG_ORDER, "THIRD_PARTY_PACKAGE",
167                         "STANDARD_JAVA_PACKAGE",
168                         "com.puppycrawl.tools.checkstyle.api.DetailAST"),
169         };
170 
171         final List<String> expectedXpathQueries = Collections.singletonList(
172                 "/COMPILATION_UNIT/IMPORT[./DOT/IDENT[@text='DetailAST']]"
173         );
174 
175         runVerifications(moduleConfig, fileToProcess, expectedViolation,
176                 expectedXpathQueries);
177     }
178 
179 }