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