Skip to content

Commit

Permalink
Save work
Browse files Browse the repository at this point in the history
  • Loading branch information
romain-grecourt committed Dec 18, 2024
1 parent 01d0850 commit d10edd9
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ public class HelidonJunitExtension implements BeforeEachCallback,

@Override
public Object createTestInstance(TestInstanceFactoryContext fc, ExtensionContext context) {
// Use a proxy to start the container after the test instance creation
// The container is started lazily when invoking a method
// or when resolving parameters
// Instrument the test class
// Use a proxy to start the container lazily
Class<?> testClass = instrument(context.getRequiredTestClass(), List.of(), List.of(),
(type, method) -> {
// class context store specific to the intercepted method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@
/**
* Config delegate.
* <p>
* Also implements a {@link io.helidon.config.Config Helidon Config} delegate backed by a {@link io.helidon.config.spi.LazyConfigSource}
* Also implements a {@link Config Helidon Config} delegate backed by a {@link LazyConfigSource}
* to support "just in time" caching when using {@link io.helidon.config.Config Helidon Config}.
*/
abstract class HelidonTestConfigDelegate implements org.eclipse.microprofile.config.Config, Config {

private final LazyValue<Config> hdelegate = LazyValue.create(this::delegate0);
private final Map<org.eclipse.microprofile.config.Config, List<String>> cache = new HashMap<>();

/**
/*
* Get the MicroProfile config delegate.
*
* @return delegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,4 @@ <A extends Annotation, C extends Annotation> Stream<A> annotations(Class<A> aTyp
* @return annotations
*/
<A extends Annotation> Stream<A> annotations(Class<A> aType);

/**
* Test if an annotation of the given type is present.
*
* @param type annotation type
* @return {@code true} if found, {@code false} otherwise
*/
default boolean containsAnnotation(Class<? extends Annotation> type) {
return annotations(type).findFirst().isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ public sealed interface HelidonTestInfo<T extends AnnotatedElement> extends Heli
/**
* Create a new class info.
*
* @param element class
* @param clazz class
* @return ClassInfo
*/
static ClassInfo classInfo(Class<?> element) {
return classInfo(element, HelidonTestDescriptorImpl::new);
static ClassInfo classInfo(Class<?> clazz) {
return classInfo(clazz, HelidonTestDescriptorImpl::new);
}

/**
* Create a new class info.
*
* @param element class
* @param clazz class
* @param function descriptor factory
* @return ClassInfo
*/
static ClassInfo classInfo(Class<?> element, Function<Class<?>, HelidonTestDescriptor<Class<?>>> function) {
Class<?> clazz = Instrumented.unwrap(element);
return ClassInfo.CACHE.compute(clazz.getName(), (e, r) -> {
static ClassInfo classInfo(Class<?> clazz, Function<Class<?>, HelidonTestDescriptor<Class<?>>> function) {
Class<?> theClass = Instrumented.unwrap(clazz);
return ClassInfo.CACHE.compute(theClass.getName(), (e, r) -> {
if (r == null || r.get() == null) {
return new SoftReference<>(new ClassInfo(function.apply(element)));
return new SoftReference<>(new ClassInfo(function.apply(clazz)));
}
return r;
}).get();
Expand Down Expand Up @@ -95,15 +95,15 @@ static MethodInfo methodInfo(Method element, ClassInfo classInfo) {
/**
* Create a new method info.
*
* @param element method
* @param method method
* @param classInfo class info
* @param function descriptor factory
* @return MethodInfo
*/
static MethodInfo methodInfo(Method element, ClassInfo classInfo, Function<Method, HelidonTestDescriptor<Method>> function) {
return MethodInfo.CACHE.compute(MethodInfo.cacheKey(element, classInfo), (e, r) -> {
static MethodInfo methodInfo(Method method, ClassInfo classInfo, Function<Method, HelidonTestDescriptor<Method>> function) {
return MethodInfo.CACHE.compute(MethodInfo.cacheKey(method, classInfo), (e, r) -> {
if (r == null || r.get() == null) {
return new SoftReference<>(new MethodInfo(function.apply(element), classInfo));
return new SoftReference<>(new MethodInfo(function.apply(method), classInfo));
}
return r;
}).get();
Expand Down Expand Up @@ -155,6 +155,24 @@ default Optional<Method> testMethod() {
*/
ClassInfo classInfo();

/**
* Indicate if the container should be reset.
* For a class this is resolved via {@code HelidonTest#resetPerTest()}.
* For a method this is inferred if any of the following annotations is used:
* <ul>
* <li>{@link Configuration}</li>
* <li>{@link AddExtension}</li>
* <li>{@link AddBean}</li>
* <li>{@link AddJaxRs}</li>
* <li>{@link DisableDiscovery}</li>
* </ul>
*
* @return {@code true} if reset is required, {@code false} otherwise
*/
default boolean requiresReset() {
return false;
}

/**
* Class info.
*/
Expand All @@ -180,6 +198,17 @@ public ClassInfo classInfo() {
return this;
}

/**
* Test if any method in the represented class is annotated with the given type.
*
* @param aType annotation type
* @return {@code true} if found, {@code false} otherwise
*/
public boolean isTestClass(Class<? extends Annotation> aType) {
return Stream.of(element().getDeclaredMethods())
.anyMatch(m -> m.isAnnotationPresent(aType));
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down Expand Up @@ -307,20 +336,7 @@ public <A extends Annotation> Stream<A> annotations(Class<A> aType) {
classInfo.annotations(aType));
}

/**
* Indicate if the container should be reset.
* For a class this is resolved via {@code HelidonTest#resetPerTest()}.
* For a method this is inferred if any of the following annotations is used:
* <ul>
* <li>{@link Configuration}</li>
* <li>{@link AddExtension}</li>
* <li>{@link AddBean}</li>
* <li>{@link AddJaxRs}</li>
* <li>{@link DisableDiscovery}</li>
* </ul>
*
* @return {@code true} if reset is required, {@code false} otherwise
*/
@Override
public boolean requiresReset() {
return classInfo.resetPerTest()
|| descriptor.configuration().isPresent()
Expand Down
Loading

0 comments on commit d10edd9

Please sign in to comment.