1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }