Merge branch '6.0.x'

This commit is contained in:
Sam Brannen 2023-06-25 18:50:23 +02:00
commit 4cbf47834d
2 changed files with 33 additions and 17 deletions

View File

@ -26,9 +26,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.core.Conventions; import org.springframework.core.Conventions;
import org.springframework.test.context.TestContext; import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.support.AbstractTestExecutionListener; import org.springframework.test.context.support.AbstractTestExecutionListener;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
/** /**
@ -47,6 +45,8 @@ class MicrometerObservationRegistryTestExecutionListener extends AbstractTestExe
private static final Log logger = LogFactory.getLog(MicrometerObservationRegistryTestExecutionListener.class); private static final Log logger = LogFactory.getLog(MicrometerObservationRegistryTestExecutionListener.class);
private static final String THREAD_LOCAL_ACCESSOR_CLASS_NAME = "io.micrometer.context.ThreadLocalAccessor";
private static final String OBSERVATION_THREAD_LOCAL_ACCESSOR_CLASS_NAME = private static final String OBSERVATION_THREAD_LOCAL_ACCESSOR_CLASS_NAME =
"io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor"; "io.micrometer.observation.contextpropagation.ObservationThreadLocalAccessor";
@ -63,25 +63,41 @@ class MicrometerObservationRegistryTestExecutionListener extends AbstractTestExe
MicrometerObservationRegistryTestExecutionListener.class, "previousObservationRegistry"); MicrometerObservationRegistryTestExecutionListener.class, "previousObservationRegistry");
static { public MicrometerObservationRegistryTestExecutionListener() {
// Trigger eager resolution of Micrometer Observation types during static // Trigger eager resolution of Micrometer Observation types to ensure that
// initialization of this class to ensure that this listener can be properly // this listener can be properly skipped when SpringFactoriesLoader attempts
// skipped when SpringFactoriesLoader attempts to load it, if micrometer-observation // to load it -- for example, if context-propagation and micrometer-observation
// is not in the classpath or if the version of ObservationThreadLocalAccessor // are not in the classpath or if the version of ObservationThreadLocalAccessor
// present does not include the getObservationRegistry() method. // present does not include the getObservationRegistry() method.
String errorMessage = ClassLoader classLoader = getClass().getClassLoader();
"MicrometerObservationRegistryTestExecutionListener requires micrometer-observation 1.10.8 or higher"; String errorMessage = """
Class<?> clazz; MicrometerObservationRegistryTestExecutionListener requires \
io.micrometer:micrometer-observation:1.10.8 or higher and \
io.micrometer:context-propagation:1.0.3 or higher.""";
try { try {
clazz = Class.forName(OBSERVATION_THREAD_LOCAL_ACCESSOR_CLASS_NAME, true, Class.forName(THREAD_LOCAL_ACCESSOR_CLASS_NAME, false, classLoader);
TestExecutionListener.class.getClassLoader()); Class<?> clazz = Class.forName(OBSERVATION_THREAD_LOCAL_ACCESSOR_CLASS_NAME, false, classLoader);
Method method = ReflectionUtils.findMethod(clazz, "getObservationRegistry");
if (method == null) {
// We simulate "class not found" even though it's "method not found", since
// the ClassNotFoundException will be processed in the subsequent catch-block.
throw new ClassNotFoundException(OBSERVATION_THREAD_LOCAL_ACCESSOR_CLASS_NAME);
}
} }
catch (Throwable ex) { catch (Throwable ex) {
throw new IllegalStateException(errorMessage, ex); // Ensure that we throw a NoClassDefFoundError so that the exception will be properly
// handled in TestContextFailureHandler.
// If the original exception was a ClassNotFoundException or NoClassDefFoundError,
// we throw a NoClassDefFoundError with an augmented error message and omit the cause.
if (ex instanceof ClassNotFoundException || ex instanceof NoClassDefFoundError) {
throw new NoClassDefFoundError(ex.getMessage() + ". " + errorMessage);
}
// Otherwise, we throw a NoClassDefFoundError with the cause initialized.
Error error = new NoClassDefFoundError(errorMessage);
error.initCause(ex);
throw error;
} }
Method method = ReflectionUtils.findMethod(clazz, "getObservationRegistry");
Assert.state(method != null, errorMessage);
} }

View File

@ -43,7 +43,7 @@ class TestContextFailureHandler implements FailureHandler {
logger.debug(""" logger.debug("""
Skipping candidate %1$s [%2$s] due to a missing dependency. \ Skipping candidate %1$s [%2$s] due to a missing dependency. \
Specify custom %1$s classes or make the default %1$s classes \ Specify custom %1$s classes or make the default %1$s classes \
and their required dependencies available. Offending class: [%3$s]""" and their required dependencies available. Offending class: %3$s"""
.formatted(factoryType.getSimpleName(), factoryImplementationName, ex.getMessage())); .formatted(factoryType.getSimpleName(), factoryImplementationName, ex.getMessage()));
} }
} }