de.hilling.junit.cdi.scope.TestScopeExtension Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cdi-test-core Show documentation
Show all versions of cdi-test-core Show documentation
implementation of cdi-test
The newest version!
package de.hilling.junit.cdi.scope;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.spi.*;
import de.hilling.junit.cdi.annotations.ActivatableTestImplementation;
import de.hilling.junit.cdi.annotations.BypassTestInterceptor;
import de.hilling.junit.cdi.annotations.GlobalTestImplementation;
import de.hilling.junit.cdi.scope.annotationreplacement.AnnotationReplacementBuilder;
import de.hilling.junit.cdi.scope.context.TestContext;
import de.hilling.junit.cdi.scope.context.TestSuiteContext;
import de.hilling.junit.cdi.util.ReflectionsUtils;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import static java.util.logging.Level.FINE;
/**
* CDI {@link Extension} to enable proxying of (nearly) all method invocations. By
* default, these are all classes, except:
- Anonymous classes.
- Enums.
To preventing
* everything from being proxied it is possible to define explicit packages.
*/
@BypassTestInterceptor
public class TestScopeExtension implements Extension, Serializable {
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(
TestScopeExtension.class.getCanonicalName());
private final transient Map, ActivatableTestImplementation> decoratedTypes = new HashMap<>();
/**
* Add contexts after bean discovery.
*
* @param afterBeanDiscovery AfterBeanDiscovery
*/
public void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscovery) {
afterBeanDiscovery.addContext(new TestSuiteContext());
afterBeanDiscovery.addContext(new TestContext());
}
ActivatableTestImplementation annotationsFor(Class> clazz) {
return decoratedTypes.get(clazz);
}
/**
* Use {@link AfterTypeDiscovery} to add Stereotype.
*
* @param afterTypeDiscovery type meta information.
*/
public void afterTypeDiscovery(@Observes AfterTypeDiscovery afterTypeDiscovery) {
afterTypeDiscovery.getAlternatives().add(GlobalTestImplementation.class);
}
public void processAnnotatedTypes(@Observes ProcessAnnotatedType pat) {
LOG.log(FINE, "processing type {0}", pat);
new AnnotationReplacementBuilder<>(pat).invoke();
AnnotatedType type = pat.getAnnotatedType();
final Class javaClass = type.getJavaClass();
if (javaClass.isAnnotationPresent(ActivatableTestImplementation.class)) {
decoratedTypes.put(pat.getAnnotatedType().getJavaClass(), new ActivatableAlternativeBuilder<>(pat).invoke());
} else if (ReflectionsUtils.shouldProxyCdiType(javaClass)) {
pat.configureAnnotatedType().add(ImmutableReplaceable.builder().build());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy