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.utils;
21  
22  import static com.puppycrawl.tools.checkstyle.internal.utils.TestUtil.isUtilsClassHasPrivateConstructor;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertTrue;
27  
28  import java.io.File;
29  import java.util.List;
30  
31  import org.junit.Test;
32  
33  import com.puppycrawl.tools.checkstyle.DefaultLogger;
34  import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent;
35  import com.puppycrawl.tools.checkstyle.TreeWalkerFilter;
36  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
37  import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
38  import com.puppycrawl.tools.checkstyle.api.AuditEvent;
39  import com.puppycrawl.tools.checkstyle.api.AuditListener;
40  import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
41  import com.puppycrawl.tools.checkstyle.api.BeforeExecutionFileFilter;
42  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
43  import com.puppycrawl.tools.checkstyle.api.FileText;
44  import com.puppycrawl.tools.checkstyle.api.Filter;
45  import com.puppycrawl.tools.checkstyle.api.RootModule;
46  
47  public class ModuleReflectionUtilsTest {
48  
49      @Test
50      public void testIsProperUtilsClass() throws ReflectiveOperationException {
51          assertTrue("Constructor is not private",
52                  isUtilsClassHasPrivateConstructor(ModuleReflectionUtils.class, true));
53      }
54  
55      @Test
56      public void testIsCheckstyleModule() {
57          assertTrue("Should return true when checkstyle module is passed",
58                  ModuleReflectionUtils.isCheckstyleModule(CheckClass.class));
59          assertTrue("Should return true when checkstyle module is passed",
60                  ModuleReflectionUtils.isCheckstyleModule(FileSetModuleClass.class));
61          assertTrue("Should return true when checkstyle module is passed",
62                  ModuleReflectionUtils.isCheckstyleModule(FilterClass.class));
63          assertTrue("Should return true when checkstyle module is passed",
64                  ModuleReflectionUtils.isCheckstyleModule(TreeWalkerFilterClass.class));
65          assertTrue("Should return true when checkstyle module is passed",
66                  ModuleReflectionUtils.isCheckstyleModule(FileFilterModuleClass.class));
67          assertTrue("Should return true when checkstyle module is passed",
68                  ModuleReflectionUtils.isCheckstyleModule(AuditListenerClass.class));
69          assertTrue("Should return true when checkstyle module is passed",
70                  ModuleReflectionUtils.isCheckstyleModule(RootModuleClass.class));
71      }
72  
73      @Test
74      public void testIsValidCheckstyleClass() {
75          assertTrue("Should return true when valid checkstyle class is passed",
76                  ModuleReflectionUtils.isValidCheckstyleClass(ValidCheckstyleClass.class));
77          assertFalse("Should return false when invalid class is passed",
78                  ModuleReflectionUtils
79                  .isValidCheckstyleClass(InvalidNonAutomaticBeanClass.class));
80          assertFalse("Should return false when invalid class is passed",
81                  ModuleReflectionUtils.isValidCheckstyleClass(AbstractInvalidClass.class));
82          assertFalse("Should return false when invalid class is passed",
83                  ModuleReflectionUtils
84                          .isValidCheckstyleClass(InvalidNonDefaultConstructorClass.class));
85      }
86  
87      @Test
88      public void testIsCheckstyleCheck() {
89          assertTrue("Should return true when valid checkstyle check is passed",
90                  ModuleReflectionUtils.isCheckstyleTreeWalkerCheck(CheckClass.class));
91          assertFalse("Should return false when invalid class is passed",
92                  ModuleReflectionUtils.isCheckstyleTreeWalkerCheck(NotCheckstyleCheck.class));
93      }
94  
95      @Test
96      public void testIsFileSetModule() {
97          assertTrue("Should return true when valid checkstyle file set module is passed",
98                  ModuleReflectionUtils.isFileSetModule(FileSetModuleClass.class));
99          assertFalse("Should return false when invalid class is passed",
100                 ModuleReflectionUtils.isFileSetModule(NotCheckstyleCheck.class));
101     }
102 
103     @Test
104     public void testIsFilterModule() {
105         assertTrue("Should return true when valid checkstyle filter module is passed",
106                 ModuleReflectionUtils.isFilterModule(FilterClass.class));
107         assertFalse("Should return false when invalid class is passed",
108                 ModuleReflectionUtils.isFilterModule(NotCheckstyleCheck.class));
109     }
110 
111     @Test
112     public void testIsFileFilterModule() {
113         assertTrue("Should return true when valid checkstyle file filter module is passed",
114                 ModuleReflectionUtils.isFileFilterModule(FileFilterModuleClass.class));
115         assertFalse("Should return false when invalid class is passed",
116                 ModuleReflectionUtils.isFileFilterModule(NotCheckstyleCheck.class));
117     }
118 
119     @Test
120     public void testIsTreeWalkerFilterModule() {
121         assertTrue("Should return true when valid checkstyle TreeWalker filter module is passed",
122                 ModuleReflectionUtils.isTreeWalkerFilterModule(TreeWalkerFilterClass.class));
123         assertFalse("Should return false when invalid class is passed",
124                 ModuleReflectionUtils.isTreeWalkerFilterModule(NotCheckstyleCheck.class));
125     }
126 
127     @Test
128     public void testIsAuditListener() {
129         assertTrue("Should return true when valid checkstyle AuditListener module is passed",
130                 ModuleReflectionUtils.isAuditListener(DefaultLogger.class));
131         assertFalse("Should return false when invalid class is passed",
132                 ModuleReflectionUtils.isAuditListener(NotCheckstyleCheck.class));
133     }
134 
135     @Test
136     public void testIsRootModule() {
137         assertTrue("Should return true when valid checkstyle root module is passed",
138                 ModuleReflectionUtils.isRootModule(RootModuleClass.class));
139         assertFalse("Should return false when invalid class is passed",
140                 ModuleReflectionUtils.isRootModule(NotCheckstyleCheck.class));
141     }
142 
143     @Test
144     public void testKeepEclipseHappy() {
145         final InvalidNonDefaultConstructorClass test = new InvalidNonDefaultConstructorClass(0);
146         assertNotNull("should use constructor", test);
147         assertEquals("should use field", 1, test.getField());
148     }
149 
150     private static class ValidCheckstyleClass extends AutomaticBean {
151         // empty, use default constructor
152 
153         @Override
154         protected void finishLocalSetup() throws CheckstyleException {
155             //dummy method
156         }
157     }
158 
159     private static class InvalidNonAutomaticBeanClass {
160         // empty, use default constructor
161     }
162 
163     /**
164      * AbstractInvalidClass.
165      * @noinspection AbstractClassNeverImplemented
166      */
167     private abstract static class AbstractInvalidClass extends AutomaticBean {
168         public abstract void method();
169     }
170 
171     private static class CheckClass extends AbstractCheck {
172         @Override
173         public int[] getDefaultTokens() {
174             return new int[] {0};
175         }
176 
177         @Override
178         public int[] getAcceptableTokens() {
179             return getDefaultTokens();
180         }
181 
182         @Override
183         public int[] getRequiredTokens() {
184             return getDefaultTokens();
185         }
186     }
187 
188     private static class FileSetModuleClass extends AbstractFileSetCheck {
189         @Override
190         protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
191             //dummy method
192         }
193     }
194 
195     private static class FilterClass extends AutomaticBean implements Filter {
196         @Override
197         protected void finishLocalSetup() throws CheckstyleException {
198             //dummy method
199         }
200 
201         @Override
202         public boolean accept(AuditEvent event) {
203             return false;
204         }
205     }
206 
207     private static class FileFilterModuleClass extends AutomaticBean
208             implements BeforeExecutionFileFilter {
209         @Override
210         protected void finishLocalSetup() throws CheckstyleException {
211             //dummy method
212         }
213 
214         @Override
215         public boolean accept(String uri) {
216             return false;
217         }
218     }
219 
220     private static class RootModuleClass extends AutomaticBean implements RootModule {
221         @Override
222         protected void finishLocalSetup() throws CheckstyleException {
223             //dummy method
224         }
225 
226         @Override
227         public void addListener(AuditListener listener) {
228             //dummy method
229         }
230 
231         @Override
232         public int process(List<File> files) throws CheckstyleException {
233             return 0;
234         }
235 
236         @Override
237         public void destroy() {
238             //dummy method
239         }
240 
241         @Override
242         public void setModuleClassLoader(ClassLoader moduleClassLoader) {
243             //dummy method
244         }
245     }
246 
247     private static class TreeWalkerFilterClass extends AutomaticBean implements TreeWalkerFilter {
248         @Override
249         protected void finishLocalSetup() throws CheckstyleException {
250             //dummy method
251         }
252 
253         @Override
254         public boolean accept(TreeWalkerAuditEvent treeWalkerAuditEvent) {
255             return false;
256         }
257     }
258 
259     private static class AuditListenerClass extends AutomaticBean implements AuditListener {
260 
261         @Override
262         protected void finishLocalSetup() throws CheckstyleException {
263             //dummy method
264         }
265 
266         @Override
267         public void auditStarted(AuditEvent event) {
268             //dummy method
269         }
270 
271         @Override
272         public void auditFinished(AuditEvent event) {
273             //dummy method
274         }
275 
276         @Override
277         public void fileStarted(AuditEvent event) {
278             //dummy method
279         }
280 
281         @Override
282         public void fileFinished(AuditEvent event) {
283             //dummy method
284         }
285 
286         @Override
287         public void addError(AuditEvent event) {
288             //dummy method
289         }
290 
291         @Override
292         public void addException(AuditEvent event, Throwable throwable) {
293             //dummy method
294         }
295     }
296 
297     private static class NotCheckstyleCheck {
298         // empty, use default constructor
299     }
300 
301     private static class InvalidNonDefaultConstructorClass extends AutomaticBean {
302         private int field;
303 
304         protected InvalidNonDefaultConstructorClass(int data) {
305             //keep pmd calm and happy
306             field = 0;
307             method(data);
308         }
309 
310         public final void method(int data) {
311             field++;
312             if (data > 0) {
313                 method(data - 1);
314             }
315         }
316 
317         public int getField() {
318             return field;
319         }
320 
321         @Override
322         protected void finishLocalSetup() throws CheckstyleException {
323             //dummy method
324         }
325     }
326 }