From 8ad91ef6a56dca3f0773dd1a924e50b34f65af65 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Wed, 6 Jul 2016 16:09:17 -0500 Subject: [PATCH] WithSecurityContextTestExecutionListener > SqlScriptsTestExecutionListener WithSecurityContextTestExecutionListener should order after SqlScriptsTestExecutionListener so sql can setup the current user's info in the database. Fixes gh-3962 --- ...hSecurityContextTestExecutionListener.java | 4 +- ...ityContextTestExcecutionListenerTests.java | 90 +++++++++++++- ...rityContextTestExecutionListenerTests.java | 110 ------------------ test/test.gradle | 1 + 4 files changed, 89 insertions(+), 116 deletions(-) delete mode 100644 test/src/test/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListenerTests.java 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 ee17df418f..50304b39da 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 @@ -144,10 +144,10 @@ public class WithSecurityContextTestExecutionListener } /** - * Returns {@code 1000}. + * Returns {@code 10000}. */ @Override public int getOrder() { - return 1000; + return 10000; } } diff --git a/test/src/test/java/org/springframework/security/test/context/support/WithSecurityContextTestExcecutionListenerTests.java b/test/src/test/java/org/springframework/security/test/context/support/WithSecurityContextTestExcecutionListenerTests.java index 696323532a..976c15202e 100644 --- a/test/src/test/java/org/springframework/security/test/context/support/WithSecurityContextTestExcecutionListenerTests.java +++ b/test/src/test/java/org/springframework/security/test/context/support/WithSecurityContextTestExcecutionListenerTests.java @@ -15,9 +15,13 @@ */ package org.springframework.security.test.context.support; -import static org.assertj.core.api.Assertions.*; - -import static org.mockito.Mockito.when; +import java.lang.annotation.Annotation; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import org.junit.After; import org.junit.Before; @@ -25,13 +29,25 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; + import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.AnnotationAwareOrderComparator; +import org.springframework.security.authentication.TestingAuthenticationToken; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.test.context.TestSecurityContextHolder; import org.springframework.test.context.TestContext; +import org.springframework.test.context.TestExecutionListener; +import org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener; +import org.springframework.test.context.support.AbstractTestExecutionListener; import org.springframework.util.ReflectionUtils; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + @RunWith(MockitoJUnitRunner.class) public class WithSecurityContextTestExcecutionListenerTests { private ConfigurableApplicationContext context; @@ -78,7 +94,73 @@ public class WithSecurityContextTestExcecutionListenerTests { listener.beforeTestMethod(testContext); - assertThat(TestSecurityContextHolder.getContext().getAuthentication().getName()).isEqualTo("user"); + assertThat(TestSecurityContextHolder.getContext().getAuthentication().getName()) + .isEqualTo("user"); + } + // gh-3962 + @Test + public void withSecurityContextAfterSqlScripts() { + SqlScriptsTestExecutionListener sql = new SqlScriptsTestExecutionListener(); + WithSecurityContextTestExecutionListener security = new WithSecurityContextTestExecutionListener(); + + List listeners = Arrays.asList(security, sql); + + AnnotationAwareOrderComparator.sort(listeners); + + assertThat(listeners).containsExactly(sql, security); + } + + // SEC-2709 + @Test + public void orderOverridden() { + AbstractTestExecutionListener otherListener = new AbstractTestExecutionListener() { + }; + + List listeners = new ArrayList(); + listeners.add(otherListener); + listeners.add(this.listener); + + AnnotationAwareOrderComparator.sort(listeners); + + assertThat(listeners).containsSequence(this.listener, otherListener); + } + + @Test + // gh-3837 + public void handlesGenericAnnotation() throws Exception { + Method method = ReflectionUtils.findMethod( + WithSecurityContextTestExcecutionListenerTests.class, + "handlesGenericAnnotationTestMethod"); + TestContext testContext = mock(TestContext.class); + when(testContext.getTestMethod()).thenReturn(method); + when(testContext.getApplicationContext()) + .thenThrow(new IllegalStateException("")); + + this.listener.beforeTestMethod(testContext); + + assertThat(SecurityContextHolder.getContext().getAuthentication().getPrincipal()) + .isInstanceOf(WithSuperClassWithSecurityContext.class); + } + + @WithSuperClassWithSecurityContext + public void handlesGenericAnnotationTestMethod() { + } + + @Retention(RetentionPolicy.RUNTIME) + @WithSecurityContext(factory = SuperClassWithSecurityContextFactory.class) + @interface WithSuperClassWithSecurityContext { + String username() default "WithSuperClassWithSecurityContext"; + } + + static class SuperClassWithSecurityContextFactory + implements WithSecurityContextFactory { + + @Override + public SecurityContext createSecurityContext(Annotation annotation) { + SecurityContext context = SecurityContextHolder.createEmptyContext(); + context.setAuthentication(new TestingAuthenticationToken(annotation, "NA")); + return context; + } } static class FakeTest { diff --git a/test/src/test/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListenerTests.java b/test/src/test/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListenerTests.java deleted file mode 100644 index a468115321..0000000000 --- a/test/src/test/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListenerTests.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2002-2015 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.test.context.support; - -import java.lang.annotation.Annotation; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import org.springframework.core.annotation.AnnotationAwareOrderComparator; -import org.springframework.security.authentication.TestingAuthenticationToken; -import org.springframework.security.core.context.SecurityContext; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.test.context.TestContext; -import org.springframework.test.context.TestExecutionListener; -import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; -import org.springframework.util.ReflectionUtils; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -/** - * @author Rob Winch - * - */ -public class WithSecurityContextTestExecutionListenerTests { - WithSecurityContextTestExecutionListener listener; - - @Before - public void setup() { - this.listener = new WithSecurityContextTestExecutionListener(); - } - - @After - public void cleanup() { - SecurityContextHolder.clearContext(); - } - - // SEC-2709 - @Test - public void orderOverridden() { - DependencyInjectionTestExecutionListener otherListener = new DependencyInjectionTestExecutionListener(); - - List listeners = new ArrayList(); - listeners.add(otherListener); - listeners.add(this.listener); - - AnnotationAwareOrderComparator.sort(listeners); - - assertThat(listeners).containsSequence(this.listener, otherListener); - } - - @Test - // gh-3837 - public void handlesGenericAnnotation() throws Exception { - Method method = ReflectionUtils.findMethod( - WithSecurityContextTestExecutionListenerTests.class, - "handlesGenericAnnotationTestMethod"); - TestContext testContext = mock(TestContext.class); - when(testContext.getTestMethod()).thenReturn(method); - when(testContext.getApplicationContext()) - .thenThrow(new IllegalStateException("")); - - this.listener.beforeTestMethod(testContext); - - assertThat(SecurityContextHolder.getContext().getAuthentication().getPrincipal()) - .isInstanceOf(WithSuperClassWithSecurityContext.class); - } - - @WithSuperClassWithSecurityContext - public void handlesGenericAnnotationTestMethod() { - } - - @Retention(RetentionPolicy.RUNTIME) - @WithSecurityContext(factory = SuperClassWithSecurityContextFactory.class) - @interface WithSuperClassWithSecurityContext { - String username() default "WithSuperClassWithSecurityContext"; - } - - static class SuperClassWithSecurityContextFactory - implements WithSecurityContextFactory { - - @Override - public SecurityContext createSecurityContext(Annotation annotation) { - SecurityContext context = SecurityContextHolder.createEmptyContext(); - context.setAuthentication(new TestingAuthenticationToken(annotation, "NA")); - return context; - } - } -} diff --git a/test/test.gradle b/test/test.gradle index 69eaa2c640..e7e312ac30 100644 --- a/test/test.gradle +++ b/test/test.gradle @@ -11,5 +11,6 @@ dependencies { provided "javax.servlet:javax.servlet-api:$servletApiVersion" testCompile "org.springframework:spring-webmvc:$springVersion", + "org.springframework:spring-tx:$springVersion", powerMockDependencies }