Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check deps only once in MicrometerObservationRegistryTestExecutionLis…
…tener Prior to this commit, the required runtime dependencies were checked via reflection each time an attempt was made to instantiate MicrometerObservationRegistryTestExecutionListener. Since it's sufficient to check for the presence of required runtime dependencies only once, this commit caches the results of the dependency checks in a static field. This commit also introduces automated tests for the runtime dependency checks in MicrometerObservationRegistryTestExecutionListener. See gh-30747
- Loading branch information
Showing
2 changed files
with
142 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...ontext/observation/MicrometerObservationRegistryTestExecutionListenerDependencyTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright 2002-2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.observation; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.function.Predicate; | ||
import java.util.stream.IntStream; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.core.OverridingClassLoader; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
import static org.assertj.core.api.Assertions.assertThatNoException; | ||
import static org.springframework.test.context.observation.MicrometerObservationRegistryTestExecutionListener.DEPENDENCIES_ERROR_MESSAGE; | ||
import static org.springframework.test.context.observation.MicrometerObservationRegistryTestExecutionListener.OBSERVATION_THREAD_LOCAL_ACCESSOR_CLASS_NAME; | ||
import static org.springframework.test.context.observation.MicrometerObservationRegistryTestExecutionListener.THREAD_LOCAL_ACCESSOR_CLASS_NAME; | ||
|
||
/** | ||
* Unit tests for {@link MicrometerObservationRegistryTestExecutionListener} | ||
* behavior regarding required dependencies. | ||
* | ||
* @author Sam Brannen | ||
* @since 6.0.11 | ||
*/ | ||
class MicrometerObservationRegistryTestExecutionListenerDependencyTests { | ||
|
||
@Test | ||
void allDependenciesArePresent() throws Exception { | ||
FilteringClassLoader classLoader = new FilteringClassLoader(getClass().getClassLoader(), name -> false); | ||
Class<?> listenerClass = classLoader.loadClass(MicrometerObservationRegistryTestExecutionListener.class.getName()); | ||
// Invoke multiple times to ensure consistency. | ||
IntStream.rangeClosed(1, 5).forEach(n -> assertThatNoException().isThrownBy(() -> instantiateListener(listenerClass))); | ||
} | ||
|
||
@Test | ||
void threadLocalAccessorIsNotPresent() throws Exception { | ||
assertNoClassDefFoundErrorIsThrown(THREAD_LOCAL_ACCESSOR_CLASS_NAME); | ||
} | ||
|
||
@Test | ||
void observationThreadLocalAccessorIsNotPresent() throws Exception { | ||
assertNoClassDefFoundErrorIsThrown(OBSERVATION_THREAD_LOCAL_ACCESSOR_CLASS_NAME); | ||
} | ||
|
||
private void assertNoClassDefFoundErrorIsThrown(String missingClassName) throws Exception { | ||
FilteringClassLoader classLoader = new FilteringClassLoader(getClass().getClassLoader(), missingClassName::equals); | ||
Class<?> listenerClass = classLoader.loadClass(MicrometerObservationRegistryTestExecutionListener.class.getName()); | ||
// Invoke multiple times to ensure the same error message is generated every time. | ||
IntStream.rangeClosed(1, 5).forEach(n -> assertExceptionThrown(missingClassName, listenerClass)); | ||
} | ||
|
||
private void assertExceptionThrown(String missingClassName, Class<?> listenerClass) { | ||
assertThatExceptionOfType(InvocationTargetException.class) | ||
.isThrownBy(() -> instantiateListener(listenerClass)) | ||
.havingCause() | ||
.isInstanceOf(NoClassDefFoundError.class) | ||
.withMessage(missingClassName + ". " + DEPENDENCIES_ERROR_MESSAGE); | ||
} | ||
|
||
private void instantiateListener(Class<?> listenerClass) throws Exception { | ||
assertThat(listenerClass).isNotNull(); | ||
Constructor<?> constructor = listenerClass.getDeclaredConstructor(); | ||
constructor.setAccessible(true); | ||
constructor.newInstance(); | ||
} | ||
|
||
|
||
static class FilteringClassLoader extends OverridingClassLoader { | ||
|
||
private static final Predicate<? super String> isListenerClass = | ||
MicrometerObservationRegistryTestExecutionListener.class.getName()::equals; | ||
|
||
private final Predicate<String> classNameFilter; | ||
|
||
|
||
FilteringClassLoader(ClassLoader parent, Predicate<String> classNameFilter) { | ||
super(parent); | ||
this.classNameFilter = classNameFilter; | ||
} | ||
|
||
@Override | ||
protected boolean isEligibleForOverriding(String className) { | ||
return this.classNameFilter.or(isListenerClass).test(className); | ||
} | ||
|
||
@Override | ||
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { | ||
if (this.classNameFilter.test(name)) { | ||
throw new ClassNotFoundException(name); | ||
} | ||
return super.loadClass(name, resolve); | ||
} | ||
} | ||
|
||
} |