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;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.FinalParametersCheck.MSG_KEY;
23  
24  import org.junit.Test;
25  
26  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
27  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
28  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
29  
30  public class FinalParametersCheckTest extends AbstractModuleTestSupport {
31      @Override
32      protected String getPackageLocation() {
33          return "com/puppycrawl/tools/checkstyle/checks/finalparameters";
34      }
35  
36      @Test
37      public void testDefaultTokens() throws Exception {
38          final DefaultConfiguration checkConfig =
39              createModuleConfig(FinalParametersCheck.class);
40          final String[] expected = {
41              "23:26: " + getCheckMessage(MSG_KEY, "s"),
42              "38:26: " + getCheckMessage(MSG_KEY, "i"),
43              "43:26: " + getCheckMessage(MSG_KEY, "s"),
44              "53:17: " + getCheckMessage(MSG_KEY, "s"),
45              "69:17: " + getCheckMessage(MSG_KEY, "s"),
46              "75:17: " + getCheckMessage(MSG_KEY, "s"),
47              "90:45: " + getCheckMessage(MSG_KEY, "e"),
48              "93:36: " + getCheckMessage(MSG_KEY, "e"),
49              "110:18: " + getCheckMessage(MSG_KEY, "aParam"),
50              "113:18: " + getCheckMessage(MSG_KEY, "args"),
51              "116:18: " + getCheckMessage(MSG_KEY, "args"),
52          };
53          verify(checkConfig, getPath("InputFinalParameters.java"), expected);
54      }
55  
56      @Test
57      public void testCtorToken() throws Exception {
58          final DefaultConfiguration checkConfig =
59              createModuleConfig(FinalParametersCheck.class);
60          checkConfig.addAttribute("tokens", "CTOR_DEF");
61          final String[] expected = {
62              "23:26: " + getCheckMessage(MSG_KEY, "s"),
63              "38:26: " + getCheckMessage(MSG_KEY, "i"),
64              "43:26: " + getCheckMessage(MSG_KEY, "s"),
65          };
66          verify(checkConfig, getPath("InputFinalParameters.java"), expected);
67      }
68  
69      @Test
70      public void testMethodToken() throws Exception {
71          final DefaultConfiguration checkConfig =
72              createModuleConfig(FinalParametersCheck.class);
73          checkConfig.addAttribute("tokens", "METHOD_DEF");
74          final String[] expected = {
75              "53:17: " + getCheckMessage(MSG_KEY, "s"),
76              "69:17: " + getCheckMessage(MSG_KEY, "s"),
77              "75:17: " + getCheckMessage(MSG_KEY, "s"),
78              "90:45: " + getCheckMessage(MSG_KEY, "e"),
79              "93:36: " + getCheckMessage(MSG_KEY, "e"),
80              "110:18: " + getCheckMessage(MSG_KEY, "aParam"),
81              "113:18: " + getCheckMessage(MSG_KEY, "args"),
82              "116:18: " + getCheckMessage(MSG_KEY, "args"),
83          };
84          verify(checkConfig, getPath("InputFinalParameters.java"), expected);
85      }
86  
87      @Test
88      public void testCatchToken() throws Exception {
89          final DefaultConfiguration checkConfig =
90              createModuleConfig(FinalParametersCheck.class);
91          checkConfig.addAttribute("tokens", "LITERAL_CATCH");
92          final String[] expected = {
93              "125:16: " + getCheckMessage(MSG_KEY, "npe"),
94              "131:16: " + getCheckMessage(MSG_KEY, "e"),
95              "134:16: " + getCheckMessage(MSG_KEY, "e"),
96          };
97          verify(checkConfig, getPath("InputFinalParameters.java"), expected);
98      }
99  
100     @Test
101     public void testForEachClauseToken() throws Exception {
102         final DefaultConfiguration checkConfig =
103             createModuleConfig(FinalParametersCheck.class);
104         checkConfig.addAttribute("tokens", "FOR_EACH_CLAUSE");
105         final String[] expected = {
106             "152:13: " + getCheckMessage(MSG_KEY, "s"),
107             "160:13: " + getCheckMessage(MSG_KEY, "s"),
108         };
109         verify(checkConfig, getPath("InputFinalParameters.java"), expected);
110     }
111 
112     @Test
113     public void testIgnorePrimitiveTypesParameters() throws Exception {
114         final DefaultConfiguration checkConfig =
115             createModuleConfig(FinalParametersCheck.class);
116         checkConfig.addAttribute("ignorePrimitiveTypes", "true");
117         final String[] expected = {
118             "6:22: " + getCheckMessage(MSG_KEY, "k"),
119             "7:15: " + getCheckMessage(MSG_KEY, "s"),
120             "7:25: " + getCheckMessage(MSG_KEY, "o"),
121             "8:15: " + getCheckMessage(MSG_KEY, "array"),
122             "9:31: " + getCheckMessage(MSG_KEY, "s"),
123             "10:22: " + getCheckMessage(MSG_KEY, "l"),
124             "10:32: " + getCheckMessage(MSG_KEY, "s"),
125         };
126         verify(checkConfig, getPath("InputFinalParametersPrimitiveTypes.java"), expected);
127     }
128 
129     @Test
130     public void testReceiverParameters() throws Exception {
131         final DefaultConfiguration checkConfig =
132             createModuleConfig(FinalParametersCheck.class);
133         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
134         verify(checkConfig, getPath("InputFinalParametersReceiver.java"), expected);
135     }
136 }