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.AbstractParenPadCheck.MSG_WS_FOLLOWED;
23  import static com.puppycrawl.tools.checkstyle.checks.whitespace.AbstractParenPadCheck.MSG_WS_NOT_FOLLOWED;
24  import static com.puppycrawl.tools.checkstyle.checks.whitespace.AbstractParenPadCheck.MSG_WS_NOT_PRECEDED;
25  import static com.puppycrawl.tools.checkstyle.checks.whitespace.AbstractParenPadCheck.MSG_WS_PRECEDED;
26  
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
31  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
34  
35  public class TypecastParenPadCheckTest
36      extends AbstractModuleTestSupport {
37      @Override
38      protected String getPackageLocation() {
39          return "com/puppycrawl/tools/checkstyle/checks/whitespace/typecastparenpad";
40      }
41  
42      @Test
43      public void testDefault()
44              throws Exception {
45          final DefaultConfiguration checkConfig =
46              createModuleConfig(TypecastParenPadCheck.class);
47          final String[] expected = {
48              "89:14: " + getCheckMessage(MSG_WS_FOLLOWED, "("),
49              "89:21: " + getCheckMessage(MSG_WS_PRECEDED, ")"),
50          };
51          verify(checkConfig, getPath("InputTypecastParenPadWhitespace.java"), expected);
52      }
53  
54      @Test
55      public void testSpace()
56              throws Exception {
57          final DefaultConfiguration checkConfig =
58              createModuleConfig(TypecastParenPadCheck.class);
59          checkConfig.addAttribute("option", PadOption.SPACE.toString());
60          final String[] expected = {
61              "87:21: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
62              "87:27: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
63              "88:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
64              "88:20: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
65              "90:14: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
66              "90:20: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
67              "241:18: " + getCheckMessage(MSG_WS_NOT_FOLLOWED, "("),
68              "241:21: " + getCheckMessage(MSG_WS_NOT_PRECEDED, ")"),
69          };
70          verify(checkConfig, getPath("InputTypecastParenPadWhitespace.java"), expected);
71      }
72  
73      @Test
74      public void test1322879() throws Exception {
75          final DefaultConfiguration checkConfig =
76              createModuleConfig(TypecastParenPadCheck.class);
77          checkConfig.addAttribute("option", PadOption.SPACE.toString());
78          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
79          verify(checkConfig, getPath("InputTypecastParenPadWhitespaceAround.java"),
80                 expected);
81      }
82  
83      @Test
84      public void testGetAcceptableTokens() {
85          final TypecastParenPadCheck typecastParenPadCheckObj = new TypecastParenPadCheck();
86          final int[] actual = typecastParenPadCheckObj.getAcceptableTokens();
87          final int[] expected = {
88              TokenTypes.RPAREN,
89              TokenTypes.TYPECAST,
90          };
91          Assert.assertArrayEquals("Invalid acceptable tokens", expected, actual);
92      }
93  }