View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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.rule411bracesareused;
21  
22  import org.junit.Test;
23  
24  import com.google.checkstyle.test.base.AbstractModuleTestSupport;
25  import com.puppycrawl.tools.checkstyle.api.Configuration;
26  import com.puppycrawl.tools.checkstyle.checks.blocks.NeedBracesCheck;
27  
28  public class NeedBracesTest extends AbstractModuleTestSupport {
29  
30      @Override
31      protected String getPackageLocation() {
32          return "com/google/checkstyle/test/chapter4formatting/rule411bracesareused";
33      }
34  
35      @Test
36      public void testNeedBraces() throws Exception {
37  
38          final Class<NeedBracesCheck> clazz = NeedBracesCheck.class;
39          final String messageKey = "needBraces";
40  
41          final String[] expected = {
42              "29: " + getCheckMessage(clazz, messageKey, "do"),
43              "41: " + getCheckMessage(clazz, messageKey, "while"),
44              "42: " + getCheckMessage(clazz, messageKey, "while"),
45              "44: " + getCheckMessage(clazz, messageKey, "while"),
46              "45: " + getCheckMessage(clazz, messageKey, "if"),
47              "58: " + getCheckMessage(clazz, messageKey, "for"),
48              "59: " + getCheckMessage(clazz, messageKey, "for"),
49              "61: " + getCheckMessage(clazz, messageKey, "for"),
50              "63: " + getCheckMessage(clazz, messageKey, "if"),
51              "82: " + getCheckMessage(clazz, messageKey, "if"),
52              "83: " + getCheckMessage(clazz, messageKey, "if"),
53              "85: " + getCheckMessage(clazz, messageKey, "if"),
54              "87: " + getCheckMessage(clazz, messageKey, "else"),
55              "89: " + getCheckMessage(clazz, messageKey, "if"),
56              "97: " + getCheckMessage(clazz, messageKey, "else"),
57              "99: " + getCheckMessage(clazz, messageKey, "if"),
58              "100: " + getCheckMessage(clazz, messageKey, "if"),
59              "126: " + getCheckMessage(clazz, messageKey, "while"),
60              "129: " + getCheckMessage(clazz, messageKey, "do"),
61              "135: " + getCheckMessage(clazz, messageKey, "if"),
62              "138: " + getCheckMessage(clazz, messageKey, "if"),
63              "139: " + getCheckMessage(clazz, messageKey, "else"),
64              "144: " + getCheckMessage(clazz, messageKey, "for"),
65              "147: " + getCheckMessage(clazz, messageKey, "for"),
66              "157: " + getCheckMessage(clazz, messageKey, "while"),
67              "160: " + getCheckMessage(clazz, messageKey, "do"),
68              "166: " + getCheckMessage(clazz, messageKey, "if"),
69              "169: " + getCheckMessage(clazz, messageKey, "if"),
70              "170: " + getCheckMessage(clazz, messageKey, "else"),
71              "175: " + getCheckMessage(clazz, messageKey, "for"),
72              "178: " + getCheckMessage(clazz, messageKey, "for"),
73              "189: " + getCheckMessage(clazz, messageKey, "while"),
74              "192: " + getCheckMessage(clazz, messageKey, "do"),
75              "198: " + getCheckMessage(clazz, messageKey, "if"),
76              "201: " + getCheckMessage(clazz, messageKey, "if"),
77              "202: " + getCheckMessage(clazz, messageKey, "else"),
78              "207: " + getCheckMessage(clazz, messageKey, "for"),
79              "210: " + getCheckMessage(clazz, messageKey, "for"),
80          };
81  
82          final Configuration checkConfig = getModuleConfig("NeedBraces");
83          final String filePath = getPath("InputNeedBraces.java");
84  
85          final Integer[] warnList = getLinesWithWarn(filePath);
86          verify(checkConfig, filePath, expected, warnList);
87      }
88  }