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.coding;
21  
22  import java.io.File;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.coding.OverloadMethodsDeclarationOrderCheck;
31  
32  public class XpathRegressionOverloadMethodsDeclarationOrderTest extends AbstractXpathTestSupport {
33  
34      private static final Class<OverloadMethodsDeclarationOrderCheck> CLAZZ =
35              OverloadMethodsDeclarationOrderCheck.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/coding/overloadmethodsdeclarationorder";
45      }
46  
47      @Test
48      public void testDefault() throws Exception {
49          final File fileToProcess = new File(
50                  getPath("InputXpathOverloadMethodsDeclarationOrderDefault.java"));
51  
52          final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
53  
54          final String[] expectedViolation = {
55              "14:5: " + getCheckMessage(CLAZZ,
56                          OverloadMethodsDeclarationOrderCheck.MSG_KEY, "5"),
57          };
58  
59          final List<String> expectedXpathQueries = Arrays.asList(
60                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
61                          + "[@text='InputXpathOverloadMethodsDeclarationOrderDefault']]"
62                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='overloadMethod']]",
63                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
64                          + "[@text='InputXpathOverloadMethodsDeclarationOrderDefault']]"
65                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='overloadMethod']]/MODIFIERS",
66                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
67                          + "[@text='InputXpathOverloadMethodsDeclarationOrderDefault']]"
68                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='overloadMethod']]"
69                          + "/MODIFIERS/LITERAL_PUBLIC"
70          );
71  
72          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
73      }
74  
75      @Test
76      public void testAnonymous() throws Exception {
77          final File fileToProcess = new File(
78                  getPath("InputXpathOverloadMethodsDeclarationOrderAnonymous.java"));
79  
80          final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
81  
82          final String[] expectedViolation = {
83              "30:9: " + getCheckMessage(CLAZZ,
84                      OverloadMethodsDeclarationOrderCheck.MSG_KEY, "21"),
85          };
86  
87          final List<String> expectedXpathQueries = Arrays.asList(
88                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
89                          + "'InputXpathOverloadMethodsDeclarationOrderAnonymous']]"
90                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text="
91                          + "'MyInputXpathOverloadMethodsDeclarationOrderAnonymous']]"
92                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='overloadMethod']]",
93                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
94                          + "'InputXpathOverloadMethodsDeclarationOrderAnonymous']]"
95                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text="
96                          + "'MyInputXpathOverloadMethodsDeclarationOrderAnonymous']]"
97                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='overloadMethod']]/MODIFIERS",
98                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text="
99                          + "'InputXpathOverloadMethodsDeclarationOrderAnonymous']]"
100                         + "/OBJBLOCK/CLASS_DEF[./IDENT[@text="
101                         + "'MyInputXpathOverloadMethodsDeclarationOrderAnonymous']]"
102                         + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='overloadMethod']]"
103                         + "/MODIFIERS/LITERAL_PUBLIC"
104         );
105 
106         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
107     }
108 
109 }