1
0
mirror of synced 2026-08-05 17:57:15 +00:00

Support WithSecurityContextFactory on superclass

Fixes gh-3888
This commit is contained in:
Eddú Meléndez
2016-05-20 16:42:34 +10:00
committed by Rob Winch
parent 9f95bfdfc9
commit a53d022312
4 changed files with 112 additions and 2 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -28,6 +28,7 @@ import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequ
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.support.AbstractTestExecutionListener;
import org.springframework.test.util.MetaAnnotationUtils;
import org.springframework.test.web.servlet.MockMvc;
/**
@@ -39,6 +40,7 @@ import org.springframework.test.web.servlet.MockMvc;
* too.
*
* @author Rob Winch
* @author Eddú Meléndez
* @since 4.0
*/
public class WithSecurityContextTestExecutionListener extends
@@ -83,6 +85,27 @@ public class WithSecurityContextTestExecutionListener extends
return null;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private SecurityContext createSecurityContext(Class<?> annotated,
TestContext context) {
MetaAnnotationUtils.AnnotationDescriptor<WithSecurityContext>
withSecurityContext = MetaAnnotationUtils.findAnnotationDescriptor(
annotated, WithSecurityContext.class);
if (withSecurityContext != null) {
WithSecurityContextFactory factory = createFactory(withSecurityContext.getAnnotation(), context);
Class<? extends Annotation> type = (Class<? extends Annotation>) GenericTypeResolver.resolveTypeArgument(factory.getClass(), WithSecurityContextFactory.class);
Annotation annotation = findAnnotation(annotated, type);
try {
return factory.createSecurityContext(annotation);
}
catch (RuntimeException e) {
throw new IllegalStateException(
"Unable to create SecurityContext using " + annotation, e);
}
}
return null;
}
private Annotation findAnnotation(AnnotatedElement annotated,
Class<? extends Annotation> type) {
Annotation findAnnotation = AnnotationUtils.findAnnotation(annotated, type);
@@ -131,4 +154,4 @@ public class WithSecurityContextTestExecutionListener extends
public int getOrder() {
return 1000;
}
}
}