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