BAEL-635 Overview of Spring 5 (#1633)
* Overview of Spring 5 * Overview of Spring 5 * BAEL-635 Formatting
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.jupiter;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.SynthesizingMethodParameter;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Executable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
|
||||
abstract class MethodParameterFactory {
|
||||
|
||||
private MethodParameterFactory() {
|
||||
}
|
||||
|
||||
public static MethodParameter createMethodParameter(Parameter parameter) {
|
||||
Assert.notNull(parameter, "Parameter must not be null");
|
||||
Executable executable = parameter.getDeclaringExecutable();
|
||||
if (executable instanceof Method) {
|
||||
return new MethodParameter((Method) executable, getIndex(parameter));
|
||||
}
|
||||
return new MethodParameter((Constructor<?>) executable, getIndex(parameter));
|
||||
}
|
||||
|
||||
public static SynthesizingMethodParameter createSynthesizingMethodParameter(Parameter parameter) {
|
||||
Assert.notNull(parameter, "Parameter must not be null");
|
||||
Executable executable = parameter.getDeclaringExecutable();
|
||||
if (executable instanceof Method) {
|
||||
return new SynthesizingMethodParameter((Method) executable, getIndex(parameter));
|
||||
}
|
||||
throw new UnsupportedOperationException("Cannot create a SynthesizingMethodParameter for a constructor parameter: " + parameter);
|
||||
}
|
||||
|
||||
private static int getIndex(Parameter parameter) {
|
||||
Assert.notNull(parameter, "Parameter must not be null");
|
||||
Executable executable = parameter.getDeclaringExecutable();
|
||||
Parameter[] parameters = executable.getParameters();
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
if (parameters[i] == parameter) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException(String.format("Failed to resolve index of parameter [%s] in executable [%s]", parameter, executable.toGenericString()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.jupiter;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.config.DependencyDescriptor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.springframework.core.annotation.AnnotatedElementUtils.hasAnnotation;
|
||||
|
||||
abstract class ParameterAutowireUtils {
|
||||
|
||||
private ParameterAutowireUtils() {
|
||||
}
|
||||
|
||||
public static boolean isAutowirable(Parameter parameter) {
|
||||
return ApplicationContext.class.isAssignableFrom(parameter.getType()) || hasAnnotation(parameter, Autowired.class) || hasAnnotation(parameter, Qualifier.class) || hasAnnotation(parameter, Value.class);
|
||||
}
|
||||
|
||||
public static Object resolveDependency(Parameter parameter, Class<?> containingClass, ApplicationContext applicationContext) {
|
||||
|
||||
boolean required = findMergedAnnotation(parameter, Autowired.class)
|
||||
.map(Autowired::required)
|
||||
.orElse(true);
|
||||
MethodParameter methodParameter = (parameter.getDeclaringExecutable() instanceof Method ? MethodParameterFactory.createSynthesizingMethodParameter(parameter) : MethodParameterFactory.createMethodParameter(parameter));
|
||||
DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
|
||||
descriptor.setContainingClass(containingClass);
|
||||
|
||||
return applicationContext
|
||||
.getAutowireCapableBeanFactory()
|
||||
.resolveDependency(descriptor, null);
|
||||
}
|
||||
|
||||
private static <A extends Annotation> Optional<A> findMergedAnnotation(AnnotatedElement element, Class<A> annotationType) {
|
||||
|
||||
return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(element, annotationType));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.baeldung.jupiter;
|
||||
|
||||
import org.junit.jupiter.api.extension.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.test.context.TestContextManager;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Executable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
|
||||
public class SpringExtension implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback, ParameterResolver {
|
||||
|
||||
private static final ExtensionContext.Namespace namespace = ExtensionContext.Namespace.create(SpringExtension.class);
|
||||
|
||||
@Override
|
||||
public void beforeAll(ContainerExtensionContext context) throws Exception {
|
||||
getTestContextManager(context).beforeTestClass();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterAll(ContainerExtensionContext context) throws Exception {
|
||||
try {
|
||||
getTestContextManager(context).afterTestClass();
|
||||
} finally {
|
||||
context
|
||||
.getStore(namespace)
|
||||
.remove(context
|
||||
.getTestClass()
|
||||
.get());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {
|
||||
getTestContextManager(context).prepareTestInstance(testInstance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeEach(TestExtensionContext context) throws Exception {
|
||||
Object testInstance = context.getTestInstance();
|
||||
Method testMethod = context
|
||||
.getTestMethod()
|
||||
.get();
|
||||
getTestContextManager(context).beforeTestMethod(testInstance, testMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterEach(TestExtensionContext context) throws Exception {
|
||||
Object testInstance = context.getTestInstance();
|
||||
Method testMethod = context
|
||||
.getTestMethod()
|
||||
.get();
|
||||
Throwable testException = context
|
||||
.getTestException()
|
||||
.orElse(null);
|
||||
getTestContextManager(context).afterTestMethod(testInstance, testMethod, testException);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(ParameterContext parameterContext, ExtensionContext extensionContext) {
|
||||
Parameter parameter = parameterContext.getParameter();
|
||||
Executable executable = parameter.getDeclaringExecutable();
|
||||
return (executable instanceof Constructor && AnnotatedElementUtils.hasAnnotation(executable, Autowired.class)) || ParameterAutowireUtils.isAutowirable(parameter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object resolve(ParameterContext parameterContext, ExtensionContext extensionContext) {
|
||||
Parameter parameter = parameterContext.getParameter();
|
||||
Class<?> testClass = extensionContext
|
||||
.getTestClass()
|
||||
.get();
|
||||
ApplicationContext applicationContext = getApplicationContext(extensionContext);
|
||||
return ParameterAutowireUtils.resolveDependency(parameter, testClass, applicationContext);
|
||||
}
|
||||
|
||||
private ApplicationContext getApplicationContext(ExtensionContext context) {
|
||||
return getTestContextManager(context)
|
||||
.getTestContext()
|
||||
.getApplicationContext();
|
||||
}
|
||||
|
||||
private TestContextManager getTestContextManager(ExtensionContext context) {
|
||||
Assert.notNull(context, "ExtensionContext must not be null");
|
||||
Class<?> testClass = context
|
||||
.getTestClass()
|
||||
.get();
|
||||
ExtensionContext.Store store = context.getStore(namespace);
|
||||
return store.getOrComputeIfAbsent(testClass, TestContextManager::new, TestContextManager.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.jupiter;
|
||||
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration
|
||||
@Documented
|
||||
@Inherited
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface SpringJUnit5Config {
|
||||
|
||||
@AliasFor(annotation = ContextConfiguration.class, attribute = "classes")
|
||||
Class<?>[] value() default {};
|
||||
|
||||
@AliasFor(annotation = ContextConfiguration.class)
|
||||
Class<?>[] classes() default {};
|
||||
|
||||
@AliasFor(annotation = ContextConfiguration.class)
|
||||
String[] locations() default {};
|
||||
|
||||
@AliasFor(annotation = ContextConfiguration.class)
|
||||
Class<? extends ApplicationContextInitializer<?
|
||||
extends ConfigurableApplicationContext>>[] initializers() default {};
|
||||
|
||||
@AliasFor(annotation = ContextConfiguration.class)
|
||||
boolean inheritLocations() default true;
|
||||
|
||||
@AliasFor(annotation = ContextConfiguration.class)
|
||||
boolean inheritInitializers() default true;
|
||||
|
||||
@AliasFor(annotation = ContextConfiguration.class)
|
||||
String name() default "";
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.jupiter;
|
||||
|
||||
import com.baeldung.web.reactive.Task;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class TestConfig {
|
||||
|
||||
@Bean
|
||||
static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
|
||||
return new PropertySourcesPlaceholderConfigurer();
|
||||
}
|
||||
|
||||
@Bean
|
||||
Task taskName() {
|
||||
return new Task("taskName", 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.web.reactive;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Task {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final int id;
|
||||
|
||||
public Task(@JsonProperty("name") String name, @JsonProperty("id") int id) {
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Task{" +
|
||||
"name='" + name + '\'' +
|
||||
", id=" + id +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user