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