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