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.ParameterAssignmentCheck.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 ParameterAssignmentCheckTest extends AbstractModuleTestSupport {
40  
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 String[] expected = {
50              "17:15: " + getCheckMessage(MSG_KEY, "field"),
51              "18:15: " + getCheckMessage(MSG_KEY, "field"),
52              "20:14: " + getCheckMessage(MSG_KEY, "field"),
53              "28:30: " + getCheckMessage(MSG_KEY, "field1"),
54              "45:31: " + getCheckMessage(MSG_KEY, "q"),
55              "46:39: " + getCheckMessage(MSG_KEY, "q"),
56              "47:34: " + getCheckMessage(MSG_KEY, "w"),
57              "49:41: " + getCheckMessage(MSG_KEY, "w"),
58              "50:49: " + getCheckMessage(MSG_KEY, "a"),
59              "52:11: " + getCheckMessage(MSG_KEY, "c"),
60              "53:11: " + getCheckMessage(MSG_KEY, "d"),
61              "63:15: " + getCheckMessage(MSG_KEY, "d"),
62          };
63          verifyWithInlineConfigParser(
64                  getPath("InputParameterAssignmentWithUnchecked.java"),
65                 expected);
66      }
67  
68      @Test
69      public void testReceiverParameter() throws Exception {
70          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
71          verifyWithInlineConfigParser(
72                  getPath("InputParameterAssignmentReceiver.java"), expected);
73      }
74  
75      @Test
76      public void testEnhancedSwitch() throws Exception {
77          final String[] expected = {
78              "14:28: " + getCheckMessage(MSG_KEY, "a"),
79              "21:16: " + getCheckMessage(MSG_KEY, "result"),
80          };
81          verifyWithInlineConfigParser(
82                  getNonCompilablePath("InputParameterAssignmentWithEnhancedSwitch.java"),
83                  expected);
84      }
85  
86      @Test
87      public void testTokensNotNull() {
88          final ParameterAssignmentCheck check = new ParameterAssignmentCheck();
89          assertWithMessage("Acceptable tokens should not be null")
90              .that(check.getAcceptableTokens())
91              .isNotNull();
92          assertWithMessage("Default tokens should not be null")
93              .that(check.getDefaultTokens())
94              .isNotNull();
95          assertWithMessage("Required tokens should not be null")
96              .that(check.getRequiredTokens())
97              .isNotNull();
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             JavaParser.parseFile(new File(getPath("InputParameterAssignmentReceiver.java")),
113                 JavaParser.Options.WITHOUT_COMMENTS),
114             ast -> ast.getType() == TokenTypes.METHOD_DEF);
115 
116         assertWithMessage("Ast should contain METHOD_DEF")
117                 .that(methodDef.isPresent())
118                 .isTrue();
119         assertWithMessage("State is not cleared on beginTree")
120             .that(TestUtil.isStatefulFieldClearedDuringBeginTree(check, methodDef.get(),
121                 "parameterNamesStack",
122                 parameterNamesStack -> ((Collection<Set<String>>) parameterNamesStack).isEmpty()))
123             .isTrue();
124     }
125 
126 }