revised web scoping tests

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3572 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Juergen Hoeller 2010-08-12 22:49:26 +00:00
parent 93b98f48ed
commit 7a47dc548c
2 changed files with 30 additions and 14 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2006 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,23 +18,23 @@ package org.springframework.web.context.request;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import junit.framework.TestCase; import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.TestBean; import org.springframework.beans.TestBean;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.StaticWebApplicationContext; import org.springframework.web.context.support.StaticWebApplicationContext;
/** /**
* @author Rod Johnson * @author Rod Johnson
* @author Juergen Hoeller * @author Juergen Hoeller
*/ */
public class RequestAndSessionScopedProxyTests extends TestCase { public class RequestAndSessionScopedBeanTests {
@Test
public void testPutBeanInRequest() throws Exception { public void testPutBeanInRequest() throws Exception {
String targetBeanName = "target"; String targetBeanName = "target";
@ -73,6 +73,7 @@ public class RequestAndSessionScopedProxyTests extends TestCase {
} }
} }
@Test
public void testPutBeanInSession() throws Exception { public void testPutBeanInSession() throws Exception {
String targetBeanName = "target"; String targetBeanName = "target";
HttpServletRequest request = new MockHttpServletRequest(); HttpServletRequest request = new MockHttpServletRequest();
@ -97,6 +98,8 @@ public class RequestAndSessionScopedProxyTests extends TestCase {
catch (BeanCreationException ex) { catch (BeanCreationException ex) {
// expected // expected
} }
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,9 @@
package org.springframework.web.context.request; package org.springframework.web.context.request;
import junit.framework.TestCase; import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
import org.springframework.beans.DerivedTestBean; import org.springframework.beans.DerivedTestBean;
@ -33,11 +35,12 @@ import org.springframework.mock.web.MockHttpServletRequest;
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
*/ */
public class RequestScopedProxyTests extends TestCase { public class RequestScopedProxyTests {
private DefaultListableBeanFactory beanFactory; private DefaultListableBeanFactory beanFactory;
protected void setUp() throws Exception { @Before
public void setUp() throws Exception {
this.beanFactory = new DefaultListableBeanFactory(); this.beanFactory = new DefaultListableBeanFactory();
this.beanFactory.registerScope("request", new RequestScope()); this.beanFactory.registerScope("request", new RequestScope());
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this.beanFactory);
@ -45,6 +48,7 @@ public class RequestScopedProxyTests extends TestCase {
this.beanFactory.preInstantiateSingletons(); this.beanFactory.preInstantiateSingletons();
} }
@Test
public void testGetFromScope() throws Exception { public void testGetFromScope() throws Exception {
String name = "requestScopedObject"; String name = "requestScopedObject";
TestBean bean = (TestBean) this.beanFactory.getBean(name); TestBean bean = (TestBean) this.beanFactory.getBean(name);
@ -58,15 +62,18 @@ public class RequestScopedProxyTests extends TestCase {
assertNull(request.getAttribute("scopedTarget." + name)); assertNull(request.getAttribute("scopedTarget." + name));
assertEquals("scoped", bean.getName()); assertEquals("scoped", bean.getName());
assertNotNull(request.getAttribute("scopedTarget." + name)); assertNotNull(request.getAttribute("scopedTarget." + name));
assertEquals(TestBean.class, request.getAttribute("scopedTarget." + name).getClass()); TestBean target = (TestBean) request.getAttribute("scopedTarget." + name);
assertEquals("scoped", ((TestBean) request.getAttribute("scopedTarget." + name)).getName()); assertEquals(TestBean.class, target.getClass());
assertEquals("scoped", target.getName());
assertSame(bean, this.beanFactory.getBean(name)); assertSame(bean, this.beanFactory.getBean(name));
assertEquals(bean.toString(), target.toString());
} }
finally { finally {
RequestContextHolder.setRequestAttributes(null); RequestContextHolder.setRequestAttributes(null);
} }
} }
@Test
public void testGetFromScopeThroughDynamicProxy() throws Exception { public void testGetFromScopeThroughDynamicProxy() throws Exception {
String name = "requestScopedProxy"; String name = "requestScopedProxy";
ITestBean bean = (ITestBean) this.beanFactory.getBean(name); ITestBean bean = (ITestBean) this.beanFactory.getBean(name);
@ -80,15 +87,18 @@ public class RequestScopedProxyTests extends TestCase {
assertNull(request.getAttribute("scopedTarget." + name)); assertNull(request.getAttribute("scopedTarget." + name));
assertEquals("scoped", bean.getName()); assertEquals("scoped", bean.getName());
assertNotNull(request.getAttribute("scopedTarget." + name)); assertNotNull(request.getAttribute("scopedTarget." + name));
assertEquals(TestBean.class, request.getAttribute("scopedTarget." + name).getClass()); TestBean target = (TestBean) request.getAttribute("scopedTarget." + name);
assertEquals("scoped", ((TestBean) request.getAttribute("scopedTarget." + name)).getName()); assertEquals(TestBean.class, target.getClass());
assertEquals("scoped", target.getName());
assertSame(bean, this.beanFactory.getBean(name)); assertSame(bean, this.beanFactory.getBean(name));
assertEquals(bean.toString(), target.toString());
} }
finally { finally {
RequestContextHolder.setRequestAttributes(null); RequestContextHolder.setRequestAttributes(null);
} }
} }
@Test
public void testDestructionAtRequestCompletion() throws Exception { public void testDestructionAtRequestCompletion() throws Exception {
String name = "requestScopedDisposableObject"; String name = "requestScopedDisposableObject";
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name); DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
@ -114,6 +124,7 @@ public class RequestScopedProxyTests extends TestCase {
} }
} }
@Test
public void testGetFromFactoryBeanInScope() throws Exception { public void testGetFromFactoryBeanInScope() throws Exception {
String name = "requestScopedFactoryBean"; String name = "requestScopedFactoryBean";
TestBean bean = (TestBean) this.beanFactory.getBean(name); TestBean bean = (TestBean) this.beanFactory.getBean(name);
@ -135,6 +146,7 @@ public class RequestScopedProxyTests extends TestCase {
} }
} }
@Test
public void testGetInnerBeanFromScope() throws Exception { public void testGetInnerBeanFromScope() throws Exception {
TestBean bean = (TestBean) this.beanFactory.getBean("outerBean"); TestBean bean = (TestBean) this.beanFactory.getBean("outerBean");
assertFalse(AopUtils.isAopProxy(bean)); assertFalse(AopUtils.isAopProxy(bean));
@ -158,6 +170,7 @@ public class RequestScopedProxyTests extends TestCase {
} }
} }
@Test
public void testGetAnonymousInnerBeanFromScope() throws Exception { public void testGetAnonymousInnerBeanFromScope() throws Exception {
TestBean bean = (TestBean) this.beanFactory.getBean("outerBean"); TestBean bean = (TestBean) this.beanFactory.getBean("outerBean");
assertFalse(AopUtils.isAopProxy(bean)); assertFalse(AopUtils.isAopProxy(bean));