Browse Source

Support WithSecurityContextFactory on superclass

Fixes gh-3888
Eddú Meléndez 9 years ago
parent
commit
a53d022312

+ 1 - 0
gradle/javaprojects.gradle

@@ -215,6 +215,7 @@ javadoc {
 			'Spring Security ACL':['org.springframework.security.acls*'],
 			'Spring Security Config':['org.springframework.security.config*'],
 			'Spring Security Taglibs':['org.springframework.security.taglibs*'],
+			'Spring Security Test':['org.springframework.security.test*'],
 		]
 		addStringOption('-quiet')
 	}

+ 25 - 2
test/src/main/java/org/springframework/security/test/context/support/WithSecurityContextTestExecutionListener.java

@@ -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;
 	}
-}
+}

+ 26 - 0
test/src/test/java/org/springframework/security/test/context/showcase/WithMockUserParent.java

@@ -0,0 +1,26 @@
+/*
+ * 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.
+ * 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.showcase;
+
+import org.springframework.security.test.context.support.WithMockUser;
+
+/**
+ * @author Eddú Meléndez
+ */
+@WithMockUser
+public class WithMockUserParent {
+
+}

+ 60 - 0
test/src/test/java/org/springframework/security/test/context/showcase/WithMockUserParentTests.java

@@ -0,0 +1,60 @@
+/*
+ * 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.
+ * 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.showcase;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
+import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
+import org.springframework.security.test.context.showcase.service.HelloMessageService;
+import org.springframework.security.test.context.showcase.service.MessageService;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * @author Eddú Meléndez
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(classes = WithMockUserParentTests.Config.class)
+public class WithMockUserParentTests extends WithMockUserParent {
+
+	@Autowired
+	private MessageService messageService;
+
+	@Test
+	public void getMessageWithMockUser() {
+		String message = messageService.getMessage();
+		assertThat(message).contains("user");
+	}
+
+	@EnableGlobalMethodSecurity(prePostEnabled = true)
+	@ComponentScan(basePackageClasses = HelloMessageService.class)
+	static class Config {
+		// @formatter:off
+		@Autowired
+		public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
+			auth
+				.inMemoryAuthentication()
+					.withUser("user").password("password").roles("USER");
+		}
+		// @formatter:on
+	}
+}