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 static com.puppycrawl.tools.checkstyle.checks.coding.IllegalInstantiationCheck.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.coding.IllegalInstantiationCheck;
33  
34  public class XpathRegressionIllegalInstantiationTest extends AbstractXpathTestSupport {
35      @Override
36      protected String getCheckName() {
37          return IllegalInstantiationCheck.class.getSimpleName();
38      }
39  
40      @Override
41      protected String getPackageLocation() {
42          return "org/checkstyle/suppressionxpathfilter/coding/illegalinstantiation";
43      }
44  
45      @Test
46      public void testSimple() throws Exception {
47          final String fileName = "InputXpathIllegalInstantiationSimple.java";
48          final File fileToProcess = new File(getNonCompilablePath(fileName));
49  
50          final DefaultConfiguration moduleConfig =
51                  createModuleConfig(IllegalInstantiationCheck.class);
52          moduleConfig.addProperty("classes", "java.lang.Boolean");
53  
54          final String[] expectedViolation = {
55              "8:21: " + getCheckMessage(IllegalInstantiationCheck.class, MSG_KEY,
56                      "java.lang.Boolean"),
57          };
58  
59          final List<String> expectedXpathQueries = Arrays.asList(
60                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
61                  + "[@text='InputXpathIllegalInstantiationSimple']]"
62                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
63                  + "VARIABLE_DEF[./IDENT[@text='x']]/ASSIGN/EXPR",
64                  "/COMPILATION_UNIT/CLASS_DEF"
65                  + "[./IDENT[@text='InputXpathIllegalInstantiationSimple']]"
66                  + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF"
67                  + "[./IDENT[@text='x']]/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='Boolean']]"
68          );
69  
70          runVerifications(moduleConfig, fileToProcess, expectedViolation,
71                  expectedXpathQueries);
72      }
73  
74      @Test
75      public void testAnonymous() throws Exception {
76          final String fileName = "InputXpathIllegalInstantiationAnonymous.java";
77          final File fileToProcess = new File(getNonCompilablePath(fileName));
78  
79          final DefaultConfiguration moduleConfig =
80                  createModuleConfig(IllegalInstantiationCheck.class);
81          moduleConfig.addProperty("classes", "java.lang.Integer");
82  
83          final String[] expectedViolation = {
84              "10:25: " + getCheckMessage(IllegalInstantiationCheck.class, MSG_KEY,
85                      "java.lang.Integer"),
86          };
87  
88          final List<String> expectedXpathQueries = Arrays.asList(
89                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
90                  + "[@text='InputXpathIllegalInstantiationAnonymous']]"
91                  + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]/OBJBLOCK/METHOD_DEF"
92                  + "[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT[@text='e']]/ASSIGN/EXPR",
93                  "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
94                  + "[@text='InputXpathIllegalInstantiationAnonymous']]"
95                  + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='Inner']]/OBJBLOCK/METHOD_DEF"
96                  + "[./IDENT[@text='test']]/SLIST/VARIABLE_DEF[./IDENT[@text='e']]"
97                  + "/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='Integer']]"
98          );
99  
100         runVerifications(moduleConfig, fileToProcess, expectedViolation,
101                 expectedXpathQueries);
102     }
103 
104     @Test
105     public void testInterface() throws Exception {
106         final String fileName = "InputXpathIllegalInstantiationInterface.java";
107         final File fileToProcess = new File(getNonCompilablePath(fileName));
108 
109         final DefaultConfiguration moduleConfig =
110                 createModuleConfig(IllegalInstantiationCheck.class);
111         moduleConfig.addProperty("classes", "java.lang.String");
112 
113         final String[] expectedViolation = {
114             "10:24: " + getCheckMessage(IllegalInstantiationCheck.class, MSG_KEY,
115                     "java.lang.String"),
116         };
117 
118         final List<String> expectedXpathQueries = Arrays.asList(
119                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
120                 + "[@text='InputXpathIllegalInstantiationInterface']]"
121                 + "/OBJBLOCK/INTERFACE_DEF[./IDENT[@text='Inner']]/"
122                 + "OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/"
123                 + "VARIABLE_DEF[./IDENT[@text='s']]/ASSIGN/EXPR",
124                 "/COMPILATION_UNIT/CLASS_DEF[./IDENT"
125                 + "[@text='InputXpathIllegalInstantiationInterface']]"
126                 + "/OBJBLOCK/INTERFACE_DEF[./IDENT[@text='Inner']]"
127                 + "/OBJBLOCK/METHOD_DEF[./IDENT[@text='test']]/SLIST/VARIABLE_DEF"
128                 + "[./IDENT[@text='s']]/ASSIGN/EXPR/LITERAL_NEW[./IDENT[@text='String']]"
129         );
130 
131         runVerifications(moduleConfig, fileToProcess, expectedViolation,
132                 expectedXpathQueries);
133     }
134 }