HHH-10922 - Add support for custom enhancement context on bytecode enhacer tests

This commit is contained in:
barreiro 2016-07-13 03:47:49 +01:00 committed by Gail Badner
parent 2dcb75e2a5
commit 68a2896c2b
2 changed files with 36 additions and 13 deletions

View File

@ -7,6 +7,7 @@
package org.hibernate.testing.bytecode.enhancement;
import javassist.CtClass;
import javassist.CtField;
import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext;
@ -17,8 +18,29 @@ import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext;
*/
public class EnhancerTestContext extends DefaultEnhancementContext {
@Override
public boolean doBiDirectionalAssociationManagement(CtField field) {
return true;
}
@Override
public boolean doDirtyCheckingInline(CtClass classDescriptor) {
return true;
}
@Override
public boolean doExtendedEnhancement(CtClass classDescriptor) {
return true;
}
@Override
public boolean hasLazyLoadableAttributes(CtClass classDescriptor) {
return true;
}
@Override
public boolean isLazyLoadable(CtField field) {
return true;
}
}

View File

@ -47,7 +47,6 @@ import static org.junit.Assert.fail;
public abstract class EnhancerTestUtils extends BaseUnitTestCase {
private static final CoreMessageLogger log = CoreLogging.messageLogger( EnhancerTestUtils.class );
private static EnhancementContext enhancementContext = new EnhancerTestContext();
private static String workingDir = System.getProperty( "java.io.tmpdir" );
/**
@ -58,7 +57,7 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
CtClass entityCtClass = generateCtClassForAnEntity( classToEnhance );
byte[] original = entityCtClass.toBytecode();
byte[] enhanced = new Enhancer( enhancementContext ).enhance( entityCtClass.getName(), original );
byte[] enhanced = new Enhancer( new EnhancerTestContext() ).enhance( entityCtClass.getName(), original );
assertFalse( "entity was not enhanced", Arrays.equals( original, enhanced ) );
log.infof( "enhanced entity [%s]", entityCtClass.getName() );
@ -81,18 +80,20 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
}
/* --- */
@SuppressWarnings("unchecked")
public static void runEnhancerTestTask(final Class<? extends EnhancerTestTask> task) {
public static <T extends EnhancerTestTask> void runEnhancerTestTask(Class<T> task) {
runEnhancerTestTask( task, new EnhancerTestContext() );
}
public static <T extends EnhancerTestTask> void runEnhancerTestTask(Class<T> task, EnhancementContext context) {
EnhancerTestTask taskObject = null;
ClassLoader defaultCL = Thread.currentThread().getContextClassLoader();
try {
ClassLoader cl = EnhancerTestUtils.getEnhancerClassLoader( task.getPackage().getName() );
ClassLoader cl = EnhancerTestUtils.getEnhancerClassLoader( context, task.getPackage().getName() );
EnhancerTestUtils.setupClassLoader( cl, task );
EnhancerTestUtils.setupClassLoader( cl, task.newInstance().getAnnotatedClasses() );
Thread.currentThread().setContextClassLoader( cl );
taskObject = ( (Class<? extends EnhancerTestTask>) cl.loadClass( task.getName() ) ).newInstance();
taskObject = cl.loadClass( task.getName() ).asSubclass( EnhancerTestTask.class ).newInstance();
taskObject.prepare();
taskObject.execute();
@ -123,7 +124,7 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
}
}
private static ClassLoader getEnhancerClassLoader(final String packageName) {
private static ClassLoader getEnhancerClassLoader(EnhancementContext context, String packageName) {
return new ClassLoader() {
@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
@ -131,21 +132,21 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
if ( !name.startsWith( packageName ) ) {
return getParent().loadClass( name );
}
final Class c = findLoadedClass( name );
Class c = findLoadedClass( name );
if ( c != null ) {
return c;
}
final InputStream is = this.getResourceAsStream( name.replace( '.', '/' ) + ".class" );
InputStream is = getResourceAsStream( name.replace( '.', '/' ) + ".class" );
if ( is == null ) {
throw new ClassNotFoundException( name + " not found" );
}
try {
final byte[] original = new byte[is.available()];
byte[] original = new byte[is.available()];
new BufferedInputStream( is ).read( original );
final byte[] enhanced = new Enhancer( enhancementContext ).enhance( name, original );
byte[] enhanced = new Enhancer( context ).enhance( name, original );
File f = new File( workingDir + File.separator + name.replace( ".", File.separator ) + ".class" );
f.getParentFile().mkdirs();
@ -196,7 +197,7 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
* compares the dirty fields of an entity with a set of expected values
*/
public static void checkDirtyTracking(Object entityInstance, String... dirtyFields) {
final SelfDirtinessTracker selfDirtinessTracker = (SelfDirtinessTracker) entityInstance;
SelfDirtinessTracker selfDirtinessTracker = (SelfDirtinessTracker) entityInstance;
assertEquals( dirtyFields.length > 0, selfDirtinessTracker.$$_hibernate_hasDirtyAttributes() );
String[] tracked = selfDirtinessTracker.$$_hibernate_getDirtyAttributes();
assertEquals( dirtyFields.length, tracked.length );