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.design;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.design.HideUtilityClassConstructorCheck.MSG_KEY;
23  
24  import java.io.File;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
29  import org.junit.jupiter.api.Test;
30  
31  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32  import com.puppycrawl.tools.checkstyle.checks.design.HideUtilityClassConstructorCheck;
33  
34  public class XpathRegressionHideUtilityClassConstructorTest extends AbstractXpathTestSupport {
35  
36      private final String checkName = HideUtilityClassConstructorCheck.class.getSimpleName();
37  
38      @Override
39      protected String getCheckName() {
40          return checkName;
41      }
42  
43      @Override
44      protected String getPackageLocation() {
45          return "org/checkstyle/suppressionxpathfilter/design/hideutilityclassconstructor";
46      }
47  
48      @Test
49      public void testDefaultConstructor() throws Exception {
50          final File fileToProcess =
51                  new File(getPath("InputXpathHideUtilityClassConstructorDefault.java"));
52          final DefaultConfiguration moduleConfig =
53                  createModuleConfig(HideUtilityClassConstructorCheck.class);
54          final String[] expectedViolation = {
55              "3:1: " + getCheckMessage(HideUtilityClassConstructorCheck.class, MSG_KEY),
56          };
57          final List<String> expectedXpathQueries = Arrays.asList(
58              "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
59                      + "InputXpathHideUtilityClassConstructorDefault']]",
60              "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
61                      + "InputXpathHideUtilityClassConstructorDefault']]/MODIFIERS",
62              "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
63                      + "InputXpathHideUtilityClassConstructorDefault']]/MODIFIERS/LITERAL_PUBLIC"
64          );
65  
66          runVerifications(moduleConfig, fileToProcess, expectedViolation,
67                  expectedXpathQueries);
68      }
69  
70      @Test
71      public void testPublicConstructor() throws Exception {
72          final File fileToProcess =
73                  new File(getPath("InputXpathHideUtilityClassConstructorPublic.java"));
74          final DefaultConfiguration moduleConfig =
75                  createModuleConfig(HideUtilityClassConstructorCheck.class);
76          final String[] expectedViolation = {
77              "3:1: " + getCheckMessage(HideUtilityClassConstructorCheck.class, MSG_KEY),
78          };
79          final List<String> expectedXpathQueries = Arrays.asList(
80              "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
81                      + "InputXpathHideUtilityClassConstructorPublic']]",
82              "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
83                      + "InputXpathHideUtilityClassConstructorPublic']]/MODIFIERS",
84              "/COMPILATION_UNIT/CLASS_DEF[./IDENT[@text='"
85                      + "InputXpathHideUtilityClassConstructorPublic']]/MODIFIERS/LITERAL_PUBLIC"
86          );
87  
88          runVerifications(moduleConfig, fileToProcess, expectedViolation,
89                  expectedXpathQueries);
90      }
91  }