|
Spring Framework example source code file (RequestScopedProxyTests.java)
The Spring Framework RequestScopedProxyTests.java source code/* * Copyright 2002-2007 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(bean.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); } } } Other Spring Framework examples (source code examples)Here is a short list of links related to this Spring Framework RequestScopedProxyTests.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.