From 68a2896c2b33c672ca72e7c21b8a9257e8f78cfe Mon Sep 17 00:00:00 2001 From: barreiro Date: Wed, 13 Jul 2016 03:47:49 +0100 Subject: [PATCH] HHH-10922 - Add support for custom enhancement context on bytecode enhacer tests --- .../enhancement/EnhancerTestContext.java | 22 +++++++++++++++ .../enhancement/EnhancerTestUtils.java | 27 ++++++++++--------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/EnhancerTestContext.java b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/EnhancerTestContext.java index b9fa8aa4d3..7d21977cf4 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/EnhancerTestContext.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/EnhancerTestContext.java @@ -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; + } + } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/EnhancerTestUtils.java b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/EnhancerTestUtils.java index 3a7e6dfa31..481c90e812 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/EnhancerTestUtils.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/EnhancerTestUtils.java @@ -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 task) { + public static void runEnhancerTestTask(Class task) { + runEnhancerTestTask( task, new EnhancerTestContext() ); + } + public static void runEnhancerTestTask(Class 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) 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(); @@ -172,7 +173,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 ); } @@ -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 );