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