Issue #3008: revert of 80ab48f commit to fix #1192

This commit is contained in:
Roman Ivanov 2016-03-05 15:02:46 -08:00
parent a1d903e6e8
commit b003d5b082
2 changed files with 32 additions and 2 deletions

View File

@ -169,7 +169,7 @@ public class ClassResolver {
safeLoad(name);
return true;
}
catch (final ClassNotFoundException ignored) {
catch (final ClassNotFoundException | NoClassDefFoundError ignored) {
return false;
}
}
@ -180,8 +180,9 @@ public class ClassResolver {
* @param name name of the class to load
* @return the {@code Class} for the specified class
* @throws ClassNotFoundException if an error occurs
* @throws NoClassDefFoundError if an error occurs
*/
public Class<?> safeLoad(String name) throws ClassNotFoundException {
private Class<?> safeLoad(String name) throws ClassNotFoundException, NoClassDefFoundError {
// The next line will load the class using the specified class
// loader. The magic is having the "false" parameter. This means the
// class will not be initialised. Very, very important.

View File

@ -20,6 +20,7 @@
package com.puppycrawl.tools.checkstyle.checks;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@ -150,4 +151,32 @@ public class ClassResolverTest {
assertTrue(ex.getMessage().endsWith("expected exception"));
}
}
/**
* This test exists to prevent any possible regression and let of
* https://github.com/checkstyle/checkstyle/issues/1192 to be persistent
* event is not very obvious
*
* @throws Exception when smth is not expected
*/
@Test
public void testIsLoadableWithNoClassDefFoundError() throws Exception {
final Set<String> imports = Sets.newHashSet();
imports.add("java.applet.someClass");
final ClassResolver classResolver = PowerMockito.spy(new ClassResolver(Thread
.currentThread().getContextClassLoader(), "", imports));
PowerMockito.doThrow(new NoClassDefFoundError("expected exception"))
.when(classResolver, "safeLoad", anyObject());
try {
final boolean result = classResolver.isLoadable("someClass");
assertFalse("result should be false", result);
}
catch (NoClassDefFoundError ex) {
fail("NoClassDefFoundError is not expected");
assertTrue(ex.getMessage().endsWith("expected exception"));
}
}
}