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.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 }