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