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