Polish WithSecurityContextTestExecutionListener

Extract method for reuse

SecurityContext createSecurityContext(AnnotatedElement annotated,
    WithSecurityContext withSecurityContext,
    TestContext context)

Issue gh-3888
This commit is contained in:
Rob Winch 2016-05-20 09:36:34 -05:00
parent a53d022312
commit 336de35874

View File

@ -65,45 +65,40 @@ public class WithSecurityContextTestExecutionListener extends
} }
} }
@SuppressWarnings({ "rawtypes", "unchecked" })
private SecurityContext createSecurityContext(AnnotatedElement annotated, private SecurityContext createSecurityContext(AnnotatedElement annotated,
TestContext context) { TestContext context) {
WithSecurityContext withSecurityContext = AnnotationUtils.findAnnotation( WithSecurityContext withSecurityContext = AnnotationUtils
annotated, WithSecurityContext.class); .findAnnotation(annotated, WithSecurityContext.class);
if (withSecurityContext != null) { return createSecurityContext(annotated, withSecurityContext, context);
WithSecurityContextFactory factory = createFactory(withSecurityContext, context); }
Class<? extends Annotation> type = (Class<? extends Annotation>) GenericTypeResolver.resolveTypeArgument(factory.getClass(), WithSecurityContextFactory.class);
Annotation annotation = findAnnotation(annotated, type); private SecurityContext createSecurityContext(Class<?> annotated,
try { TestContext context) {
return factory.createSecurityContext(annotation); MetaAnnotationUtils.AnnotationDescriptor<WithSecurityContext> withSecurityContextDescriptor = MetaAnnotationUtils
} .findAnnotationDescriptor(annotated, WithSecurityContext.class);
catch (RuntimeException e) { WithSecurityContext withSecurityContext = withSecurityContextDescriptor == null
throw new IllegalStateException( ? null : withSecurityContextDescriptor.getAnnotation();
"Unable to create SecurityContext using " + annotation, e); return createSecurityContext(annotated, withSecurityContext, context);
}
}
return null;
} }
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "rawtypes", "unchecked" })
private SecurityContext createSecurityContext(Class<?> annotated, private SecurityContext createSecurityContext(AnnotatedElement annotated,
TestContext context) { WithSecurityContext withSecurityContext, TestContext context) {
MetaAnnotationUtils.AnnotationDescriptor<WithSecurityContext> if (withSecurityContext == null) {
withSecurityContext = MetaAnnotationUtils.findAnnotationDescriptor( return null;
annotated, WithSecurityContext.class); }
if (withSecurityContext != null) { WithSecurityContextFactory factory = createFactory(withSecurityContext, context);
WithSecurityContextFactory factory = createFactory(withSecurityContext.getAnnotation(), context); Class<? extends Annotation> type = (Class<? extends Annotation>) GenericTypeResolver
Class<? extends Annotation> type = (Class<? extends Annotation>) GenericTypeResolver.resolveTypeArgument(factory.getClass(), WithSecurityContextFactory.class); .resolveTypeArgument(factory.getClass(),
Annotation annotation = findAnnotation(annotated, type); WithSecurityContextFactory.class);
try { Annotation annotation = findAnnotation(annotated, type);
return factory.createSecurityContext(annotation); try {
} return factory.createSecurityContext(annotation);
catch (RuntimeException e) { }
throw new IllegalStateException( catch (RuntimeException e) {
"Unable to create SecurityContext using " + annotation, e); throw new IllegalStateException(
} "Unable to create SecurityContext using " + annotation, e);
} }
return null;
} }
private Annotation findAnnotation(AnnotatedElement annotated, private Annotation findAnnotation(AnnotatedElement annotated,