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