View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.puppycrawl.tools.checkstyle.checks.coding.AbstractSuperCheck.MSG_KEY;
23  import static org.junit.Assert.assertTrue;
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.Assert;
31  import org.junit.Test;
32  
33  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
34  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
35  import com.puppycrawl.tools.checkstyle.api.DetailAST;
36  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
37  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
38  
39  public class SuperCloneCheckTest
40      extends AbstractModuleTestSupport {
41      @Override
42      protected String getPackageLocation() {
43          return "com/puppycrawl/tools/checkstyle/checks/coding/superclone";
44      }
45  
46      @Test
47      public void testIt() throws Exception {
48          final DefaultConfiguration checkConfig =
49              createModuleConfig(SuperCloneCheck.class);
50          final String[] expected = {
51              "27:19: " + getCheckMessage(MSG_KEY, "clone", "super.clone"),
52              "35:19: " + getCheckMessage(MSG_KEY, "clone", "super.clone"),
53              "60:48: " + getCheckMessage(MSG_KEY, "clone", "super.clone"),
54          };
55          verify(checkConfig, getPath("InputSuperCloneInnerAndWithArguments.java"), expected);
56      }
57  
58      @Test
59      public void testAnotherInputFile() throws Exception {
60          final DefaultConfiguration checkConfig =
61              createModuleConfig(SuperCloneCheck.class);
62          final String[] expected = {
63              "9:17: " + getCheckMessage(MSG_KEY, "clone", "super.clone"),
64          };
65          verify(checkConfig, getPath("InputSuperClonePlainAndSubclasses.java"), expected);
66      }
67  
68      @Test
69      public void testTokensNotNull() {
70          final SuperCloneCheck check = new SuperCloneCheck();
71          Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
72          Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
73          Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
74      }
75  
76      /**
77       * We cannot reproduce situation when visitToken is called and leaveToken is not.
78       * So, we have to use reflection to be sure that even in such situation
79       * state of the field will be cleared.
80       *
81       * @throws Exception when code tested throws exception
82       */
83      @Test
84      @SuppressWarnings("unchecked")
85      public void testClearState() throws Exception {
86          final AbstractSuperCheck check = new SuperCloneCheck();
87          final Optional<DetailAST> methodDef = TestUtil.findTokenInAstByPredicate(
88              TestUtil.parseFile(new File(getPath("InputSuperCloneWithoutWarnings.java"))),
89              ast -> ast.getType() == TokenTypes.METHOD_DEF);
90  
91          assertTrue("Ast should contain METHOD_DEF", methodDef.isPresent());
92          assertTrue("State is not cleared on beginTree",
93              TestUtil.isStatefulFieldClearedDuringBeginTree(check, methodDef.get(),
94                  "methodStack",
95                  methodStack -> ((Collection<Set<String>>) methodStack).isEmpty()));
96      }
97  }