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 java.io.File;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.checkstyle.suppressionxpathfilter.AbstractXpathTestSupport;
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.checks.coding.AbstractSuperCheck;
31  import com.puppycrawl.tools.checkstyle.checks.coding.SuperCloneCheck;
32  
33  public class XpathRegressionSuperCloneTest extends AbstractXpathTestSupport {
34  
35      @Override
36      protected String getCheckName() {
37          return SuperCloneCheck.class.getSimpleName();
38      }
39  
40      @Override
41      protected String getPackageLocation() {
42          return "org/checkstyle/suppressionxpathfilter/coding/superclone";
43      }
44  
45      @Test
46      public void testInnerClone() throws Exception {
47          final File fileToProcess =
48                  new File(getPath("InputXpathSuperCloneInnerClone.java"));
49  
50          final DefaultConfiguration moduleConfig =
51                  createModuleConfig(SuperCloneCheck.class);
52  
53          final String[] expectedViolation = {
54              "6:23: " + getCheckMessage(SuperCloneCheck.class, AbstractSuperCheck.MSG_KEY, "clone"),
55          };
56  
57          final List<String> expectedXpathQueries = Collections.singletonList(
58                  "/COMPILATION_UNIT/CLASS_DEF"
59                          + "[./IDENT[@text='InputXpathSuperCloneInnerClone']]"
60                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='InnerClone']]"
61                          + "/OBJBLOCK/METHOD_DEF/IDENT[@text='clone']"
62          );
63  
64          runVerifications(moduleConfig, fileToProcess, expectedViolation,
65                  expectedXpathQueries);
66      }
67  
68      @Test
69      public void testNoSuperClone() throws Exception {
70          final File fileToProcess =
71                  new File(getPath("InputXpathSuperCloneNoSuperClone.java"));
72  
73          final DefaultConfiguration moduleConfig =
74                  createModuleConfig(SuperCloneCheck.class);
75  
76          final String[] expectedViolation = {
77              "6:23: " + getCheckMessage(SuperCloneCheck.class, AbstractSuperCheck.MSG_KEY, "clone"),
78          };
79  
80          final List<String> expectedXpathQueries = Collections.singletonList(
81                  "/COMPILATION_UNIT/CLASS_DEF"
82                          + "[./IDENT[@text='InputXpathSuperCloneNoSuperClone']]"
83                          + "/OBJBLOCK/CLASS_DEF[./IDENT[@text='NoSuperClone']]"
84                          + "/OBJBLOCK/METHOD_DEF/IDENT[@text='clone']"
85          );
86  
87          runVerifications(moduleConfig, fileToProcess, expectedViolation,
88                  expectedXpathQueries);
89      }
90  
91      @Test
92      public void testPlainAndSubclasses() throws Exception {
93          final File fileToProcess =
94                  new File(getPath("InputXpathSuperClonePlainAndSubclasses.java"));
95  
96          final DefaultConfiguration moduleConfig =
97                  createModuleConfig(SuperCloneCheck.class);
98  
99          final String[] expectedViolation = {
100             "4:19: " + getCheckMessage(SuperCloneCheck.class, AbstractSuperCheck.MSG_KEY, "clone"),
101         };
102 
103         final List<String> expectedXpathQueries = Collections.singletonList(
104                 "/COMPILATION_UNIT/CLASS_DEF"
105                         + "[./IDENT[@text='InputXpathSuperClonePlainAndSubclasses']]"
106                         + "/OBJBLOCK/METHOD_DEF/IDENT[@text='clone']"
107         );
108 
109         runVerifications(moduleConfig, fileToProcess, expectedViolation,
110                 expectedXpathQueries);
111     }
112 }