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.ConstructorsDeclarationGroupingCheck;
31  
32  public class XpathRegressionConstructorsDeclarationGroupingTest extends AbstractXpathTestSupport {
33  
34      private static final Class<ConstructorsDeclarationGroupingCheck> CLAZZ =
35              ConstructorsDeclarationGroupingCheck.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/constructorsdeclarationgrouping";
45      }
46  
47      @Test
48      public void testClass() throws Exception {
49          final File fileToProcess = new File(
50                  getPath("InputXpathConstructorsDeclarationGroupingClass.java"));
51  
52          final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
53  
54          final String[] expectedViolation = {
55              "10:5: " + getCheckMessage(CLAZZ,
56                      ConstructorsDeclarationGroupingCheck.MSG_KEY, 6),
57          };
58  
59          final List<String> expectedXpathQueries = Arrays.asList(
60                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
61                          + "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
62                          + "/OBJBLOCK/CTOR_DEF[./IDENT"
63                          + "[@text='InputXpathConstructorsDeclarationGroupingClass']]",
64                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
65                          + "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
66                          + "/OBJBLOCK/CTOR_DEF[./IDENT"
67                          + "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
68                          + "/MODIFIERS",
69                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
70                          + "[@text='InputXpathConstructorsDeclarationGroupingClass']]"
71                          + "/OBJBLOCK/CTOR_DEF/IDENT"
72                          + "[@text='InputXpathConstructorsDeclarationGroupingClass']"
73  
74          );
75  
76          runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
77      }
78  
79      @Test
80      public void testEnum() throws Exception {
81          final File fileToProcess = new File(
82                  getPath("InputXpathConstructorsDeclarationGroupingEnum.java"));
83  
84          final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
85  
86          final String[] expectedViolation = {
87              "12:5: " + getCheckMessage(CLAZZ,
88                      ConstructorsDeclarationGroupingCheck.MSG_KEY, 8),
89          };
90  
91          final List<String> expectedXpathQueries = Arrays.asList(
92                  "/COMPILATION_UNIT/ENUM_DEF[./IDENT"
93                          + "[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
94                          + "/OBJBLOCK/CTOR_DEF"
95                          + "[./IDENT[@text='InputXpathConstructorsDeclarationGroupingEnum']]",
96  
97                  "/COMPILATION_UNIT/ENUM_DEF[./IDENT"
98                          + "[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
99                          + "/OBJBLOCK/CTOR_DEF"
100                         + "[./IDENT[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
101                         + "/MODIFIERS",
102 
103                 "/COMPILATION_UNIT/ENUM_DEF[./IDENT"
104                         + "[@text='InputXpathConstructorsDeclarationGroupingEnum']]"
105                         + "/OBJBLOCK/CTOR_DEF/IDENT"
106                         + "[@text='InputXpathConstructorsDeclarationGroupingEnum']"
107         );
108 
109         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
110     }
111 
112     @Test
113     public void testRecords() throws Exception {
114         final File fileToProcess = new File(
115                 getPath("InputXpathConstructorsDeclarationGroupingRecords.java"));
116 
117         final DefaultConfiguration moduleConfig = createModuleConfig(CLAZZ);
118 
119         final String[] expectedViolation = {
120             "14:5: " + getCheckMessage(CLAZZ,
121                     ConstructorsDeclarationGroupingCheck.MSG_KEY, 8),
122         };
123 
124         final List<String> expectedXpathQueries = Arrays.asList(
125                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
126                         + "[@text='InputXpathConstructorsDeclarationGroupingRecords']]"
127                         + "/OBJBLOCK/RECORD_DEF[./IDENT[@text='MyRecord']]"
128                         + "/OBJBLOCK/COMPACT_CTOR_DEF[./IDENT[@text='MyRecord']]",
129 
130                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
131                         + "[@text='InputXpathConstructorsDeclarationGroupingRecords']]"
132                         + "/OBJBLOCK/RECORD_DEF[./IDENT[@text='MyRecord']]"
133                         + "/OBJBLOCK/COMPACT_CTOR_DEF[./IDENT[@text='MyRecord']]/MODIFIERS",
134 
135                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
136                         + "[@text='InputXpathConstructorsDeclarationGroupingRecords']]"
137                         + "/OBJBLOCK/RECORD_DEF[./IDENT[@text='MyRecord']]"
138                         + "/OBJBLOCK/COMPACT_CTOR_DEF[./IDENT[@text='MyRecord']]"
139                         + "/MODIFIERS/LITERAL_PUBLIC"
140         );
141 
142         runVerifications(moduleConfig, fileToProcess, expectedViolation, expectedXpathQueries);
143     }
144 }