HHH-10922 - Add support for custom enhancement context on bytecode enhacer tests
(cherry picked from commit 68a2896c2b
)
Conflicts:
hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTestUtils.java
This commit is contained in:
parent
afab46fefa
commit
40ac9ce4b0
|
@ -7,6 +7,7 @@
|
|||
package org.hibernate.test.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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -45,8 +45,6 @@ import static org.junit.Assert.fail;
|
|||
*/
|
||||
public abstract class EnhancerTestUtils extends BaseUnitTestCase {
|
||||
|
||||
private static EnhancementContext enhancementContext = new EnhancerTestContext();
|
||||
|
||||
private static String workingDir = System.getProperty( "java.io.tmpdir" );
|
||||
|
||||
private static final CoreMessageLogger log = CoreLogging.messageLogger( EnhancerTestUtils.class );
|
||||
|
@ -59,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() );
|
||||
|
||||
|
@ -82,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();
|
||||
|
@ -124,28 +124,28 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
private static ClassLoader getEnhancerClassLoader(final String packageName) {
|
||||
private static ClassLoader getEnhancerClassLoader(EnhancementContext context, String packageName) {
|
||||
return new ClassLoader() {
|
||||
@Override
|
||||
public Class<?> loadClass(String name) throws ClassNotFoundException {
|
||||
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();
|
||||
|
@ -171,7 +171,7 @@ public abstract class EnhancerTestUtils extends BaseUnitTestCase {
|
|||
|
||||
public static Object getFieldByReflection(Object entity, String fieldName) {
|
||||
try {
|
||||
Field field = entity.getClass().getDeclaredField( fieldName );
|
||||
Field field = entity.getClass().getDeclaredField( fieldName );
|
||||
field.setAccessible( true );
|
||||
return field.get( entity );
|
||||
}
|
||||
|
@ -195,7 +195,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 );
|
||||
|
|
Loading…
Reference in New Issue