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.coding;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_ASSIGN;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_EXPR;
24  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_IDENT;
25  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_LAMBDA;
26  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_LITERAL;
27  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_RETURN;
28  import static com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck.MSG_STRING;
29  
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
34  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
35  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
36  
37  /**
38   * Test fixture for the UnnecessaryParenthesesCheck.
39   *
40   * @author  Eric K. Roe
41   */
42  public class UnnecessaryParenthesesCheckTest extends AbstractModuleTestSupport {
43      @Override
44      protected String getPackageLocation() {
45          return "com/puppycrawl/tools/checkstyle/checks/coding/unnecessaryparentheses";
46      }
47  
48      @Test
49      public void testDefault() throws Exception {
50          final DefaultConfiguration checkConfig =
51              createModuleConfig(UnnecessaryParenthesesCheck.class);
52  
53          final String[] expected = {
54              "5:22: " + getCheckMessage(MSG_ASSIGN),
55              "5:29: " + getCheckMessage(MSG_EXPR),
56              "5:31: " + getCheckMessage(MSG_IDENT, "i"),
57              "5:46: " + getCheckMessage(MSG_ASSIGN),
58              "6:15: " + getCheckMessage(MSG_ASSIGN),
59              "7:14: " + getCheckMessage(MSG_IDENT, "x"),
60              "7:17: " + getCheckMessage(MSG_ASSIGN),
61              "8:15: " + getCheckMessage(MSG_ASSIGN),
62              "9:14: " + getCheckMessage(MSG_IDENT, "x"),
63              "9:17: " + getCheckMessage(MSG_ASSIGN),
64              "12:22: " + getCheckMessage(MSG_ASSIGN),
65              "12:30: " + getCheckMessage(MSG_IDENT, "i"),
66              "12:46: " + getCheckMessage(MSG_ASSIGN),
67              "16:17: " + getCheckMessage(MSG_LITERAL, "0"),
68              "26:11: " + getCheckMessage(MSG_ASSIGN),
69              "30:11: " + getCheckMessage(MSG_ASSIGN),
70              "32:11: " + getCheckMessage(MSG_ASSIGN),
71              "34:11: " + getCheckMessage(MSG_ASSIGN),
72              "35:16: " + getCheckMessage(MSG_IDENT, "a"),
73              "36:14: " + getCheckMessage(MSG_IDENT, "a"),
74              "36:20: " + getCheckMessage(MSG_IDENT, "b"),
75              "36:26: " + getCheckMessage(MSG_LITERAL, "600"),
76              "36:40: " + getCheckMessage(MSG_LITERAL, "12.5f"),
77              "36:56: " + getCheckMessage(MSG_IDENT, "arg2"),
78              "37:14: " + getCheckMessage(MSG_STRING, "\"this\""),
79              "37:25: " + getCheckMessage(MSG_STRING, "\"that\""),
80              "38:11: " + getCheckMessage(MSG_ASSIGN),
81              "38:14: " + getCheckMessage(MSG_STRING, "\"this is a really, really...\""),
82              "40:16: " + getCheckMessage(MSG_RETURN),
83              "44:21: " + getCheckMessage(MSG_LITERAL, "1"),
84              "44:26: " + getCheckMessage(MSG_LITERAL, "13.5"),
85              "45:22: " + getCheckMessage(MSG_LITERAL, "true"),
86              "46:17: " + getCheckMessage(MSG_IDENT, "b"),
87              "50:17: " + getCheckMessage(MSG_ASSIGN),
88              "52:11: " + getCheckMessage(MSG_ASSIGN),
89              "54:16: " + getCheckMessage(MSG_RETURN),
90              "64:13: " + getCheckMessage(MSG_EXPR),
91              "68:16: " + getCheckMessage(MSG_EXPR),
92              "73:19: " + getCheckMessage(MSG_EXPR),
93              "74:23: " + getCheckMessage(MSG_LITERAL, "4000"),
94              "79:19: " + getCheckMessage(MSG_ASSIGN),
95              "81:11: " + getCheckMessage(MSG_ASSIGN),
96              "81:16: " + getCheckMessage(MSG_LITERAL, "3"),
97              "82:39: " + getCheckMessage(MSG_ASSIGN),
98              "93:11: " + getCheckMessage(MSG_ASSIGN),
99              "93:14: " + getCheckMessage(MSG_STRING, "\"12345678901234567890123\""),
100         };
101 
102         verify(checkConfig, getPath("InputUnnecessaryParenthesesOperatorsAndCasts.java"), expected);
103     }
104 
105     @Test
106     public void test15Extensions() throws Exception {
107         final DefaultConfiguration checkConfig =
108             createModuleConfig(UnnecessaryParenthesesCheck.class);
109         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
110         verify(checkConfig, getPath("InputUnnecessaryParentheses15Extensions.java"), expected);
111     }
112 
113     @Test
114     public void testLambdas() throws Exception {
115         final DefaultConfiguration checkConfig =
116             createModuleConfig(UnnecessaryParenthesesCheck.class);
117         checkConfig.addAttribute("tokens", "LAMBDA");
118         final String[] expected = {
119             "10:35: " + getCheckMessage(MSG_LAMBDA),
120             "11:35: " + getCheckMessage(MSG_LAMBDA),
121             "18:18: " + getCheckMessage(MSG_LAMBDA),
122             "19:57: " + getCheckMessage(MSG_LAMBDA),
123             "38:25: " + getCheckMessage(MSG_LAMBDA),
124             "38:33: " + getCheckMessage(MSG_LAMBDA),
125             "41:25: " + getCheckMessage(MSG_LAMBDA),
126             "44:31: " + getCheckMessage(MSG_LAMBDA),
127         };
128         verify(checkConfig, getPath("InputUnnecessaryParenthesesLambdas.java"), expected);
129     }
130 
131     @Test
132     public void testTokensNotNull() {
133         final UnnecessaryParenthesesCheck check = new UnnecessaryParenthesesCheck();
134         Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
135         Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
136         Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
137     }
138 }