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.google.checkstyle.test.chapter4formatting.rule43onestatement;
21  
22  import org.junit.jupiter.api.Test;
23  
24  import com.google.checkstyle.test.base.AbstractGoogleModuleTestSupport;
25  import com.puppycrawl.tools.checkstyle.api.Configuration;
26  import com.puppycrawl.tools.checkstyle.checks.coding.OneStatementPerLineCheck;
27  
28  public class OneStatementPerLineTest extends AbstractGoogleModuleTestSupport {
29  
30      @Override
31      protected String getPackageLocation() {
32          return "com/google/checkstyle/test/chapter4formatting/rule43onestatement";
33      }
34  
35      @Test
36      public void testOneStatement() throws Exception {
37          final String msg = getCheckMessage(OneStatementPerLineCheck.class,
38              "multiple.statements.line");
39  
40          final String[] expected = {
41              "6:59: " + msg,
42              "50:21: " + msg,
43              "52:21: " + msg,
44              "54:42: " + msg,
45              "57:25: " + msg,
46              "58:35: " + msg,
47              "68:14: " + msg,
48              "95:25: " + msg,
49              "97:25: " + msg,
50              "99:46: " + msg,
51              "102:29: " + msg,
52              "103:39: " + msg,
53              "111:15: " + msg,
54              "123:23: " + msg,
55              "138:59: " + msg,
56              "170:19: " + msg,
57              "188:15: " + msg,
58              "196:15: " + msg,
59              "208:6: " + msg,
60              "217:22: " + msg,
61              "307:39: " + msg,
62          };
63  
64          final Configuration checkConfig = getModuleConfig("OneStatementPerLine");
65          final String filePath = getPath("InputOneStatementPerLine.java");
66  
67          final Integer[] warnList = getLinesWithWarn(filePath);
68          verify(checkConfig, filePath, expected, warnList);
69      }
70  
71      @Test
72      public void testOneStatementNonCompilableInput() throws Exception {
73          final String msg = getCheckMessage(OneStatementPerLineCheck.class,
74              "multiple.statements.line");
75  
76          final String[] expected = {
77              "32:6: " + msg,
78              "37:58: " + msg,
79              "38:58: " + msg,
80              "38:74: " + msg,
81              "39:50: " + msg,
82              "43:85: " + msg,
83          };
84  
85          final Configuration checkConfig = getModuleConfig("OneStatementPerLine");
86          final String filePath = getNonCompilablePath("InputOneStatementPerLine.java");
87  
88          final Integer[] warnList = getLinesWithWarn(filePath);
89          verify(checkConfig, filePath, expected, warnList);
90      }
91  
92  }