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.rule412nonemptyblocks;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck.MSG_KEY_LINE_ALONE;
23  import static com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck.MSG_KEY_LINE_BREAK_BEFORE;
24  import static com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck.MSG_KEY_LINE_NEW;
25  import static com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck.MSG_KEY_LINE_SAME;
26  
27  import org.junit.Test;
28  
29  import com.google.checkstyle.test.base.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.api.Configuration;
31  import com.puppycrawl.tools.checkstyle.checks.blocks.RightCurlyCheck;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
33  
34  public class RightCurlyTest extends AbstractModuleTestSupport {
35  
36      @Override
37      protected String getPackageLocation() {
38          return "com/google/checkstyle/test/chapter4formatting/rule412nonemptyblocks";
39      }
40  
41      @Test
42      public void testRightCurlyAlone() throws Exception {
43          final String[] expected = {
44              "20:17: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 17),
45              "32:13: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 13),
46              "79:27: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_BREAK_BEFORE, "}", 27),
47          };
48  
49          final Configuration checkConfig = getModuleConfig("RightCurly", "RightCurlySame");
50          final String filePath = getPath("InputRightCurlyOther.java");
51  
52          final Integer[] warnList = getLinesWithWarn(filePath);
53          verify(checkConfig, filePath, expected, warnList);
54      }
55  
56      @Test
57      public void testRightCurlySame() throws Exception {
58          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
59  
60          final Configuration checkConfig = getModuleConfig("RightCurly", "RightCurlySame");
61          final String filePath = getPath("InputRightCurlySame.java");
62  
63          final Integer[] warnList = getLinesWithWarn(filePath);
64          verify(checkConfig, filePath, expected, warnList);
65      }
66  
67      @Test
68      public void testRightCurlySameAndLiteralDoDefault() throws Exception {
69          final String[] expected = {
70              "62:9: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 9),
71              "67:13: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 13),
72              "83:9: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_SAME, "}", 9),
73          };
74  
75          final Configuration checkConfig = getModuleConfig("RightCurly", "RightCurlySame");
76          final String filePath = getPath("InputRightCurlyDoWhile.java");
77  
78          final Integer[] warnList = getLinesWithWarn(filePath);
79          verify(checkConfig, filePath, expected, warnList);
80      }
81  
82      @Test
83      public void testRightCurlyAloneOther() throws Exception {
84          final String[] expected = {
85              "97:5: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 5),
86              "97:6: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_NEW, "}", 6),
87              "108:5: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 5),
88              "108:6: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_NEW, "}", 6),
89              "122:6: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_NEW, "}", 6),
90          };
91  
92          final Configuration checkConfig = getModuleConfig("RightCurly", "RightCurlyAlone");
93          final String filePath = getPath("InputRightCurlyOtherAlone.java");
94  
95          final Integer[] warnList = getLinesWithWarn(filePath);
96          verify(checkConfig, filePath, expected, warnList);
97      }
98  
99      @Test
100     public void testRightCurlyAloneSame() throws Exception {
101         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
102 
103         final Configuration checkConfig = getModuleConfig("RightCurly", "RightCurlyAlone");
104         final String filePath = getPath("InputRightCurlySame.java");
105 
106         final Integer[] warnList = getLinesWithWarn(filePath);
107         verify(checkConfig, filePath, expected, warnList);
108     }
109 
110     @Test
111     public void testRightCurlyAloneSameAndLiteralDo() throws Exception {
112         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
113 
114         final Configuration checkConfig = getModuleConfig("RightCurly", "RightCurlyAlone");
115         final String filePath = getPath("InputRightCurlyDoWhileAlone.java");
116 
117         final Integer[] warnList = getLinesWithWarn(filePath);
118         verify(checkConfig, filePath, expected, warnList);
119     }
120 }