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.javadoc;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocMethodCheck.MSG_CLASS_INFO;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertNull;
26  import static org.junit.Assert.assertTrue;
27  import static org.junit.Assert.fail;
28  
29  import java.lang.reflect.Constructor;
30  import java.lang.reflect.InvocationTargetException;
31  import java.lang.reflect.Method;
32  
33  import org.junit.Test;
34  
35  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
36  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
37  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
38  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
39  
40  public class AbstractTypeAwareCheckTest extends AbstractModuleTestSupport {
41      @Override
42      protected String getPackageLocation() {
43          return "com/puppycrawl/tools/checkstyle/checks/javadoc/abstracttypeaware";
44      }
45  
46      @Test
47      public void testIsSubclassWithNulls() throws Exception {
48          final JavadocMethodCheck check = new JavadocMethodCheck();
49          final Method isSubclass = check.getClass().getSuperclass()
50                  .getDeclaredMethod("isSubclass", Class.class, Class.class);
51          isSubclass.setAccessible(true);
52          assertFalse("Should return false if at least one of the params is null",
53              (boolean) isSubclass.invoke(check, null, null));
54      }
55  
56      @Test
57      public void testTokenToString() throws Exception {
58          final Class<?> tokenType = Class.forName("com.puppycrawl.tools.checkstyle.checks.javadoc."
59                  + "AbstractTypeAwareCheck$Token");
60          final Constructor<?> tokenConstructor = tokenType.getDeclaredConstructor(String.class,
61                  int.class, int.class);
62          final Object token = tokenConstructor.newInstance("blablabla", 1, 1);
63          final Method toString = token.getClass().getDeclaredMethod("toString");
64          final String result = (String) toString.invoke(token);
65          assertEquals("Invalid toString result", "Token[blablabla(1x1)]", result);
66      }
67  
68      @Test
69      public void testClassRegularClass() throws Exception {
70          final Class<?> tokenType = Class.forName("com.puppycrawl.tools.checkstyle.checks.javadoc."
71                  + "AbstractTypeAwareCheck$Token");
72  
73          final Class<?> regularClassType = Class
74                  .forName("com.puppycrawl.tools.checkstyle.checks.javadoc."
75                          + "AbstractTypeAwareCheck$RegularClass");
76          final Constructor<?> regularClassConstructor = regularClassType.getDeclaredConstructor(
77                  tokenType, String.class, AbstractTypeAwareCheck.class);
78          regularClassConstructor.setAccessible(true);
79  
80          try {
81              regularClassConstructor.newInstance(null, "", new JavadocMethodCheck());
82              fail("Exception is expected");
83          }
84          catch (InvocationTargetException ex) {
85              assertTrue("Invalid exception class, expected: IllegalArgumentException.class",
86                  ex.getCause() instanceof IllegalArgumentException);
87              assertEquals("Invalid exception message",
88                  "ClassInfo's name should be non-null", ex.getCause().getMessage());
89          }
90  
91          final Constructor<?> tokenConstructor = tokenType.getDeclaredConstructor(String.class,
92                  int.class, int.class);
93          final Object token = tokenConstructor.newInstance("blablabla", 1, 1);
94  
95          final JavadocMethodCheck methodCheck = new JavadocMethodCheck();
96          final Object regularClass = regularClassConstructor.newInstance(token, "sur",
97                  methodCheck);
98  
99          final Method toString = regularClass.getClass().getDeclaredMethod("toString");
100         toString.setAccessible(true);
101         final String result = (String) toString.invoke(regularClass);
102         final String expected = "RegularClass[name=Token[blablabla(1x1)], in class='sur', check="
103                 + methodCheck.hashCode() + "," + " loadable=true, class=null]";
104 
105         assertEquals("Invalid toString result", expected, result);
106 
107         final Method setClazz = regularClass.getClass().getDeclaredMethod("setClazz", Class.class);
108         setClazz.setAccessible(true);
109         final Class<?> arg = null;
110         setClazz.invoke(regularClass, arg);
111 
112         final Method getClazz = regularClass.getClass().getDeclaredMethod("getClazz");
113         getClazz.setAccessible(true);
114         assertNull("Expected null", getClazz.invoke(regularClass));
115     }
116 
117     @Test
118     public void testClassAliasToString() throws Exception {
119         final Class<?> tokenType = Class.forName("com.puppycrawl.tools.checkstyle.checks.javadoc."
120                 + "AbstractTypeAwareCheck$Token");
121 
122         final Class<?> regularClassType = Class
123                 .forName("com.puppycrawl.tools.checkstyle.checks.javadoc."
124                         + "AbstractTypeAwareCheck$RegularClass");
125         final Constructor<?> regularClassConstructor = regularClassType.getDeclaredConstructor(
126                 tokenType, String.class, AbstractTypeAwareCheck.class);
127         regularClassConstructor.setAccessible(true);
128 
129         final Constructor<?> tokenConstructor = tokenType.getDeclaredConstructor(String.class,
130                 int.class, int.class);
131         final Object token = tokenConstructor.newInstance("blablabla", 1, 1);
132 
133         final Object regularClass = regularClassConstructor.newInstance(token, "sur",
134                 new JavadocMethodCheck());
135 
136         final Class<?> classAliasType = Class.forName(
137                 "com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractTypeAwareCheck$ClassAlias");
138         final Class<?> abstractTypeInfoType = Class
139                 .forName("com.puppycrawl.tools.checkstyle.checks.javadoc."
140                         + "AbstractTypeAwareCheck$AbstractClassInfo");
141 
142         final Constructor<?> classAliasConstructor = classAliasType
143                 .getDeclaredConstructor(tokenType, abstractTypeInfoType);
144         classAliasConstructor.setAccessible(true);
145 
146         final Object classAlias = classAliasConstructor.newInstance(token, regularClass);
147         final Method toString = classAlias.getClass().getDeclaredMethod("toString");
148         toString.setAccessible(true);
149         final String result = (String) toString.invoke(classAlias);
150         assertEquals("Invalid toString result",
151             "ClassAlias[alias Token[blablabla(1x1)] for Token[blablabla(1x1)]]", result);
152     }
153 
154     @Test
155     public void testWithoutLogErrors() throws Exception {
156         final DefaultConfiguration config = createModuleConfig(JavadocMethodCheck.class);
157         config.addAttribute("logLoadErrors", "false");
158         config.addAttribute("allowUndeclaredRTE", "true");
159         final String[] expected = {
160             "7:8: " + getCheckMessage(MSG_CLASS_INFO, "@throws", "InvalidExceptionName"),
161         };
162         try {
163             verify(config, getPath("InputAbstractTypeAwareLoadErrors.java"), expected);
164             fail("Exception is expected");
165         }
166         catch (CheckstyleException ex) {
167             final IllegalStateException cause = (IllegalStateException) ex.getCause();
168             assertEquals("Invalid exception message",
169                 getCheckMessage(MSG_CLASS_INFO, "@throws", "InvalidExceptionName"),
170                 cause.getMessage());
171         }
172     }
173 
174     @Test
175     public void testWithSuppressLoadErrors() throws Exception {
176         final DefaultConfiguration checkConfig = createModuleConfig(JavadocMethodCheck.class);
177         checkConfig.addAttribute("suppressLoadErrors", "true");
178         checkConfig.addAttribute("allowUndeclaredRTE", "true");
179         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
180 
181         verify(checkConfig, getPath("InputAbstractTypeAwareLoadErrors.java"), expected);
182     }
183 }