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