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.OneStatementPerLineCheck.MSG_KEY;
24  
25  import java.util.List;
26  
27  import org.junit.jupiter.api.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
32  
33  public class OneStatementPerLineCheckTest extends AbstractModuleTestSupport {
34  
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/coding/onestatementperline";
38      }
39  
40      @Test
41      public void testMultiCaseSmallTalkStyle() throws Exception {
42          final String[] expected = {
43              "13:59: " + getCheckMessage(MSG_KEY),
44              "87:21: " + getCheckMessage(MSG_KEY),
45          };
46          verifyWithInlineConfigParser(
47                  getPath("InputOneStatementPerLineSingleLineSmallTalkStyle.java"),
48                  expected);
49      }
50  
51      @Test
52      public void testMultiCaseLoops() throws Exception {
53          final String[] expected = {
54              "27:18: " + getCheckMessage(MSG_KEY),
55              "53:17: " + getCheckMessage(MSG_KEY),
56              "65:25: " + getCheckMessage(MSG_KEY),
57              "85:23: " + getCheckMessage(MSG_KEY),
58              "88:63: " + getCheckMessage(MSG_KEY),
59          };
60          verifyWithInlineConfigParser(
61                  getPath("InputOneStatementPerLineSingleLineInLoops.java"),
62                  expected);
63      }
64  
65      @Test
66      public void testMultiCaseDeclarations() throws Exception {
67          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
68          verifyWithInlineConfigParser(
69                  getPath("InputOneStatementPerLineSingleLineForDeclarations.java"),
70                  expected);
71      }
72  
73      @Test
74      public void testTokensNotNull() {
75          final OneStatementPerLineCheck check = new OneStatementPerLineCheck();
76          assertWithMessage("Acceptable tokens should not be null")
77              .that(check.getAcceptableTokens())
78              .isNotNull();
79          assertWithMessage("Default tokens should not be null")
80              .that(check.getDefaultTokens())
81              .isNotNull();
82          assertWithMessage("Required tokens should not be null")
83              .that(check.getRequiredTokens())
84              .isNotNull();
85      }
86  
87      @Test
88      public void testDeclarationsWithMultilineStatements() throws Exception {
89          final String[] expected = {
90              "49:21: " + getCheckMessage(MSG_KEY),
91              "66:17: " + getCheckMessage(MSG_KEY),
92              "74:17: " + getCheckMessage(MSG_KEY),
93              "86:10: " + getCheckMessage(MSG_KEY),
94              "95:28: " + getCheckMessage(MSG_KEY),
95          };
96          verifyWithInlineConfigParser(
97                  getPath("InputOneStatementPerLineMultilineForDeclarations.java"),
98              expected);
99      }
100 
101     @Test
102     public void testLoopsAndTryWithResourceWithMultilineStatements() throws Exception {
103         final String[] expected = {
104             "53:39: " + getCheckMessage(MSG_KEY),
105             "86:44: " + getCheckMessage(MSG_KEY),
106             "97:45: " + getCheckMessage(MSG_KEY),
107         };
108         verifyWithInlineConfigParser(
109                 getPath("InputOneStatementPerLineMultilineInLoopsAndTryWithResources.java"),
110                 expected);
111     }
112 
113     @Test
114     public void oneStatementNonCompilableInputTest() throws Exception {
115         final String[] expected = {
116             "39:4: " + getCheckMessage(MSG_KEY),
117             "44:54: " + getCheckMessage(MSG_KEY),
118             "45:54: " + getCheckMessage(MSG_KEY),
119             "45:70: " + getCheckMessage(MSG_KEY),
120             "46:46: " + getCheckMessage(MSG_KEY),
121             "50:81: " + getCheckMessage(MSG_KEY),
122         };
123 
124         verifyWithInlineConfigParser(
125                 getNonCompilablePath("InputOneStatementPerLine.java"), expected);
126     }
127 
128     @Test
129     public void testResourceReferenceVariableIgnored() throws Exception {
130         final String[] expected = {
131             "32:42: " + getCheckMessage(MSG_KEY),
132             "36:43: " + getCheckMessage(MSG_KEY),
133             "42:46: " + getCheckMessage(MSG_KEY),
134             "46:46: " + getCheckMessage(MSG_KEY),
135         };
136 
137         verifyWithInlineConfigParser(
138                 getPath("InputOneStatementPerLineTryWithResources.java"),
139                 expected);
140     }
141 
142     @Test
143     public void testResourcesIgnored() throws Exception {
144         final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
145         verifyWithInlineConfigParser(
146                 getPath("InputOneStatementPerLineTryWithResourcesIgnore.java"),
147                 expected);
148     }
149 
150     @Test
151     public void testAllTheCodeInSingleLine() throws Exception {
152         final DefaultConfiguration checkConfig =
153                 createModuleConfig(OneStatementPerLineCheck.class);
154 
155         final String[] expected = {
156             "1:102: " + getCheckMessage(MSG_KEY),
157             "1:131: " + getCheckMessage(MSG_KEY),
158             "1:165: " + getCheckMessage(MSG_KEY),
159             "1:231: " + getCheckMessage(MSG_KEY),
160             "1:406: " + getCheckMessage(MSG_KEY),
161             "1:443: " + getCheckMessage(MSG_KEY),
162             "1:455: " + getCheckMessage(MSG_KEY),
163         };
164 
165         verify(checkConfig, getPath("InputOneStatementPerLine.java"),
166                 expected);
167     }
168 
169     @Test
170     public void testStateIsClearedOnBeginTreeForLastStatementEnd() throws Exception {
171         final String inputWithWarnings = getPath("InputOneStatementPerLineBeginTree1.java");
172         final String inputWithoutWarnings = getPath("InputOneStatementPerLineBeginTree2.java");
173         final List<String> expectedFirstInput = List.of(
174                 "6:96: " + getCheckMessage(MSG_KEY)
175         );
176         final List<String> expectedSecondInput = List.of(CommonUtil.EMPTY_STRING_ARRAY);
177         verifyWithInlineConfigParser(inputWithWarnings,
178             inputWithoutWarnings, expectedFirstInput, expectedSecondInput);
179     }
180 
181     @Test
182     public void testStateIsClearedOnBeginTreeForLastVariableStatement() throws Exception {
183         final String file1 = getPath(
184                 "InputOneStatementPerLineBeginTreeLastVariableResourcesStatementEnd1.java");
185         final String file2 = getPath(
186                 "InputOneStatementPerLineBeginTreeLastVariableResourcesStatementEnd2.java");
187         final List<String> expectedFirstInput = List.of(
188                 "15:59: " + getCheckMessage(MSG_KEY)
189         );
190         final List<String> expectedSecondInput = List.of(CommonUtil.EMPTY_STRING_ARRAY);
191         verifyWithInlineConfigParser(file1, file2, expectedFirstInput, expectedSecondInput);
192     }
193 }