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