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