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 org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  import static org.mockito.Matchers.anyObject;
28  
29  import java.util.HashSet;
30  import java.util.Set;
31  
32  import org.junit.Test;
33  import org.junit.runner.RunWith;
34  import org.powermock.api.mockito.PowerMockito;
35  import org.powermock.core.classloader.annotations.PrepareForTest;
36  import org.powermock.modules.junit4.PowerMockRunner;
37  
38  @RunWith(PowerMockRunner.class)
39  @PrepareForTest({ ClassResolver.class, ClassResolverTest.class })
40  public class ClassResolverTest {
41      @Test
42      public void testMisc() throws ClassNotFoundException {
43          final Set<String> imports = new HashSet<>();
44          imports.add("java.io.File");
45          imports.add("nothing.will.match.*");
46          imports.add("java.applet.*");
47          final ClassResolver classResolver =
48              new ClassResolver(Thread.currentThread().getContextClassLoader(),
49                  null, imports);
50          assertNotNull("Class resolver should not be null", classResolver);
51          try {
52              classResolver.resolve("who.will.win.the.world.cup", "");
53              fail("Should not resolve class");
54          }
55          catch (ClassNotFoundException ex) {
56              // expected
57          }
58          classResolver.resolve("java.lang.String", "");
59          classResolver.resolve("StringBuffer", "");
60          classResolver.resolve("AppletContext", "");
61  
62          try {
63              classResolver.resolve("ChoiceFormat", "");
64              fail("ClassNotFoundException is expected");
65          }
66          catch (ClassNotFoundException ex) {
67              // expected
68          }
69  
70          imports.add("java.text.ChoiceFormat");
71          final ClassResolver newClassResolver = new ClassResolver(
72                  Thread.currentThread().getContextClassLoader(), null, imports);
73          newClassResolver.resolve("ChoiceFormat", "");
74  
75          final ClassResolver javaUtilClassResolver = new ClassResolver(
76                  Thread.currentThread().getContextClassLoader(), "java.util", imports);
77          javaUtilClassResolver.resolve("List", "");
78          try {
79              javaUtilClassResolver.resolve("two.nil.england", "");
80              fail("ClassNotFoundException is expected");
81          }
82          catch (ClassNotFoundException ex) {
83              // expected
84          }
85      }
86  
87      @Test
88      public void testExistedImportCantBeResolved() {
89          final Set<String> imports = new HashSet<>();
90          imports.add("java.applet.someClass");
91          final ClassResolver classResolver = new ClassResolver(
92                  Thread.currentThread().getContextClassLoader(),
93                  "", imports);
94  
95          try {
96              classResolver.resolve("someClass", "");
97              fail("Exception expected");
98          }
99          catch (ClassNotFoundException ex) {
100             // expected
101             assertEquals("Invalid exception message", "someClass", ex.getMessage());
102         }
103     }
104 
105     @Test
106     public void testResolveInnerClass() throws Exception {
107         final Set<String> imports = new HashSet<>();
108         final ClassResolver classResolver = new ClassResolver(
109                 Thread.currentThread().getContextClassLoader(),
110                 "java.util", imports);
111 
112         final Class<?> entry = classResolver.resolve("Entry", "Map");
113         assertEquals("Invalid resolve result", "java.util.Map$Entry", entry.getName());
114     }
115 
116     @Test
117     public void testResolveInnerClassWithEmptyPackage() {
118         final Set<String> imports = new HashSet<>();
119         final ClassResolver classResolver = new ClassResolver(
120                 Thread.currentThread().getContextClassLoader(),
121                 "", imports);
122 
123         try {
124             classResolver.resolve("Entry", "Map");
125             fail("Exception is expected");
126         }
127         catch (ClassNotFoundException ex) {
128             assertEquals("Invalid exception message", "Entry", ex.getMessage());
129         }
130     }
131 
132     @Test
133     public void testResolveQualifiedNameFails() throws Exception {
134         final Set<String> imports = new HashSet<>();
135         imports.add("java.applet.someClass");
136 
137         final ClassResolver classResolver = PowerMockito.spy(new ClassResolver(Thread
138                 .currentThread().getContextClassLoader(), "", imports));
139 
140         PowerMockito.doThrow(new ClassNotFoundException("expected exception"))
141                 .when(classResolver, "safeLoad", anyObject());
142         PowerMockito.doReturn(true).when(classResolver, "isLoadable", anyObject());
143 
144         try {
145             classResolver.resolve("someClass", "");
146             fail("Exception expected");
147         }
148         catch (IllegalStateException ex) {
149             // expected
150             final String expected = "expected exception";
151             assertTrue("Invalid exception cause, should be: ClassNotFoundException",
152                     ex.getCause() instanceof ClassNotFoundException);
153             assertTrue("Invalid exception message, should end with: " + expected,
154                     ex.getMessage().endsWith(expected));
155         }
156     }
157 
158     /**
159      * This test exists to prevent any possible regression and let of
160      * https://github.com/checkstyle/checkstyle/issues/1192 to be persistent
161      * event is not very obvious.
162      *
163      * @throws Exception when smth is not expected
164      */
165     @Test
166     public void testIsLoadableWithNoClassDefFoundError() throws Exception {
167         final Set<String> imports = new HashSet<>();
168         imports.add("java.applet.someClass");
169 
170         final ClassResolver classResolver = PowerMockito.spy(new ClassResolver(Thread
171                 .currentThread().getContextClassLoader(), "", imports));
172 
173         PowerMockito.doThrow(new NoClassDefFoundError("expected exception"))
174                 .when(classResolver, "safeLoad", anyObject());
175 
176         final boolean result = classResolver.isLoadable("someClass");
177         assertFalse("result should be false", result);
178     }
179 }