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.InnerAssignmentCheck.MSG_KEY;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
29  
30  public class InnerAssignmentCheckTest
31      extends AbstractModuleTestSupport {
32  
33      @Override
34      protected String getPackageLocation() {
35          return "com/puppycrawl/tools/checkstyle/checks/coding/innerassignment";
36      }
37  
38      @Test
39      public void testIt() throws Exception {
40          final String[] expected = {
41              "22:15: " + getCheckMessage(MSG_KEY),
42              "22:19: " + getCheckMessage(MSG_KEY),
43              "24:39: " + getCheckMessage(MSG_KEY),
44              "26:35: " + getCheckMessage(MSG_KEY),
45              "73:19: " + getCheckMessage(MSG_KEY),
46          };
47          verifyWithInlineConfigParser(
48                  getPath("InputInnerAssignment.java"), expected);
49      }
50  
51      @Test
52      public void testMethod() throws Exception {
53          final String[] expected = {
54              "73:22: " + getCheckMessage(MSG_KEY),
55          };
56          verifyWithInlineConfigParser(
57                  getPath("InputInnerAssignmentMethod.java"), expected);
58      }
59  
60      @Test
61      public void testDemoBug1195047Comment3() throws Exception {
62          final String[] expected = {
63              "18:16: " + getCheckMessage(MSG_KEY),
64              "19:24: " + getCheckMessage(MSG_KEY),
65              "20:19: " + getCheckMessage(MSG_KEY),
66              "21:17: " + getCheckMessage(MSG_KEY),
67              "22:29: " + getCheckMessage(MSG_KEY),
68              "23:20: " + getCheckMessage(MSG_KEY),
69              "24:17: " + getCheckMessage(MSG_KEY),
70              "24:31: " + getCheckMessage(MSG_KEY),
71              "24:41: " + getCheckMessage(MSG_KEY),
72              "25:16: " + getCheckMessage(MSG_KEY),
73              "25:27: " + getCheckMessage(MSG_KEY),
74              "26:32: " + getCheckMessage(MSG_KEY),
75          };
76          verifyWithInlineConfigParser(
77                  getPath("InputInnerAssignmentDemoBug1195047Comment3.java"), expected);
78      }
79  
80      @Test
81      public void testLambdaExpression() throws Exception {
82          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
83          verifyWithInlineConfigParser(
84                  getPath("InputInnerAssignmentLambdaExpressions.java"),
85                  expected);
86      }
87  
88      @Test
89      public void testInnerAssignmentNotInLoopContext() throws Exception {
90          final String[] expected = {
91              "12:28: " + getCheckMessage(MSG_KEY),
92          };
93          verifyWithInlineConfigParser(
94                  getPath("InputInnerAssignmentNotInLoopContext.java"),
95                  expected);
96      }
97  
98      @Test
99      public void testTokensNotNull() {
100         final InnerAssignmentCheck check = new InnerAssignmentCheck();
101         assertWithMessage("Acceptable tokens should not be null")
102             .that(check.getAcceptableTokens())
103             .isNotNull();
104         assertWithMessage("Default tokens should not be null")
105             .that(check.getDefaultTokens())
106             .isNotNull();
107         assertWithMessage("Required tokens should not be null")
108             .that(check.getRequiredTokens())
109             .isNotNull();
110     }
111 
112 }