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.ParameterAssignmentCheck.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  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
39  
40  public class ParameterAssignmentCheckTest extends AbstractModuleTestSupport {
41      @Override
42      protected String getPackageLocation() {
43          return "com/puppycrawl/tools/checkstyle/checks/coding/parameterassignment";
44      }
45  
46      @Test
47      public void testDefault()
48              throws Exception {
49          final DefaultConfiguration checkConfig =
50              createModuleConfig(ParameterAssignmentCheck.class);
51          final String[] expected = {
52              "9:15: " + getCheckMessage(MSG_KEY, "field"),
53              "10:15: " + getCheckMessage(MSG_KEY, "field"),
54              "12:14: " + getCheckMessage(MSG_KEY, "field"),
55              "20:30: " + getCheckMessage(MSG_KEY, "field1"),
56          };
57          verify(checkConfig, getPath("InputParameterAssignmentWithUnchecked.java"),
58                 expected);
59      }
60  
61      @Test
62      public void testReceiverParameter() throws Exception {
63          final DefaultConfiguration checkConfig = createModuleConfig(ParameterAssignmentCheck.class);
64          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
65          verify(checkConfig, getPath("InputParameterAssignmentReceiver.java"), expected);
66      }
67  
68      @Test
69      public void testTokensNotNull() {
70          final ParameterAssignmentCheck check = new ParameterAssignmentCheck();
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      @Test
77      public void testImproperToken() {
78          final ParameterAssignmentCheck check = new ParameterAssignmentCheck();
79  
80          final DetailAST classDefAst = new DetailAST();
81          classDefAst.setType(TokenTypes.CLASS_DEF);
82  
83          try {
84              check.visitToken(classDefAst);
85              Assert.fail("IllegalStateException is expected");
86          }
87          catch (IllegalStateException ex) {
88              // it is OK
89          }
90  
91          try {
92              check.leaveToken(classDefAst);
93              Assert.fail("IllegalStateException is expected");
94          }
95          catch (IllegalStateException ex) {
96              // it is OK
97          }
98      }
99  
100     /**
101      * We cannot reproduce situation when visitToken is called and leaveToken is not.
102      * So, we have to use reflection to be sure that even in such situation
103      * state of the field will be cleared.
104      *
105      * @throws Exception when code tested throws exception
106      */
107     @Test
108     @SuppressWarnings("unchecked")
109     public void testClearState() throws Exception {
110         final ParameterAssignmentCheck check = new ParameterAssignmentCheck();
111         final Optional<DetailAST> methodDef = TestUtil.findTokenInAstByPredicate(
112             TestUtil.parseFile(new File(getPath("InputParameterAssignmentReceiver.java"))),
113             ast -> ast.getType() == TokenTypes.METHOD_DEF);
114 
115         assertTrue("Ast should contain METHOD_DEF", methodDef.isPresent());
116         assertTrue("State is not cleared on beginTree",
117             TestUtil.isStatefulFieldClearedDuringBeginTree(check, methodDef.get(),
118                 "parameterNamesStack",
119                 parameterNamesStack -> ((Collection<Set<String>>) parameterNamesStack).isEmpty()));
120     }
121 }