View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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      public 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              "14:59: " + getCheckMessage(MSG_KEY),
44              "88: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              "89: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             "87:44: " + getCheckMessage(MSG_KEY),
106             "99: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             "46:54: " + getCheckMessage(MSG_KEY),
118             "48:54: " + getCheckMessage(MSG_KEY),
119             "48:70: " + getCheckMessage(MSG_KEY),
120             "54:46: " + getCheckMessage(MSG_KEY),
121             "60: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             "33:42: " + getCheckMessage(MSG_KEY),
132             "38:43: " + getCheckMessage(MSG_KEY),
133             "45:46: " + getCheckMessage(MSG_KEY),
134             "50: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             "5:102: " + getCheckMessage(MSG_KEY),
157             "5:131: " + getCheckMessage(MSG_KEY),
158             "5:165: " + getCheckMessage(MSG_KEY),
159             "5:230: " + getCheckMessage(MSG_KEY),
160             "5:402: " + getCheckMessage(MSG_KEY),
161             "5:414: " + getCheckMessage(MSG_KEY),
162         };
163 
164         verify(checkConfig, getPath("InputOneStatementPerLine.java"),
165                 expected);
166     }
167 
168     @Test
169     public void testStateIsClearedOnBeginTreeForLastStatementEnd() throws Exception {
170         final String inputWithWarnings = getPath("InputOneStatementPerLineBeginTree1.java");
171         final String inputWithoutWarnings = getPath("InputOneStatementPerLineBeginTree2.java");
172         final List<String> expectedFirstInput = List.of(
173             "7:96: " + getCheckMessage(MSG_KEY)
174         );
175         final List<String> expectedSecondInput = List.of(CommonUtil.EMPTY_STRING_ARRAY);
176         verifyWithInlineConfigParser(inputWithWarnings,
177             inputWithoutWarnings, expectedFirstInput, expectedSecondInput);
178     }
179 
180     @Test
181     public void testStateIsClearedOnBeginTreeForLastVariableStatement() throws Exception {
182         final String file1 = getPath(
183                 "InputOneStatementPerLineBeginTreeLastVariableResourcesStatementEnd1.java");
184         final String file2 = getPath(
185                 "InputOneStatementPerLineBeginTreeLastVariableResourcesStatementEnd2.java");
186         final List<String> expectedFirstInput = List.of(
187             "15:59: " + getCheckMessage(MSG_KEY)
188         );
189         final List<String> expectedSecondInput = List.of(CommonUtil.EMPTY_STRING_ARRAY);
190         verifyWithInlineConfigParser(file1, file2, expectedFirstInput, expectedSecondInput);
191     }
192 }