Moved tests from testsuite to web
This commit is contained in:
@@ -1,102 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2006 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 javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class RequestAndSessionScopedProxyTests extends TestCase {
|
||||
|
||||
public void testPutBeanInRequest() throws Exception {
|
||||
String targetBeanName = "target";
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
|
||||
bd.setScope(WebApplicationContext.SCOPE_REQUEST);
|
||||
bd.getPropertyValues().addPropertyValue("name", "abc");
|
||||
wac.registerBeanDefinition(targetBeanName, bd);
|
||||
wac.refresh();
|
||||
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||
TestBean target = (TestBean) wac.getBean(targetBeanName);
|
||||
assertEquals("abc", target.getName());
|
||||
assertSame(target, request.getAttribute(targetBeanName));
|
||||
|
||||
TestBean target2 = (TestBean) wac.getBean(targetBeanName);
|
||||
assertEquals("abc", target2.getName());
|
||||
assertSame(target2, target);
|
||||
assertSame(target2, request.getAttribute(targetBeanName));
|
||||
|
||||
request = new MockHttpServletRequest();
|
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||
TestBean target3 = (TestBean) wac.getBean(targetBeanName);
|
||||
assertEquals("abc", target3.getName());
|
||||
assertSame(target3, request.getAttribute(targetBeanName));
|
||||
assertNotSame(target3, target);
|
||||
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
try {
|
||||
wac.getBean(targetBeanName);
|
||||
fail("Should have thrown BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testPutBeanInSession() throws Exception {
|
||||
String targetBeanName = "target";
|
||||
HttpServletRequest request = new MockHttpServletRequest();
|
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
|
||||
bd.setScope(WebApplicationContext.SCOPE_SESSION);
|
||||
bd.getPropertyValues().addPropertyValue("name", "abc");
|
||||
wac.registerBeanDefinition(targetBeanName, bd);
|
||||
wac.refresh();
|
||||
|
||||
TestBean target = (TestBean) wac.getBean(targetBeanName);
|
||||
assertEquals("abc", target.getName());
|
||||
assertSame(target, request.getSession().getAttribute(targetBeanName));
|
||||
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
try {
|
||||
wac.getBean(targetBeanName);
|
||||
fail("Should have thrown BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
/*
|
||||
* 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 javax.servlet.ServletRequestEvent;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.core.task.MockRunnable;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class RequestContextListenerTests extends TestCase {
|
||||
|
||||
public void testRequestContextListenerWithSameThread() {
|
||||
RequestContextListener listener = new RequestContextListener();
|
||||
MockServletContext context = new MockServletContext();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(context);
|
||||
request.setAttribute("test", "value");
|
||||
|
||||
assertNull(RequestContextHolder.getRequestAttributes());
|
||||
listener.requestInitialized(new ServletRequestEvent(context, request));
|
||||
assertNotNull(RequestContextHolder.getRequestAttributes());
|
||||
assertEquals("value",
|
||||
RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
|
||||
MockRunnable runnable = new MockRunnable();
|
||||
RequestContextHolder.getRequestAttributes().registerDestructionCallback(
|
||||
"test", runnable, RequestAttributes.SCOPE_REQUEST);
|
||||
|
||||
listener.requestDestroyed(new ServletRequestEvent(context, request));
|
||||
assertNull(RequestContextHolder.getRequestAttributes());
|
||||
assertTrue(runnable.wasExecuted());
|
||||
}
|
||||
|
||||
public void testRequestContextListenerWithSameThreadAndAttributesGone() {
|
||||
RequestContextListener listener = new RequestContextListener();
|
||||
MockServletContext context = new MockServletContext();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(context);
|
||||
request.setAttribute("test", "value");
|
||||
|
||||
assertNull(RequestContextHolder.getRequestAttributes());
|
||||
listener.requestInitialized(new ServletRequestEvent(context, request));
|
||||
assertNotNull(RequestContextHolder.getRequestAttributes());
|
||||
assertEquals("value",
|
||||
RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
|
||||
MockRunnable runnable = new MockRunnable();
|
||||
RequestContextHolder.getRequestAttributes().registerDestructionCallback(
|
||||
"test", runnable, RequestAttributes.SCOPE_REQUEST);
|
||||
|
||||
request.clearAttributes();
|
||||
listener.requestDestroyed(new ServletRequestEvent(context, request));
|
||||
assertNull(RequestContextHolder.getRequestAttributes());
|
||||
assertTrue(runnable.wasExecuted());
|
||||
}
|
||||
|
||||
public void testRequestContextListenerWithDifferentThread() {
|
||||
final RequestContextListener listener = new RequestContextListener();
|
||||
final MockServletContext context = new MockServletContext();
|
||||
final MockHttpServletRequest request = new MockHttpServletRequest(context);
|
||||
request.setAttribute("test", "value");
|
||||
|
||||
assertNull(RequestContextHolder.getRequestAttributes());
|
||||
listener.requestInitialized(new ServletRequestEvent(context, request));
|
||||
assertNotNull(RequestContextHolder.getRequestAttributes());
|
||||
assertEquals("value",
|
||||
RequestContextHolder.getRequestAttributes().getAttribute("test", RequestAttributes.SCOPE_REQUEST));
|
||||
MockRunnable runnable = new MockRunnable();
|
||||
RequestContextHolder.getRequestAttributes().registerDestructionCallback(
|
||||
"test", runnable, RequestAttributes.SCOPE_REQUEST);
|
||||
|
||||
// Execute requestDestroyed callback in different thread.
|
||||
Thread thread = new Thread() {
|
||||
public void run() {
|
||||
listener.requestDestroyed(new ServletRequestEvent(context, request));
|
||||
}
|
||||
};
|
||||
thread.start();
|
||||
try {
|
||||
thread.join();
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
}
|
||||
// Still bound to original thread, but at least completed.
|
||||
assertNotNull(RequestContextHolder.getRequestAttributes());
|
||||
assertTrue(runnable.wasExecuted());
|
||||
|
||||
// Check that a repeated execution in the same thread works and performs cleanup.
|
||||
listener.requestInitialized(new ServletRequestEvent(context, request));
|
||||
listener.requestDestroyed(new ServletRequestEvent(context, request));
|
||||
assertNull(RequestContextHolder.getRequestAttributes());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2006 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.beans.DerivedTestBean;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
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 Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
* @author Mark Fisher
|
||||
* @since 2.0
|
||||
*/
|
||||
public class RequestScopeTests 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("requestScopeTests.xml", getClass()));
|
||||
this.beanFactory.preInstantiateSingletons();
|
||||
}
|
||||
|
||||
public void testGetFromScope() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||
|
||||
try {
|
||||
String name = "requestScopedObject";
|
||||
assertNull(request.getAttribute(name));
|
||||
TestBean bean = (TestBean) this.beanFactory.getBean(name);
|
||||
assertSame(bean, request.getAttribute(name));
|
||||
assertSame(bean, this.beanFactory.getBean(name));
|
||||
}
|
||||
finally {
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void testDestructionAtRequestCompletion() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||
|
||||
try {
|
||||
String name = "requestScopedDisposableObject";
|
||||
assertNull(request.getAttribute(name));
|
||||
DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
|
||||
assertSame(bean, request.getAttribute(name));
|
||||
assertSame(bean, this.beanFactory.getBean(name));
|
||||
|
||||
requestAttributes.requestCompleted();
|
||||
assertTrue(bean.wasDestroyed());
|
||||
}
|
||||
finally {
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetFromFactoryBeanInScope() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||
|
||||
try {
|
||||
String name = "requestScopedFactoryBean";
|
||||
assertNull(request.getAttribute(name));
|
||||
TestBean bean = (TestBean) this.beanFactory.getBean(name);
|
||||
assertTrue(request.getAttribute(name) instanceof FactoryBean);
|
||||
assertSame(bean, this.beanFactory.getBean(name));
|
||||
}
|
||||
finally {
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void testCircleLeadsToException() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
RequestAttributes requestAttributes = new ServletRequestAttributes(request);
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||
|
||||
try {
|
||||
String name = "requestScopedObjectCircle1";
|
||||
assertNull(request.getAttribute(name));
|
||||
this.beanFactory.getBean(name);
|
||||
fail("Should have thrown BeanCreationException");
|
||||
}
|
||||
catch (BeanCreationException ex) {
|
||||
// expected
|
||||
assertTrue(ex.contains(BeanCurrentlyInCreationException.class));
|
||||
}
|
||||
finally {
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void testInnerBeanInheritsContainingBeanScopeByDefault() {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||
|
||||
try {
|
||||
String outerBeanName = "requestScopedOuterBean";
|
||||
assertNull(request.getAttribute(outerBeanName));
|
||||
TestBean outer1 = (TestBean) this.beanFactory.getBean(outerBeanName);
|
||||
assertNotNull(request.getAttribute(outerBeanName));
|
||||
TestBean inner1 = (TestBean) outer1.getSpouse();
|
||||
assertSame(outer1, this.beanFactory.getBean(outerBeanName));
|
||||
requestAttributes.requestCompleted();
|
||||
assertTrue(outer1.wasDestroyed());
|
||||
assertTrue(inner1.wasDestroyed());
|
||||
request = new MockHttpServletRequest();
|
||||
requestAttributes = new ServletRequestAttributes(request);
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||
TestBean outer2 = (TestBean) this.beanFactory.getBean(outerBeanName);
|
||||
assertNotSame(outer1, outer2);
|
||||
assertNotSame(inner1, outer2.getSpouse());
|
||||
}
|
||||
finally {
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
}
|
||||
}
|
||||
|
||||
public void testRequestScopedInnerBeanDestroyedWhileContainedBySingleton() throws Exception {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
|
||||
RequestContextHolder.setRequestAttributes(requestAttributes);
|
||||
|
||||
try {
|
||||
String outerBeanName = "singletonOuterBean";
|
||||
TestBean outer1 = (TestBean) this.beanFactory.getBean(outerBeanName);
|
||||
assertNull(request.getAttribute(outerBeanName));
|
||||
TestBean inner1 = (TestBean) outer1.getSpouse();
|
||||
TestBean outer2 = (TestBean) this.beanFactory.getBean(outerBeanName);
|
||||
assertSame(outer1, outer2);
|
||||
assertSame(inner1, outer2.getSpouse());
|
||||
requestAttributes.requestCompleted();
|
||||
assertTrue(inner1.wasDestroyed());
|
||||
assertFalse(outer1.wasDestroyed());
|
||||
}
|
||||
finally {
|
||||
RequestContextHolder.setRequestAttributes(null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,187 +0,0 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user