/* * Copyright 2002-2008 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.web.context.request; import junit.framework.TestCase; import org.springframework.aop.support.AopUtils; import org.springframework.beans.DerivedTestBean; import org.springframework.beans.ITestBean; import org.springframework.beans.TestBean; import org.springframework.beans.factory.DummyFactory; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.core.io.ClassPathResource; import org.springframework.mock.web.MockHttpServletRequest; /** * @author Juergen Hoeller */ public class RequestScopedProxyTests extends TestCase { private DefaultListableBeanFactory beanFactory; protected void setUp() throws Exception { this.beanFactory = new DefaultListableBeanFactory(); this.beanFactory.registerScope("request", new RequestScope()); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory); reader.loadBeanDefinitions(new ClassPathResource("requestScopedProxyTests.xml", getClass())); this.beanFactory.preInstantiateSingletons(); } public void testGetFromScope() throws Exception { String name = "requestScopedObject"; TestBean bean = (TestBean) this.beanFactory.getBean(name); assertTrue(AopUtils.isCglibProxy(bean)); MockHttpServletRequest request = new MockHttpServletRequest(); RequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); try { assertNull(request.getAttribute("scopedTarget." + name)); assertEquals("scoped", bean.getName()); assertNotNull(request.getAttribute("scopedTarget." + name)); assertEquals(TestBean.class, request.getAttribute("scopedTarget." + name).getClass()); assertEquals("scoped", ((TestBean) request.getAttribute("scopedTarget." + name)).getName()); assertSame(bean, this.beanFactory.getBean(name)); } finally { RequestContextHolder.setRequestAttributes(null); } } public void testGetFromScopeThroughDynamicProxy() throws Exception { String name = "requestScopedProxy"; ITestBean bean = (ITestBean) this.beanFactory.getBean(name); assertTrue(AopUtils.isJdkDynamicProxy(bean)); MockHttpServletRequest request = new MockHttpServletRequest(); RequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); try { assertNull(request.getAttribute("scopedTarget." + name)); assertEquals("scoped", bean.getName()); assertNotNull(request.getAttribute("scopedTarget." + name)); assertEquals(TestBean.class, request.getAttribute("scopedTarget." + name).getClass()); assertEquals("scoped", ((TestBean) request.getAttribute("scopedTarget." + name)).getName()); assertSame(bean, this.beanFactory.getBean(name)); } finally { RequestContextHolder.setRequestAttributes(null); } } public void testDestructionAtRequestCompletion() throws Exception { String name = "requestScopedDisposableObject"; DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name); assertTrue(AopUtils.isCglibProxy(bean)); MockHttpServletRequest request = new MockHttpServletRequest(); ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); try { assertNull(request.getAttribute("scopedTarget." + name)); assertEquals("scoped", bean.getName()); assertNotNull(request.getAttribute("scopedTarget." + name)); assertEquals(DerivedTestBean.class, request.getAttribute("scopedTarget." + name).getClass()); assertEquals("scoped", ((TestBean) request.getAttribute("scopedTarget." + name)).getName()); assertSame(bean, this.beanFactory.getBean(name)); requestAttributes.requestCompleted(); assertTrue(((TestBean) request.getAttribute("scopedTarget." + name)).wasDestroyed()); } finally { RequestContextHolder.setRequestAttributes(null); } } public void testGetFromFactoryBeanInScope() throws Exception { String name = "requestScopedFactoryBean"; TestBean bean = (TestBean) this.beanFactory.getBean(name); assertTrue(AopUtils.isCglibProxy(bean)); MockHttpServletRequest request = new MockHttpServletRequest(); RequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); try { assertNull(request.getAttribute("scopedTarget." + name)); assertEquals(DummyFactory.SINGLETON_NAME, bean.getName()); assertNotNull(request.getAttribute("scopedTarget." + name)); assertEquals(DummyFactory.class, request.getAttribute("scopedTarget." + name).getClass()); assertSame(bean, this.beanFactory.getBean(name)); } finally { RequestContextHolder.setRequestAttributes(null); } } public void testGetInnerBeanFromScope() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("outerBean"); assertFalse(AopUtils.isAopProxy(bean)); assertTrue(AopUtils.isCglibProxy(bean.getSpouse())); String name = "scopedInnerBean"; MockHttpServletRequest request = new MockHttpServletRequest(); RequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); try { assertNull(request.getAttribute("scopedTarget." + name)); assertEquals("scoped", bean.getSpouse().getName()); assertNotNull(request.getAttribute("scopedTarget." + name)); assertEquals(TestBean.class, request.getAttribute("scopedTarget." + name).getClass()); assertEquals("scoped", ((TestBean) request.getAttribute("scopedTarget." + name)).getName()); } finally { RequestContextHolder.setRequestAttributes(null); } } public void testGetAnonymousInnerBeanFromScope() throws Exception { TestBean bean = (TestBean) this.beanFactory.getBean("outerBean"); assertFalse(AopUtils.isAopProxy(bean)); assertTrue(AopUtils.isCglibProxy(bean.getSpouse())); BeanDefinition beanDef = this.beanFactory.getBeanDefinition("outerBean"); BeanDefinitionHolder innerBeanDef = (BeanDefinitionHolder) beanDef.getPropertyValues().getPropertyValue("spouse").getValue(); String name = innerBeanDef.getBeanName(); MockHttpServletRequest request = new MockHttpServletRequest(); RequestAttributes requestAttributes = new ServletRequestAttributes(request); RequestContextHolder.setRequestAttributes(requestAttributes); try { assertNull(request.getAttribute("scopedTarget." + name)); assertEquals("scoped", bean.getSpouse().getName()); assertNotNull(request.getAttribute("scopedTarget." + name)); assertEquals(TestBean.class, request.getAttribute("scopedTarget." + name).getClass()); assertEquals("scoped", ((TestBean) request.getAttribute("scopedTarget." + name)).getName()); } finally { RequestContextHolder.setRequestAttributes(null); } } }