From 336de35874ce5ffbbf93b81965b5abf58ef9c1d2 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Fri, 20 May 2016 09:36:34 -0500 Subject: [PATCH] Polish WithSecurityContextTestExecutionListener Extract method for reuse SecurityContext createSecurityContext(AnnotatedElement annotated, WithSecurityContext withSecurityContext, TestContext context) Issue gh-3888 --- ...hSecurityContextTestExecutionListener.java | 61 +++++++++---------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/test/src/main/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListener.java b/test/src/main/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListener.java index 3cfb27d200..f2f0ee66dc 100644 --- a/test/src/main/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListener.java +++ b/test/src/main/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListener.java @@ -65,45 +65,40 @@ public class WithSecurityContextTestExecutionListener extends } } - @SuppressWarnings({ "rawtypes", "unchecked" }) private SecurityContext createSecurityContext(AnnotatedElement annotated, TestContext context) { - WithSecurityContext withSecurityContext = AnnotationUtils.findAnnotation( - annotated, WithSecurityContext.class); - if (withSecurityContext != null) { - WithSecurityContextFactory factory = createFactory(withSecurityContext, context); - Class type = (Class) 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; + WithSecurityContext withSecurityContext = AnnotationUtils + .findAnnotation(annotated, WithSecurityContext.class); + return createSecurityContext(annotated, withSecurityContext, context); + } + + private SecurityContext createSecurityContext(Class annotated, + TestContext context) { + MetaAnnotationUtils.AnnotationDescriptor withSecurityContextDescriptor = MetaAnnotationUtils + .findAnnotationDescriptor(annotated, WithSecurityContext.class); + WithSecurityContext withSecurityContext = withSecurityContextDescriptor == null + ? null : withSecurityContextDescriptor.getAnnotation(); + return createSecurityContext(annotated, withSecurityContext, context); } @SuppressWarnings({ "rawtypes", "unchecked" }) - private SecurityContext createSecurityContext(Class annotated, - TestContext context) { - MetaAnnotationUtils.AnnotationDescriptor - withSecurityContext = MetaAnnotationUtils.findAnnotationDescriptor( - annotated, WithSecurityContext.class); - if (withSecurityContext != null) { - WithSecurityContextFactory factory = createFactory(withSecurityContext.getAnnotation(), context); - Class type = (Class) 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); - } + private SecurityContext createSecurityContext(AnnotatedElement annotated, + WithSecurityContext withSecurityContext, TestContext context) { + if (withSecurityContext == null) { + return null; + } + WithSecurityContextFactory factory = createFactory(withSecurityContext, context); + Class type = (Class) 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,