|
@@ -17,7 +17,9 @@ package org.springframework.security.test.context.support;
|
|
|
|
|
|
import java.lang.annotation.Annotation;
|
|
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.context.support.StaticApplicationContext;
|
|
|
import org.springframework.core.annotation.AnnotationUtils;
|
|
|
import org.springframework.core.annotation.Order;
|
|
|
import org.springframework.security.core.context.SecurityContext;
|
|
@@ -28,7 +30,10 @@ 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.DependencyInjectionTestExecutionListener;
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
|
+import org.springframework.util.ClassUtils;
|
|
|
+import org.springframework.util.ReflectionUtils;
|
|
|
|
|
|
/**
|
|
|
* A {@link TestExecutionListener} that will find annotations that are annotated with
|
|
@@ -45,6 +50,7 @@ import org.springframework.test.web.servlet.MockMvc;
|
|
|
public class WithSecurityContextTestExecutionListener extends
|
|
|
AbstractTestExecutionListener {
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* Sets up the {@link SecurityContext} for each test method. First the specific method
|
|
|
* is inspected for a {@link WithSecurityContext} or {@link Annotation} that has
|
|
@@ -55,12 +61,11 @@ public class WithSecurityContextTestExecutionListener extends
|
|
|
public void beforeTestMethod(TestContext testContext) throws Exception {
|
|
|
Annotation[] methodAnnotations = AnnotationUtils.getAnnotations(testContext
|
|
|
.getTestMethod());
|
|
|
- ApplicationContext context = testContext.getApplicationContext();
|
|
|
SecurityContext securityContext = createSecurityContext(methodAnnotations,
|
|
|
- context);
|
|
|
+ testContext);
|
|
|
if (securityContext == null) {
|
|
|
Annotation[] classAnnotations = testContext.getTestClass().getAnnotations();
|
|
|
- securityContext = createSecurityContext(classAnnotations, context);
|
|
|
+ securityContext = createSecurityContext(classAnnotations, testContext);
|
|
|
}
|
|
|
if (securityContext != null) {
|
|
|
TestSecurityContextHolder.setContext(securityContext);
|
|
@@ -69,7 +74,7 @@ public class WithSecurityContextTestExecutionListener extends
|
|
|
|
|
|
@SuppressWarnings({ "rawtypes", "unchecked" })
|
|
|
private SecurityContext createSecurityContext(Annotation[] annotations,
|
|
|
- ApplicationContext context) {
|
|
|
+ TestContext context) {
|
|
|
for (Annotation a : annotations) {
|
|
|
WithSecurityContext withUser = AnnotationUtils.findAnnotation(
|
|
|
a.annotationType(), WithSecurityContext.class);
|
|
@@ -88,11 +93,14 @@ public class WithSecurityContextTestExecutionListener extends
|
|
|
}
|
|
|
|
|
|
private WithSecurityContextFactory<? extends Annotation> createFactory(
|
|
|
- WithSecurityContext withUser, ApplicationContext context) {
|
|
|
+ WithSecurityContext withUser, TestContext testContext) {
|
|
|
Class<? extends WithSecurityContextFactory<? extends Annotation>> clazz = withUser
|
|
|
.factory();
|
|
|
try {
|
|
|
- return context.getAutowireCapableBeanFactory().createBean(clazz);
|
|
|
+ return testContext.getApplicationContext().getAutowireCapableBeanFactory().createBean(clazz);
|
|
|
+ }
|
|
|
+ catch (IllegalStateException e) {
|
|
|
+ return BeanUtils.instantiateClass(clazz);
|
|
|
}
|
|
|
catch (Exception e) {
|
|
|
throw new RuntimeException(e);
|