SEC-2709: Fix WithSecurityContextTestExecutionListener Order

This commit is contained in:
Rob Winch 2015-08-20 10:41:09 -05:00
parent 327695ab0c
commit 5f328b1178
2 changed files with 62 additions and 9 deletions

View File

@ -17,14 +17,10 @@ package org.springframework.security.test.context.support;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.TypeVariable;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.GenericTypeResolver;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.context.TestSecurityContextHolder;
@ -32,11 +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.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
@ -49,7 +41,6 @@ import org.springframework.util.ReflectionUtils;
* @author Rob Winch
* @since 4.0
*/
@Order(1000)
public class WithSecurityContextTestExecutionListener extends
AbstractTestExecutionListener {
@ -115,4 +106,12 @@ public class WithSecurityContextTestExecutionListener extends
public void afterTestMethod(TestContext testContext) throws Exception {
TestSecurityContextHolder.clearContext();
}
/**
* Returns {@code 1000}.
*/
@Override
public int getOrder() {
return 1000;
}
}

View File

@ -0,0 +1,54 @@
/*
* 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 static org.fest.assertions.Assertions.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
/**
* @author Rob Winch
*
*/
public class WithSecurityContextTestExecutionListenerTests {
WithSecurityContextTestExecutionListener listener;
@Before
public void setup() {
listener = new WithSecurityContextTestExecutionListener();
}
// SEC-2709
@Test
public void orderOverridden() {
DependencyInjectionTestExecutionListener otherListener = new DependencyInjectionTestExecutionListener();
List<TestExecutionListener> listeners = new ArrayList<TestExecutionListener>();
listeners.add(otherListener);
listeners.add(listener);
AnnotationAwareOrderComparator.sort(listeners);
assertThat(listeners).containsSequence(listener, otherListener);
}
}