1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.coding;
21
22 import static com.google.common.truth.Truth.assertWithMessage;
23 import static com.puppycrawl.tools.checkstyle.checks.coding.AbstractSuperCheck.MSG_KEY;
24
25 import java.io.File;
26 import java.util.Collection;
27 import java.util.Optional;
28 import java.util.Set;
29
30 import org.junit.jupiter.api.Test;
31
32 import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
33 import com.puppycrawl.tools.checkstyle.JavaParser;
34 import com.puppycrawl.tools.checkstyle.api.DetailAST;
35 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
36 import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
37 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
38
39 public class SuperCloneCheckTest
40 extends AbstractModuleTestSupport {
41
42 @Override
43 protected String getPackageLocation() {
44 return "com/puppycrawl/tools/checkstyle/checks/coding/superclone";
45 }
46
47 @Test
48 public void testIt() throws Exception {
49 final String[] expected = {
50 "33:19: " + getCheckMessage(MSG_KEY, "clone", "super.clone"),
51 "41:19: " + getCheckMessage(MSG_KEY, "clone", "super.clone"),
52 "67:48: " + getCheckMessage(MSG_KEY, "clone", "super.clone"),
53 };
54 verifyWithInlineConfigParser(
55 getPath("InputSuperCloneInnerAndWithArguments.java"), expected);
56 }
57
58 @Test
59 public void testAnotherInputFile() throws Exception {
60 final String[] expected = {
61 "14:17: " + getCheckMessage(MSG_KEY, "clone", "super.clone"),
62 "48:17: " + getCheckMessage(MSG_KEY, "clone", "super.clone"),
63 };
64 verifyWithInlineConfigParser(
65 getPath("InputSuperClonePlainAndSubclasses.java"), expected);
66 }
67
68 @Test
69 public void testMethodReference() throws Exception {
70 final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
71 verifyWithInlineConfigParser(
72 getPath("InputSuperCloneMethodReference.java"), expected);
73 }
74
75 @Test
76 public void testTokensNotNull() {
77 final SuperCloneCheck check = new SuperCloneCheck();
78 assertWithMessage("Acceptable tokens should not be null")
79 .that(check.getAcceptableTokens())
80 .isNotNull();
81 assertWithMessage("Default tokens should not be null")
82 .that(check.getDefaultTokens())
83 .isNotNull();
84 assertWithMessage("Required tokens should not be null")
85 .that(check.getRequiredTokens())
86 .isNotNull();
87 }
88
89
90
91
92
93
94
95
96 @Test
97 @SuppressWarnings("unchecked")
98 public void testClearState() throws Exception {
99 final AbstractSuperCheck check = new SuperCloneCheck();
100 final Optional<DetailAST> methodDef = TestUtil.findTokenInAstByPredicate(
101 JavaParser.parseFile(new File(getPath("InputSuperCloneWithoutWarnings.java")),
102 JavaParser.Options.WITHOUT_COMMENTS),
103 ast -> ast.getType() == TokenTypes.METHOD_DEF);
104
105 assertWithMessage("Ast should contain METHOD_DEF")
106 .that(methodDef.isPresent())
107 .isTrue();
108 assertWithMessage("State is not cleared on beginTree")
109 .that(TestUtil.isStatefulFieldClearedDuringBeginTree(check,
110 methodDef.orElseThrow(), "methodStack",
111 methodStack -> ((Collection<Set<String>>) methodStack).isEmpty()))
112 .isTrue();
113 }
114
115 }