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; package org.hibernate.testing.bytecode.enhancement;
import javassist.CtClass; import javassist.CtClass;
import javassist.CtField;
import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext; import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext;
@ -17,8 +18,29 @@ import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext;
*/ */
public class EnhancerTestContext extends DefaultEnhancementContext { public class EnhancerTestContext extends DefaultEnhancementContext {
@Override
public boolean doBiDirectionalAssociationManagement(CtField field) {
return true;
}
@Override
public boolean doDirtyCheckingInline(CtClass classDescriptor) {
return true;
}
@Override @Override
public boolean doExtendedEnhancement(CtClass classDescriptor) { public boolean doExtendedEnhancement(CtClass classDescriptor) {
return true; 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 { public abstract class EnhancerTestUtils extends BaseUnitTestCase {
private static final CoreMessageLogger log = CoreLogging.messageLogger( EnhancerTestUtils.class ); private static final CoreMessageLogger log = CoreLogging.messageLogger( EnhancerTestUtils.class );
private static EnhancementContext enhancementContext = new EnhancerTestContext();
private static String workingDir = System.getProperty( "java.io.tmpdir" ); private static String workingDir = System.getProperty( "java.io.tmpdir" );
/** /**
@ -58,7 +57,7 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
CtClass entityCtClass = generateCtClassForAnEntity( classToEnhance ); CtClass entityCtClass = generateCtClassForAnEntity( classToEnhance );
byte[] original = entityCtClass.toBytecode(); 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 ) ); assertFalse( "entity was not enhanced", Arrays.equals( original, enhanced ) );
log.infof( "enhanced entity [%s]", entityCtClass.getName() ); log.infof( "enhanced entity [%s]", entityCtClass.getName() );
@ -81,18 +80,20 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
} }
/* --- */ /* --- */
@SuppressWarnings("unchecked") public static <T extends EnhancerTestTask> void runEnhancerTestTask(Class<T> task) {
public static void runEnhancerTestTask(final Class<? extends EnhancerTestTask> task) { runEnhancerTestTask( task, new EnhancerTestContext() );
}
public static <T extends EnhancerTestTask> void runEnhancerTestTask(Class<T> task, EnhancementContext context) {
EnhancerTestTask taskObject = null; EnhancerTestTask taskObject = null;
ClassLoader defaultCL = Thread.currentThread().getContextClassLoader(); ClassLoader defaultCL = Thread.currentThread().getContextClassLoader();
try { try {
ClassLoader cl = EnhancerTestUtils.getEnhancerClassLoader( task.getPackage().getName() ); ClassLoader cl = EnhancerTestUtils.getEnhancerClassLoader( context, task.getPackage().getName() );
EnhancerTestUtils.setupClassLoader( cl, task ); EnhancerTestUtils.setupClassLoader( cl, task );
EnhancerTestUtils.setupClassLoader( cl, task.newInstance().getAnnotatedClasses() ); EnhancerTestUtils.setupClassLoader( cl, task.newInstance().getAnnotatedClasses() );
Thread.currentThread().setContextClassLoader( cl ); 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.prepare();
taskObject.execute(); 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() { return new ClassLoader() {
@SuppressWarnings("ResultOfMethodCallIgnored") @SuppressWarnings("ResultOfMethodCallIgnored")
@Override @Override
@ -131,21 +132,21 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
if ( !name.startsWith( packageName ) ) { if ( !name.startsWith( packageName ) ) {
return getParent().loadClass( name ); return getParent().loadClass( name );
} }
final Class c = findLoadedClass( name ); Class c = findLoadedClass( name );
if ( c != null ) { if ( c != null ) {
return c; return c;
} }
final InputStream is = this.getResourceAsStream( name.replace( '.', '/' ) + ".class" ); InputStream is = getResourceAsStream( name.replace( '.', '/' ) + ".class" );
if ( is == null ) { if ( is == null ) {
throw new ClassNotFoundException( name + " not found" ); throw new ClassNotFoundException( name + " not found" );
} }
try { try {
final byte[] original = new byte[is.available()]; byte[] original = new byte[is.available()];
new BufferedInputStream( is ).read( original ); 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" ); File f = new File( workingDir + File.separator + name.replace( ".", File.separator ) + ".class" );
f.getParentFile().mkdirs(); f.getParentFile().mkdirs();
@ -172,7 +173,7 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
public static Object getFieldByReflection(Object entity, String fieldName) { public static Object getFieldByReflection(Object entity, String fieldName) {
try { try {
Field field = entity.getClass().getDeclaredField( fieldName ); Field field = entity.getClass().getDeclaredField( fieldName );
field.setAccessible( true ); field.setAccessible( true );
return field.get( entity ); return field.get( entity );
} }
@ -196,7 +197,7 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
* compares the dirty fields of an entity with a set of expected values * compares the dirty fields of an entity with a set of expected values
*/ */
public static void checkDirtyTracking(Object entityInstance, String... dirtyFields) { public static void checkDirtyTracking(Object entityInstance, String... dirtyFields) {
final SelfDirtinessTracker selfDirtinessTracker = (SelfDirtinessTracker) entityInstance; SelfDirtinessTracker selfDirtinessTracker = (SelfDirtinessTracker) entityInstance;
assertEquals( dirtyFields.length > 0, selfDirtinessTracker.$$_hibernate_hasDirtyAttributes() ); assertEquals( dirtyFields.length > 0, selfDirtinessTracker.$$_hibernate_hasDirtyAttributes() );
String[] tracked = selfDirtinessTracker.$$_hibernate_getDirtyAttributes(); String[] tracked = selfDirtinessTracker.$$_hibernate_getDirtyAttributes();
assertEquals( dirtyFields.length, tracked.length ); assertEquals( dirtyFields.length, tracked.length );