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.UncommentedMainCheck;
30  
31  public class XpathRegressionUncommentedMainTest extends AbstractXpathTestSupport {
32  
33      private final Class<UncommentedMainCheck> clazz =
34              UncommentedMainCheck.class;
35  
36      @Override
37      protected String getCheckName() {
38          return clazz.getSimpleName();
39      }
40  
41      @Test
42      public void testDefault() throws Exception {
43          final File fileToProcess =
44                  new File(getPath("InputXpathUncommentedMainDefault.java"));
45  
46          final DefaultConfiguration moduleConfig =
47                  createModuleConfig(UncommentedMainCheck.class);
48  
49          final String[] expectedViolation = {
50              "4:5: " + getCheckMessage(UncommentedMainCheck.class,
51                          UncommentedMainCheck.MSG_KEY),
52          };
53  
54          final List<String> expectedXpathQueries = Arrays.asList(
55                  "/COMPILATION_UNIT/CLASS_DEF"
56                          + "[./IDENT[@text='InputXpathUncommentedMainDefault']]"
57                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='main']]",
58                  "/COMPILATION_UNIT/CLASS_DEF"
59                          + "[./IDENT[@text='InputXpathUncommentedMainDefault']]"
60                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='main']]/MODIFIERS",
61                  "/COMPILATION_UNIT/CLASS_DEF"
62                          + "[./IDENT[@text='InputXpathUncommentedMainDefault']]"
63                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='main']]/MODIFIERS/LITERAL_PUBLIC"
64          );
65  
66          runVerifications(moduleConfig, fileToProcess, expectedViolation,
67                  expectedXpathQueries);
68      }
69  
70      @Test
71      public void testInStaticClass() throws Exception {
72          final File fileToProcess =
73                  new File(getPath("InputXpathUncommentedMainInStaticClass.java"));
74  
75          final DefaultConfiguration moduleConfig =
76                  createModuleConfig(UncommentedMainCheck.class);
77  
78          final String[] expectedViolation = {
79              "5:9: " + getCheckMessage(UncommentedMainCheck.class,
80                          UncommentedMainCheck.MSG_KEY),
81          };
82  
83          final List<String> expectedXpathQueries = Arrays.asList(
84                  "/COMPILATION_UNIT/CLASS_DEF"
85                          + "[./IDENT[@text='InputXpathUncommentedMainInStaticClass']]"
86                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Launcher']"
87                          + "]/OBJBLOCK/METHOD_DEF[./IDENT[@text='main']]",
88                  "/COMPILATION_UNIT/CLASS_DEF"
89                          + "[./IDENT[@text='InputXpathUncommentedMainInStaticClass']]"
90                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Launcher']]"
91                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='main']]/MODIFIERS",
92                  "/COMPILATION_UNIT/CLASS_DEF"
93                          + "[./IDENT[@text='InputXpathUncommentedMainInStaticClass']]"
94                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Launcher']]"
95                          + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='main']]/MODIFIERS/LITERAL_PUBLIC"
96          );
97  
98          runVerifications(moduleConfig, fileToProcess, expectedViolation,
99                  expectedXpathQueries);
100     }
101 }