HHH-11307 - Throw a sensible exception if the Enhancer cannot find the .class file

This commit is contained in:
Craig Andrews 2016-12-05 11:58:11 -05:00 committed by Vlad Mihalcea
parent 59870d0a14
commit f7bb312f90
2 changed files with 65 additions and 0 deletions

View File

@ -11,8 +11,10 @@ import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import javassist.CannotCompileException;
import javassist.ClassPool;
@ -114,6 +116,9 @@ public class EnhancerImpl implements Enhancer {
protected CtClass loadCtClassFromClass(Class<?> aClass) {
String resourceName = aClass.getName().replace( '.', '/' ) + ".class";
InputStream resourceAsStream = aClass.getClassLoader().getResourceAsStream( resourceName );
if ( resourceAsStream == null ) {
throw new UncheckedIOException( new FileNotFoundException ( "Not found: " + resourceName ) );
}
try {
return classPool.makeClass( resourceAsStream );
}

View File

@ -0,0 +1,60 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.bytecode.enhancement.javassist;
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import javassist.CtClass;
import org.hibernate.bytecode.enhance.internal.javassist.EnhancerImpl;
import org.hibernate.bytecode.enhance.spi.EnhancementContext;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.junit.Test;
import org.mockito.Mockito;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
/**
* @author Vlad Mihalcea
*/
public class EnhancerFileNotFoundTest extends BaseUnitTestCase {
public static class Enhancer extends EnhancerImpl {
public Enhancer(EnhancementContext enhancementContext) {
super( enhancementContext );
}
@Override
public CtClass loadCtClassFromClass(Class<?> aClass) {
return super.loadCtClassFromClass( aClass );
}
}
@Test
@TestForIssue(jiraKey = "HHH-11307")
public void test()
throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
EnhancementContext enhancementContextMock = mock( EnhancementContext.class );
Enhancer enhancer = new Enhancer( enhancementContextMock );
try {
enhancer.loadCtClassFromClass( Mockito.mock( getClass() ).getClass() );
fail("Should throw FileNotFoundException!");
}
catch ( Exception expected ) {
assertEquals( FileNotFoundException.class, expected.getCause().getClass() );
}
}
}