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.puppycrawl.tools.checkstyle.checks.whitespace;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck.MSG_LINE_PREVIOUS;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck.MSG_WS_NOT_PRECEDED;
24  import static com.puppycrawl.tools.checkstyle.checks.whitespace.MethodParamPadCheck.MSG_WS_PRECEDED;
25  import static org.junit.Assert.assertArrayEquals;
26  import static org.junit.Assert.assertTrue;
27  import static org.junit.Assert.fail;
28  
29  import org.junit.Test;
30  
31  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
33  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
34  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
36  
37  public class MethodParamPadCheckTest
38      extends AbstractModuleTestSupport {
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/whitespace/methodparampad";
42      }
43  
44      @Test
45      public void testGetRequiredTokens() {
46          final MethodParamPadCheck checkObj = new MethodParamPadCheck();
47          assertArrayEquals(
48              "MethodParamPadCheck#getRequiredTockens should return empty array by default",
49              CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
50      }
51  
52      @Test
53      public void testDefault() throws Exception {
54          final DefaultConfiguration checkConfig = createModuleConfig(MethodParamPadCheck.class);
55          final String[] expected = {
56              "11:32: " + getCheckMessage(MSG_WS_PRECEDED, "("),
57              "13:15: " + getCheckMessage(MSG_WS_PRECEDED, "("),
58              "17:9: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
59              "20:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
60              "27:24: " + getCheckMessage(MSG_WS_PRECEDED, "("),
61              "32:9: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
62              "36:39: " + getCheckMessage(MSG_WS_PRECEDED, "("),
63              "38:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
64              "42:16: " + getCheckMessage(MSG_WS_PRECEDED, "("),
65              "44:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
66              "50:21: " + getCheckMessage(MSG_WS_PRECEDED, "("),
67              "52:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
68              "56:18: " + getCheckMessage(MSG_WS_PRECEDED, "("),
69              "58:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
70              "61:36: " + getCheckMessage(MSG_WS_PRECEDED, "("),
71              "63:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
72              "74:15: " + getCheckMessage(MSG_WS_PRECEDED, "("),
73              "79:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
74          };
75          verify(checkConfig, getPath("InputMethodParamPad.java"), expected);
76      }
77  
78      @Test
79      public void testAllowLineBreaks() throws Exception {
80          final DefaultConfiguration checkConfig = createModuleConfig(MethodParamPadCheck.class);
81          checkConfig.addAttribute("allowLineBreaks", "true");
82          final String[] expected = {
83              "11:32: " + getCheckMessage(MSG_WS_PRECEDED, "("),
84              "13:15: " + getCheckMessage(MSG_WS_PRECEDED, "("),
85              "27:24: " + getCheckMessage(MSG_WS_PRECEDED, "("),
86              "36:39: " + getCheckMessage(MSG_WS_PRECEDED, "("),
87              "42:16: " + getCheckMessage(MSG_WS_PRECEDED, "("),
88              "50:21: " + getCheckMessage(MSG_WS_PRECEDED, "("),
89              "56:18: " + getCheckMessage(MSG_WS_PRECEDED, "("),
90              "61:36: " + getCheckMessage(MSG_WS_PRECEDED, "("),
91              "74:15: " + getCheckMessage(MSG_WS_PRECEDED, "("),
92          };
93          verify(checkConfig, getPath("InputMethodParamPad.java"), expected);
94      }
95  
96      @Test
97      public void testSpaceOption() throws Exception {
98          final DefaultConfiguration checkConfig = createModuleConfig(MethodParamPadCheck.class);
99          checkConfig.addAttribute("option", "space");
100         final String[] expected = {
101             "6:31: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
102             "8:14: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
103             "17:9: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
104             "20:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
105             "23:23: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
106             "32:9: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
107             "35:58: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
108             "38:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
109             "41:15: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
110             "44:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
111             "47:28: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
112             "49:20: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
113             "52:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
114             "54:56: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
115             "55:17: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
116             "58:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
117             "60:35: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
118             "63:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
119             "66:25: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
120             "69:66: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
121             "70:57: " + getCheckMessage(MSG_WS_NOT_PRECEDED, "("),
122             "79:13: " + getCheckMessage(MSG_LINE_PREVIOUS, "("),
123         };
124         verify(checkConfig, getPath("InputMethodParamPad.java"), expected);
125     }
126 
127     @Test
128     public void test1322879() throws Exception {
129         final DefaultConfiguration checkConfig = createModuleConfig(MethodParamPadCheck.class);
130         checkConfig.addAttribute("option", PadOption.SPACE.toString());
131         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
132         verify(checkConfig, getPath("InputMethodParamPadWhitespaceAround.java"),
133                expected);
134     }
135 
136     @Test
137     public void testGetAcceptableTokens() {
138         final MethodParamPadCheck methodParamPadCheckObj = new MethodParamPadCheck();
139         final int[] actual = methodParamPadCheckObj.getAcceptableTokens();
140         final int[] expected = {
141             TokenTypes.CTOR_DEF,
142             TokenTypes.LITERAL_NEW,
143             TokenTypes.METHOD_CALL,
144             TokenTypes.METHOD_DEF,
145             TokenTypes.SUPER_CTOR_CALL,
146             TokenTypes.ENUM_CONSTANT_DEF,
147         };
148         assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
149     }
150 
151     @Test
152     public void testInvalidOption() throws Exception {
153         final DefaultConfiguration checkConfig = createModuleConfig(MethodParamPadCheck.class);
154         checkConfig.addAttribute("option", "invalid_option");
155 
156         try {
157             final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
158 
159             verify(checkConfig, getPath("InputMethodParamPad.java"), expected);
160             fail("exception expected");
161         }
162         catch (CheckstyleException ex) {
163             final String messageStart = "cannot initialize module "
164                 + "com.puppycrawl.tools.checkstyle.TreeWalker - Cannot set property 'option' to "
165                 + "'invalid_option' in module";
166             assertTrue("Invalid exception message, should start with: " + messageStart,
167                 ex.getMessage().startsWith(messageStart));
168         }
169     }
170 }