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