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 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              "66: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       * We cannot reproduce situation when visitToken is called and leaveToken is not.
91       * So, we have to use reflection to be sure that even in such situation
92       * state of the field will be cleared.
93       *
94       * @throws Exception when code tested throws exception
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 }