Skip to content

Commit b3d755b

Browse files
committed
Add an extra message to the @BytecodeEnhanced engine discovery errors
1 parent 565a56c commit b3d755b

File tree

1 file changed

+62
-55
lines changed

1 file changed

+62
-55
lines changed

hibernate-testing/src/main/java/org/hibernate/testing/bytecode/enhancement/extension/engine/BytecodeEnhancedTestEngine.java

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.jupiter.api.TestInstance;
3232
import org.junit.jupiter.api.extension.ExecutionCondition;
3333
import org.junit.jupiter.api.extension.Extension;
34+
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
3435
import org.junit.jupiter.api.extension.ExtensionContext;
3536
import org.junit.jupiter.api.extension.TestInstantiationAwareExtension;
3637
import org.junit.jupiter.api.io.CleanupMode;
@@ -64,61 +65,67 @@ public String getId() {
6465

6566
@Override
6667
public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId) {
67-
final BytecodeEnhancedEngineDescriptor engineDescriptor = new BytecodeEnhancedEngineDescriptor(
68-
(JupiterEngineDescriptor) new JupiterTestEngine().discover( discoveryRequest, uniqueId )
69-
);
70-
71-
for ( TestDescriptor testDescriptor : new HashSet<>( engineDescriptor.getChildren() ) ) {
72-
if ( testDescriptor instanceof ClassBasedTestDescriptor ) {
73-
try {
74-
ClassBasedTestDescriptor descriptor = (ClassBasedTestDescriptor) testDescriptor;
75-
// if the test class is annotated with @BytecodeEnhanced
76-
// we replace the descriptor with the new one that will point to an enhanced test class,
77-
// this also means that we need to add all the child descriptors back as well...
78-
// Then on the extension side we set the classloader that contains the enhanced test class
79-
// and set it back to the original once the test class is destroyed.
80-
Optional<BytecodeEnhanced> bytecodeEnhanced = findAnnotation(
81-
descriptor.getTestClass(), BytecodeEnhanced.class );
82-
if ( bytecodeEnhanced.isPresent() ) {
83-
TestDescriptor parent = descriptor.getParent().orElseThrow( IllegalStateException::new );
84-
Class<?> klass = descriptor.getTestClass();
85-
86-
JupiterConfiguration jc = ( (JupiterEngineDescriptor) parent ).getConfiguration();
87-
88-
String[] testEnhancedClasses = Arrays.stream( bytecodeEnhanced.get().testEnhancedClasses() )
89-
.map( Class::getName ).toArray( String[]::new );
90-
91-
// NOTE: get children before potentially removing from hierarchy, since after that there will be none.
92-
Set<? extends TestDescriptor> children = new HashSet<>( descriptor.getChildren() );
93-
if ( !bytecodeEnhanced.get().runNotEnhancedAsWell() ) {
94-
descriptor.removeFromHierarchy();
95-
}
96-
97-
Map<Object, Class<?>> classes = enhanceTestClass( klass );
98-
if ( classes.size() == 1 ) {
99-
replaceWithEnhanced( classes.values().iterator().next(), descriptor, jc, children, parent, testEnhancedClasses );
100-
}
101-
else {
102-
for ( Map.Entry<Object, Class<?>> entry : classes.entrySet() ) {
103-
replaceWithEnhanced(
104-
entry.getValue(), descriptor, jc, children, parent, testEnhancedClasses, entry.getKey() );
105-
}
106-
}
107-
108-
addEnhancementCheck( false, testEnhancedClasses, descriptor, jc );
109-
}
110-
else {
111-
testDescriptor.removeFromHierarchy();
112-
}
113-
}
114-
catch (ClassNotFoundException | NoSuchMethodException e) {
115-
throw new RuntimeException( e );
116-
}
117-
}
118-
}
119-
120-
return engineDescriptor;
121-
}
68+
try {
69+
final BytecodeEnhancedEngineDescriptor engineDescriptor = new BytecodeEnhancedEngineDescriptor(
70+
(JupiterEngineDescriptor) new JupiterTestEngine().discover( discoveryRequest, uniqueId )
71+
);
72+
73+
for ( TestDescriptor testDescriptor : new HashSet<>( engineDescriptor.getChildren() ) ) {
74+
if ( testDescriptor instanceof ClassBasedTestDescriptor ) {
75+
try {
76+
ClassBasedTestDescriptor descriptor = (ClassBasedTestDescriptor) testDescriptor;
77+
// if the test class is annotated with @BytecodeEnhanced
78+
// we replace the descriptor with the new one that will point to an enhanced test class,
79+
// this also means that we need to add all the child descriptors back as well...
80+
// Then on the extension side we set the classloader that contains the enhanced test class
81+
// and set it back to the original once the test class is destroyed.
82+
Optional<BytecodeEnhanced> bytecodeEnhanced = findAnnotation(
83+
descriptor.getTestClass(), BytecodeEnhanced.class );
84+
if ( bytecodeEnhanced.isPresent() ) {
85+
TestDescriptor parent = descriptor.getParent().orElseThrow( IllegalStateException::new );
86+
Class<?> klass = descriptor.getTestClass();
87+
88+
JupiterConfiguration jc = ( (JupiterEngineDescriptor) parent ).getConfiguration();
89+
90+
String[] testEnhancedClasses = Arrays.stream( bytecodeEnhanced.get().testEnhancedClasses() )
91+
.map( Class::getName ).toArray( String[]::new );
92+
93+
// NOTE: get children before potentially removing from hierarchy, since after that there will be none.
94+
Set<? extends TestDescriptor> children = new HashSet<>( descriptor.getChildren() );
95+
if ( !bytecodeEnhanced.get().runNotEnhancedAsWell() ) {
96+
descriptor.removeFromHierarchy();
97+
}
98+
99+
Map<Object, Class<?>> classes = enhanceTestClass( klass );
100+
if ( classes.size() == 1 ) {
101+
replaceWithEnhanced( classes.values().iterator().next(), descriptor, jc, children, parent, testEnhancedClasses );
102+
}
103+
else {
104+
for ( Map.Entry<Object, Class<?>> entry : classes.entrySet() ) {
105+
replaceWithEnhanced(
106+
entry.getValue(), descriptor, jc, children, parent, testEnhancedClasses, entry.getKey() );
107+
}
108+
}
109+
110+
addEnhancementCheck( false, testEnhancedClasses, descriptor, jc );
111+
}
112+
else {
113+
testDescriptor.removeFromHierarchy();
114+
}
115+
}
116+
catch (ClassNotFoundException | NoSuchMethodException e) {
117+
throw new RuntimeException( e );
118+
}
119+
}
120+
}
121+
122+
return engineDescriptor;
123+
} catch (OutOfMemoryError e) {
124+
throw e;
125+
} catch (Error e) {
126+
throw new ExtensionConfigurationException("Encountered a problem when enhancing the test classes. It is highly likely that @BytecodeEnhanced extension is incompatible with the provided version of JUnit.", e);
127+
}
128+
}
122129

123130
private void addEnhancementCheck(boolean enhance, String[] testEnhancedClasses,
124131
ClassBasedTestDescriptor descriptor, JupiterConfiguration jc) {

0 commit comments

Comments
 (0)