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