Forráskód Böngészése

AutowireBeanFactoryObjectPostProcessorTests groovy->java

Issue: gh-4939
Rob Winch 7 éve
szülő
commit
12bd506ee7

+ 0 - 174
config/src/test/groovy/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessorTests.groovy

@@ -1,174 +0,0 @@
-/*
- * Copyright 2002-2013 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.config.annotation.configuration
-
-import org.springframework.beans.factory.BeanClassLoaderAware
-import org.springframework.beans.factory.BeanFactoryAware
-import org.springframework.beans.factory.DisposableBean
-import org.springframework.beans.factory.SmartInitializingSingleton
-import org.springframework.beans.factory.annotation.Autowired
-import org.springframework.beans.factory.config.AutowireCapableBeanFactory
-import org.springframework.beans.factory.support.BeanNameGenerator;
-import org.springframework.context.ApplicationContextAware
-import org.springframework.context.ApplicationEventPublisherAware
-import org.springframework.context.EnvironmentAware
-import org.springframework.context.MessageSourceAware
-import org.springframework.context.annotation.Bean
-import org.springframework.context.annotation.Configuration
-import org.springframework.context.support.ClassPathXmlApplicationContext
-import org.springframework.mock.web.MockServletConfig
-import org.springframework.mock.web.MockServletContext
-import org.springframework.security.config.annotation.BaseSpringSpec
-import org.springframework.security.config.annotation.ObjectPostProcessor
-import org.springframework.web.context.ServletConfigAware
-import org.springframework.web.context.ServletContextAware
-import org.springframework.web.context.support.AnnotationConfigWebApplicationContext
-
-/**
- *
- * @author Rob Winch
- */
-class AutowireBeanFactoryObjectPostProcessorTests extends BaseSpringSpec {
-
-	def "Verify All Aware methods are invoked"() {
-		setup:
-			ApplicationContextAware contextAware = Mock(ApplicationContextAware)
-			ApplicationEventPublisherAware publisher = Mock(ApplicationEventPublisherAware)
-			BeanClassLoaderAware classloader = Mock(BeanClassLoaderAware)
-			BeanFactoryAware beanFactory = Mock(BeanFactoryAware)
-			EnvironmentAware environment = Mock(EnvironmentAware)
-			MessageSourceAware messageSource = Mock(MessageSourceAware)
-			ServletConfigAware servletConfig = Mock(ServletConfigAware)
-			ServletContextAware servletContext = Mock(ServletContextAware)
-			DisposableBean disposable = Mock(DisposableBean)
-
-			context = new AnnotationConfigWebApplicationContext([servletConfig:new MockServletConfig(),servletContext:new MockServletContext()])
-			context.register(Config)
-			context.refresh()
-			context.start()
-
-			ObjectPostProcessor opp = context.getBean(ObjectPostProcessor)
-		when:
-			opp.postProcess(contextAware)
-		then:
-			1 * contextAware.setApplicationContext(!null)
-
-		when:
-			opp.postProcess(publisher)
-		then:
-			1 * publisher.setApplicationEventPublisher(!null)
-
-		when:
-			opp.postProcess(classloader)
-		then:
-			1 * classloader.setBeanClassLoader(!null)
-
-		when:
-			opp.postProcess(beanFactory)
-		then:
-			1 * beanFactory.setBeanFactory(!null)
-
-		when:
-			opp.postProcess(environment)
-		then:
-			1 * environment.setEnvironment(!null)
-
-		when:
-			opp.postProcess(messageSource)
-		then:
-			1 * messageSource.setMessageSource(!null)
-
-		when:
-			opp.postProcess(servletConfig)
-		then:
-			1 * servletConfig.setServletConfig(!null)
-
-		when:
-			opp.postProcess(servletContext)
-		then:
-			1 * servletContext.setServletContext(!null)
-
-		when:
-			opp.postProcess(disposable)
-			context.close()
-			context = null
-		then:
-			1 * disposable.destroy()
-	}
-
-	@Configuration
-	static class Config {
-		@Bean
-		public ObjectPostProcessor objectPostProcessor(AutowireCapableBeanFactory beanFactory) {
-			return new AutowireBeanFactoryObjectPostProcessor(beanFactory);
-		}
-	}
-
-	def "SEC-2382: AutowireBeanFactoryObjectPostProcessor works with BeanNameAutoProxyCreator"() {
-		when:
-			// must load with XML for BeanPostProcessors to work
-			context = new ClassPathXmlApplicationContext("AutowireBeanFactoryObjectPostProcessorTests-aopconfig.xml", getClass());
-		then:
-			noExceptionThrown()
-		and: "make sure autoproxying was actually enabled"
-			context.getBean(MyAdvisedBean).doStuff() == "null"
-	}
-
-	@Configuration
-	static class WithBanNameAutoProxyCreatorConfig {
-		@Bean
-		public ObjectPostProcessor objectPostProcessor(AutowireCapableBeanFactory beanFactory) {
-			return new AutowireBeanFactoryObjectPostProcessor(beanFactory)
-		}
-
-		@Autowired
-		public void configure(ObjectPostProcessor<Object> p) {
-			p.postProcess(new Object())
-		}
-	}
-
-	def "SmartInitializingSingleton"() {
-		when:
-			context = new AnnotationConfigWebApplicationContext([servletConfig:new MockServletConfig(),servletContext:new MockServletContext()])
-			context.register(SmartConfig)
-			context.refresh()
-			context.start()
-		then:
-			context.getBean(SmartConfig).smart.instantiated
-	}
-
-	@Configuration
-	static class SmartConfig {
-		SmartInitializingSingleton smart = new SmartInitializingSingletonStub()
-
-		@Bean
-		public static ObjectPostProcessor objectPostProcessor(AutowireCapableBeanFactory beanFactory) {
-			return new AutowireBeanFactoryObjectPostProcessor(beanFactory)
-		}
-
-		@Autowired
-		public void configure(ObjectPostProcessor<Object> p) {
-			p.postProcess(smart)
-		}
-	}
-
-	static class SmartInitializingSingletonStub implements SmartInitializingSingleton {
-		boolean instantiated
-		void afterSingletonsInstantiated() {
-			instantiated = true
-		}
-	}
-}

+ 177 - 0
config/src/test/java/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessorTests.java

@@ -0,0 +1,177 @@
+/*
+ * Copyright 2002-2018 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.config.annotation.configuration;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.springframework.beans.factory.BeanClassLoaderAware;
+import org.springframework.beans.factory.BeanFactoryAware;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.factory.SmartInitializingSingleton;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.context.ApplicationEventPublisherAware;
+import org.springframework.context.EnvironmentAware;
+import org.springframework.context.MessageSourceAware;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.security.config.annotation.ObjectPostProcessor;
+import org.springframework.security.config.test.SpringTestRule;
+import org.springframework.web.context.ServletContextAware;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+import static org.mockito.ArgumentMatchers.isNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+/**
+ *
+ * @author Rob Winch
+ */
+public class AutowireBeanFactoryObjectPostProcessorTests {
+	@Rule
+	public final SpringTestRule spring = new SpringTestRule();
+
+	@Autowired
+	private ObjectPostProcessor<Object> objectObjectPostProcessor;
+
+	@Test
+	public void postProcessWhenApplicationContextAwareThenAwareInvoked() {
+		this.spring.register(Config.class).autowire();
+
+		ApplicationContextAware toPostProcess = mock(ApplicationContextAware.class);
+		this.objectObjectPostProcessor.postProcess(toPostProcess);
+		verify(toPostProcess).setApplicationContext(isNotNull());
+	}
+
+	@Test
+	public void postProcessWhenApplicationEventPublisherAwareThenAwareInvoked() {
+		this.spring.register(Config.class).autowire();
+
+		ApplicationEventPublisherAware toPostProcess = mock(ApplicationEventPublisherAware.class);
+		this.objectObjectPostProcessor.postProcess(toPostProcess);
+		verify(toPostProcess).setApplicationEventPublisher(isNotNull());
+
+	}
+
+	@Test
+	public void postProcessWhenBeanClassLoaderAwareThenAwareInvoked() {
+		this.spring.register(Config.class).autowire();
+
+		BeanClassLoaderAware toPostProcess = mock(BeanClassLoaderAware.class);
+		this.objectObjectPostProcessor.postProcess(toPostProcess);
+		verify(toPostProcess).setBeanClassLoader(isNotNull());
+	}
+
+	@Test
+	public void postProcessWhenBeanFactoryAwareThenAwareInvoked() {
+		this.spring.register(Config.class).autowire();
+
+		BeanFactoryAware toPostProcess = mock(BeanFactoryAware.class);
+		this.objectObjectPostProcessor.postProcess(toPostProcess);
+		verify(toPostProcess).setBeanFactory(isNotNull());
+	}
+
+	@Test
+	public void postProcessWhenEnvironmentAwareThenAwareInvoked() {
+		this.spring.register(Config.class).autowire();
+
+		EnvironmentAware toPostProcess = mock(EnvironmentAware.class);
+		this.objectObjectPostProcessor.postProcess(toPostProcess);
+		verify(toPostProcess).setEnvironment(isNotNull());
+	}
+
+	@Test
+	public void postProcessWhenMessageSourceAwareThenAwareInvoked() {
+		this.spring.register(Config.class).autowire();
+
+		MessageSourceAware toPostProcess = mock(MessageSourceAware.class);
+		this.objectObjectPostProcessor.postProcess(toPostProcess);
+		verify(toPostProcess).setMessageSource(isNotNull());
+	}
+
+	@Test
+	public void postProcessWhenServletContextAwareThenAwareInvoked() {
+		this.spring.register(Config.class).autowire();
+
+		ServletContextAware toPostProcess = mock(ServletContextAware.class);
+		this.objectObjectPostProcessor.postProcess(toPostProcess);
+		verify(toPostProcess).setServletContext(isNotNull());
+	}
+
+	@Test
+	public void postProcessWhenDisposableBeanThenAwareInvoked() throws Exception {
+		this.spring.register(Config.class).autowire();
+
+		DisposableBean toPostProcess = mock(DisposableBean.class);
+		this.objectObjectPostProcessor.postProcess(toPostProcess);
+
+		this.spring.getContext().close();
+
+		verify(toPostProcess).destroy();
+	}
+
+	@Configuration
+	static class Config {
+		@Bean
+		public ObjectPostProcessor objectPostProcessor(AutowireCapableBeanFactory beanFactory) {
+			return new AutowireBeanFactoryObjectPostProcessor(beanFactory);
+		}
+	}
+
+	@Test
+	public void postProcessWhenSmartInitializingSingletonThenAwareInvoked() {
+		this.spring.register(Config.class, SmartConfig.class).autowire();
+
+		SmartConfig config = this.spring.getContext().getBean(SmartConfig.class);
+
+		verify(config.toTest).afterSingletonsInstantiated();
+	}
+
+	@Configuration
+	static class SmartConfig {
+		SmartInitializingSingleton toTest = mock(SmartInitializingSingleton.class);
+
+		@Autowired
+		public void configure(ObjectPostProcessor<Object> p) {
+			p.postProcess(this.toTest);
+		}
+	}
+
+	@Test
+	// SEC-2382
+	public void autowireBeanFactoryWhenBeanNameAutoProxyCreatorThenWorks() throws Exception {
+		this.spring.testConfigLocations("AutowireBeanFactoryObjectPostProcessorTests-aopconfig.xml").autowire();
+
+		MyAdvisedBean bean = this.spring.getContext().getBean(MyAdvisedBean.class);
+
+		assertThat(bean.doStuff()).isEqualTo("null");
+	}
+
+	@Configuration
+	static class WithBeanNameAutoProxyCreatorConfig {
+		@Bean
+		public ObjectPostProcessor objectPostProcessor(AutowireCapableBeanFactory beanFactory) {
+			return new AutowireBeanFactoryObjectPostProcessor(beanFactory);
+		}
+
+		@Autowired
+		public void configure(ObjectPostProcessor<Object> p) {
+			p.postProcess(new Object());
+		}
+	}
+}

+ 1 - 1
config/src/test/resources/org/springframework/security/config/annotation/configuration/AutowireBeanFactoryObjectPostProcessorTests-aopconfig.xml

@@ -10,7 +10,7 @@
 
 	<context:annotation-config/>
 
-	<bean class="org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessorTests$WithBanNameAutoProxyCreatorConfig"/>
+	<bean class="org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessorTests$WithBeanNameAutoProxyCreatorConfig"/>
 
 	<bean id="interceptor" class="org.springframework.security.config.annotation.configuration.AroundMethodInterceptor"/>
 	<bean id="myBean" class="org.springframework.security.config.annotation.configuration.MyAdvisedBean"/>