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.modifier;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.modifier.RedundantModifierCheck.MSG_KEY;
23  
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
31  
32  public class RedundantModifierCheckTest
33      extends AbstractModuleTestSupport {
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/modifier/redundantmodifier";
37      }
38  
39      @Test
40      public void testClassesInsideOfInterfaces() throws Exception {
41          final DefaultConfiguration checkConfig =
42              createModuleConfig(RedundantModifierCheck.class);
43          final String[] expected = {
44              "11:5: " + getCheckMessage(MSG_KEY, "static"),
45              "17:5: " + getCheckMessage(MSG_KEY, "public"),
46              "20:5: " + getCheckMessage(MSG_KEY, "public"),
47              "26:5: " + getCheckMessage(MSG_KEY, "static"),
48          };
49          verify(checkConfig, getPath("InputRedundantModifierClassesInsideOfInterfaces.java"),
50              expected);
51      }
52  
53      @Test
54      public void testIt() throws Exception {
55          final DefaultConfiguration checkConfig =
56              createModuleConfig(RedundantModifierCheck.class);
57          final String[] expected = {
58              "54:12: " + getCheckMessage(MSG_KEY, "static"),
59              "57:9: " + getCheckMessage(MSG_KEY, "public"),
60              "63:9: " + getCheckMessage(MSG_KEY, "abstract"),
61              "66:9: " + getCheckMessage(MSG_KEY, "public"),
62              //"69:9: Redundant 'abstract' modifier.",
63              "72:9: " + getCheckMessage(MSG_KEY, "final"),
64              "79:13: " + getCheckMessage(MSG_KEY, "final"),
65              "88:12: " + getCheckMessage(MSG_KEY, "final"),
66              "99:1: " + getCheckMessage(MSG_KEY, "abstract"),
67              "116:5: " + getCheckMessage(MSG_KEY, "public"),
68              "117:5: " + getCheckMessage(MSG_KEY, "final"),
69              "118:5: " + getCheckMessage(MSG_KEY, "static"),
70              "120:5: " + getCheckMessage(MSG_KEY, "public"),
71              "121:5: " + getCheckMessage(MSG_KEY, "abstract"),
72          };
73          verify(checkConfig, getPath("InputRedundantModifierIt.java"), expected);
74      }
75  
76      @Test
77      public void testStaticMethodInInterface()
78              throws Exception {
79          final DefaultConfiguration checkConfig =
80                  createModuleConfig(RedundantModifierCheck.class);
81          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
82          verify(checkConfig, getPath("InputRedundantModifierStaticMethodInInterface.java"),
83              expected);
84      }
85  
86      @Test
87      public void testFinalInInterface()
88              throws Exception {
89          final DefaultConfiguration checkConfig =
90                  createModuleConfig(RedundantModifierCheck.class);
91          final String[] expected = {
92              "5:9: " + getCheckMessage(MSG_KEY, "final"),
93          };
94          verify(checkConfig, getPath("InputRedundantModifierFinalInInterface.java"), expected);
95      }
96  
97      @Test
98      public void testEnumConstructorIsImplicitlyPrivate() throws Exception {
99          final DefaultConfiguration checkConfig =
100                 createModuleConfig(RedundantModifierCheck.class);
101         final String[] expected = {
102             "10:5: " + getCheckMessage(MSG_KEY, "private"),
103         };
104         verify(checkConfig, getPath("InputRedundantModifierConstructorModifier.java"), expected);
105     }
106 
107     @Test
108     public void testInnerTypeInInterfaceIsImplicitlyStatic() throws Exception {
109         final DefaultConfiguration checkConfig =
110                 createModuleConfig(RedundantModifierCheck.class);
111         final String[] expected = {
112             "8:5: " + getCheckMessage(MSG_KEY, "static"),
113             "12:5: " + getCheckMessage(MSG_KEY, "static"),
114         };
115         verify(checkConfig, getPath("InputRedundantModifierStaticInInnerTypeOfInterface.java"),
116             expected);
117     }
118 
119     @Test
120     public void testNotPublicClassConstructorHasNotPublicModifier() throws Exception {
121         final DefaultConfiguration checkConfig =
122                 createModuleConfig(RedundantModifierCheck.class);
123 
124         final String[] expected = {
125             "18:5: " + getCheckMessage(MSG_KEY, "public"),
126         };
127         verify(checkConfig, getPath("InputRedundantModifierPublicModifierInNotPublicClass.java"),
128             expected);
129     }
130 
131     @Test
132     public void testNestedClassConsInPublicInterfaceHasValidPublicModifier() throws Exception {
133         final DefaultConfiguration checkConfig =
134                 createModuleConfig(RedundantModifierCheck.class);
135 
136         final String[] expected = {
137             "18:33: " + getCheckMessage(MSG_KEY, "public"),
138             "22:41: " + getCheckMessage(MSG_KEY, "public"),
139             "33:33: " + getCheckMessage(MSG_KEY, "public"),
140             "41:33: " + getCheckMessage(MSG_KEY, "public"),
141         };
142 
143         verify(checkConfig,
144             getPath("InputRedundantModifierNestedClassInPublicInterfaceRedundantModifiers.java"),
145             expected);
146     }
147 
148     @Test
149     public void testGetAcceptableTokens() {
150         final RedundantModifierCheck redundantModifierCheckObj = new RedundantModifierCheck();
151         final int[] actual = redundantModifierCheckObj.getAcceptableTokens();
152         final int[] expected = {
153             TokenTypes.METHOD_DEF,
154             TokenTypes.VARIABLE_DEF,
155             TokenTypes.ANNOTATION_FIELD_DEF,
156             TokenTypes.INTERFACE_DEF,
157             TokenTypes.CTOR_DEF,
158             TokenTypes.CLASS_DEF,
159             TokenTypes.ENUM_DEF,
160             TokenTypes.RESOURCE,
161         };
162         Assert.assertArrayEquals("Invalid acceptable tokens", expected, actual);
163     }
164 
165     @Test
166     public void testGetRequiredTokens() {
167         final RedundantModifierCheck redundantModifierCheckObj = new RedundantModifierCheck();
168         final int[] actual = redundantModifierCheckObj.getRequiredTokens();
169         final int[] expected = CommonUtils.EMPTY_INT_ARRAY;
170         Assert.assertArrayEquals("Invalid required tokens", expected, actual);
171     }
172 
173     @Test
174     public void testNestedStaticEnum() throws Exception {
175         final DefaultConfiguration checkConfig =
176                 createModuleConfig(RedundantModifierCheck.class);
177         final String[] expected = {
178             "4:5: " + getCheckMessage(MSG_KEY, "static"),
179             "8:9: " + getCheckMessage(MSG_KEY, "static"),
180             "12:9: " + getCheckMessage(MSG_KEY, "static"),
181         };
182         verify(checkConfig, getPath("InputRedundantModifierStaticModifierInNestedEnum.java"),
183             expected);
184     }
185 
186     @Test
187     public void testFinalInAnonymousClass()
188             throws Exception {
189         final DefaultConfiguration checkConfig =
190                 createModuleConfig(RedundantModifierCheck.class);
191         final String[] expected = {
192             "14:20: " + getCheckMessage(MSG_KEY, "final"),
193         };
194         verify(checkConfig, getPath("InputRedundantModifierFinalInAnonymousClass.java"),
195             expected);
196     }
197 
198     @Test
199     public void testFinalInTryWithResource() throws Exception {
200         final DefaultConfiguration checkConfig = createModuleConfig(RedundantModifierCheck.class);
201         final String[] expected = {
202             "22:14: " + getCheckMessage(MSG_KEY, "final"),
203             "27:14: " + getCheckMessage(MSG_KEY, "final"),
204             "28:17: " + getCheckMessage(MSG_KEY, "final"),
205         };
206         verify(checkConfig, getPath("InputRedundantModifierFinalInTryWithResource.java"),
207             expected);
208     }
209 
210     @Test
211     public void testFinalInAbstractMethods() throws Exception {
212         final DefaultConfiguration checkConfig = createModuleConfig(RedundantModifierCheck.class);
213         final String[] expected = {
214             "4:33: " + getCheckMessage(MSG_KEY, "final"),
215             "8:49: " + getCheckMessage(MSG_KEY, "final"),
216             "11:17: " + getCheckMessage(MSG_KEY, "final"),
217             "16:24: " + getCheckMessage(MSG_KEY, "final"),
218             "25:33: " + getCheckMessage(MSG_KEY, "final"),
219         };
220         verify(checkConfig, getPath("InputRedundantModifierFinalInAbstractMethods.java"),
221             expected);
222     }
223 
224     @Test
225     public void testEnumMethods() throws Exception {
226         final DefaultConfiguration checkConfig = createModuleConfig(RedundantModifierCheck.class);
227         final String[] expected = {
228             "7:16: " + getCheckMessage(MSG_KEY, "final"),
229             "22:16: " + getCheckMessage(MSG_KEY, "final"),
230         };
231         verify(checkConfig, getPath("InputRedundantModifierFinalInEnumMethods.java"), expected);
232     }
233 
234     @Test
235     public void testEnumStaticMethodsInPublicClass() throws Exception {
236         final DefaultConfiguration checkConfig = createModuleConfig(RedundantModifierCheck.class);
237         final String[] expected = {
238             "12:23: " + getCheckMessage(MSG_KEY, "final"),
239         };
240         verify(checkConfig,
241             getPath("InputRedundantModifierFinalInEnumStaticMethods.java"), expected);
242     }
243 
244     @Test
245     public void testAnnotationOnEnumConstructor() throws Exception {
246         final DefaultConfiguration checkConfig = createModuleConfig(RedundantModifierCheck.class);
247         final String[] expected = {
248             "14:5: " + getCheckMessage(MSG_KEY, "private"),
249         };
250         verify(checkConfig, getPath("InputRedundantModifierAnnotationOnEnumConstructor.java"),
251                 expected);
252     }
253 }