org.springframework.tests.sample.beans.DerivedTestBean Java Examples

The following examples show how to use org.springframework.tests.sample.beans.DerivedTestBean. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebApplicationContextScopeTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSessionScope() {
	WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_SESSION);
	MockHttpServletRequest request = new MockHttpServletRequest();
	ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);
	try {
		assertNull(request.getSession().getAttribute(NAME));
		DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
		assertSame(bean, request.getSession().getAttribute(NAME));
		assertSame(bean, ac.getBean(NAME));
		request.getSession().invalidate();
		assertTrue(bean.wasDestroyed());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example #2
Source File: DefaultSingletonBeanRegistryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testDisposableBean() {
	DefaultSingletonBeanRegistry beanRegistry = new DefaultSingletonBeanRegistry();

	DerivedTestBean tb = new DerivedTestBean();
	beanRegistry.registerSingleton("tb", tb);
	beanRegistry.registerDisposableBean("tb", tb);
	assertSame(tb, beanRegistry.getSingleton("tb"));

	assertSame(tb, beanRegistry.getSingleton("tb"));
	assertEquals(1, beanRegistry.getSingletonCount());
	String[] names = beanRegistry.getSingletonNames();
	assertEquals(1, names.length);
	assertEquals("tb", names[0]);
	assertFalse(tb.wasDestroyed());

	beanRegistry.destroySingletons();
	assertEquals(0, beanRegistry.getSingletonCount());
	assertEquals(0, beanRegistry.getSingletonNames().length);
	assertTrue(tb.wasDestroyed());
}
 
Example #3
Source File: ConcurrencyThrottleInterceptorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSerializable() throws Exception {
	DerivedTestBean tb = new DerivedTestBean();
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setInterfaces(ITestBean.class);
	ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
	proxyFactory.addAdvice(cti);
	proxyFactory.setTarget(tb);
	ITestBean proxy = (ITestBean) proxyFactory.getProxy();
	proxy.getAge();

	ITestBean serializedProxy = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy);
	Advised advised = (Advised) serializedProxy;
	ConcurrencyThrottleInterceptor serializedCti =
			(ConcurrencyThrottleInterceptor) advised.getAdvisors()[0].getAdvice();
	assertEquals(cti.getConcurrencyLimit(), serializedCti.getConcurrencyLimit());
	serializedProxy.getAge();
}
 
Example #4
Source File: RequestScopedProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
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);
	}
}
 
Example #5
Source File: WebApplicationContextScopeTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testSessionScope() {
	WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_SESSION);
	MockHttpServletRequest request = new MockHttpServletRequest();
	ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);
	try {
		assertNull(request.getSession().getAttribute(NAME));
		DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
		assertSame(bean, request.getSession().getAttribute(NAME));
		assertSame(bean, ac.getBean(NAME));
		request.getSession().invalidate();
		assertTrue(bean.wasDestroyed());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example #6
Source File: BeanFactoryTransactionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testGetsAreNotTransactionalWithProxyFactory3() {
	ITestBean testBean = (ITestBean) factory.getBean("proxyFactory3");
	assertTrue("testBean is a full proxy", testBean instanceof DerivedTestBean);
	assertTrue(testBean instanceof TransactionalProxy);
	InvocationCounterPointcut txnCounter = (InvocationCounterPointcut) factory.getBean("txnInvocationCounterPointcut");
	InvocationCounterInterceptor preCounter = (InvocationCounterInterceptor) factory.getBean("preInvocationCounterInterceptor");
	InvocationCounterInterceptor postCounter = (InvocationCounterInterceptor) factory.getBean("postInvocationCounterInterceptor");
	txnCounter.counter = 0;
	preCounter.counter = 0;
	postCounter.counter = 0;
	doTestGetsAreNotTransactional(testBean);
	// Can't assert it's equal to 4 as the pointcut may be optimized and only invoked once
	assertTrue(0 < txnCounter.counter && txnCounter.counter <= 4);
	assertEquals(4, preCounter.counter);
	assertEquals(4, postCounter.counter);
}
 
Example #7
Source File: DefaultSingletonBeanRegistryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testDisposableBean() {
	DefaultSingletonBeanRegistry beanRegistry = new DefaultSingletonBeanRegistry();

	DerivedTestBean tb = new DerivedTestBean();
	beanRegistry.registerSingleton("tb", tb);
	beanRegistry.registerDisposableBean("tb", tb);
	assertSame(tb, beanRegistry.getSingleton("tb"));

	assertSame(tb, beanRegistry.getSingleton("tb"));
	assertEquals(1, beanRegistry.getSingletonCount());
	String[] names = beanRegistry.getSingletonNames();
	assertEquals(1, names.length);
	assertEquals("tb", names[0]);
	assertFalse(tb.wasDestroyed());

	beanRegistry.destroySingletons();
	assertEquals(0, beanRegistry.getSingletonCount());
	assertEquals(0, beanRegistry.getSingletonNames().length);
	assertTrue(tb.wasDestroyed());
}
 
Example #8
Source File: SessionScopeTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void destructionAtSessionTermination() throws Exception {
	MockHttpSession session = new MockHttpSession();
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setSession(session);
	ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);

	RequestContextHolder.setRequestAttributes(requestAttributes);
	String name = "sessionScopedDisposableObject";
	assertNull(session.getAttribute(name));
	DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
	assertEquals(session.getAttribute(name), bean);
	assertSame(bean, this.beanFactory.getBean(name));

	requestAttributes.requestCompleted();
	session.invalidate();
	assertTrue(bean.wasDestroyed());
}
 
Example #9
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPrototypeWithArrayConversionForConstructor() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	List<String> list = new ManagedList<>();
	list.add("myName");
	list.add("myBeanName");
	RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bd.getConstructorArgumentValues().addGenericArgumentValue(list);
	lbf.registerBeanDefinition("test", bd);
	DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test");
	assertEquals("myName", tb.getName());
	assertEquals("myBeanName", tb.getBeanName());
	DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test");
	assertTrue(tb != tb2);
	assertEquals("myName", tb2.getName());
	assertEquals("myBeanName", tb2.getBeanName());
}
 
Example #10
Source File: DefaultListableBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPrototypeWithArrayConversionForFactoryMethod() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	List<String> list = new ManagedList<>();
	list.add("myName");
	list.add("myBeanName");
	RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bd.setFactoryMethodName("create");
	bd.getConstructorArgumentValues().addGenericArgumentValue(list);
	lbf.registerBeanDefinition("test", bd);
	DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test");
	assertEquals("myName", tb.getName());
	assertEquals("myBeanName", tb.getBeanName());
	DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test");
	assertTrue(tb != tb2);
	assertEquals("myName", tb2.getName());
	assertEquals("myBeanName", tb2.getBeanName());
}
 
Example #11
Source File: ConcurrencyThrottleInterceptorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSerializable() throws Exception {
	DerivedTestBean tb = new DerivedTestBean();
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setInterfaces(ITestBean.class);
	ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
	proxyFactory.addAdvice(cti);
	proxyFactory.setTarget(tb);
	ITestBean proxy = (ITestBean) proxyFactory.getProxy();
	proxy.getAge();

	ITestBean serializedProxy = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy);
	Advised advised = (Advised) serializedProxy;
	ConcurrencyThrottleInterceptor serializedCti =
			(ConcurrencyThrottleInterceptor) advised.getAdvisors()[0].getAdvice();
	assertEquals(cti.getConcurrencyLimit(), serializedCti.getConcurrencyLimit());
	serializedProxy.getAge();
}
 
Example #12
Source File: JndiObjectFactoryBeanTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testLookupWithProxyInterfaceAndExpectedTypeAndNoMatch() {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	TestBean tb = new TestBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
	jof.setJndiName("foo");
	jof.setExpectedType(DerivedTestBean.class);
	jof.setProxyInterface(ITestBean.class);
	try {
		jof.afterPropertiesSet();
		fail("Should have thrown NamingException");
	}
	catch (NamingException ex) {
		assertTrue(ex.getMessage().contains("org.springframework.tests.sample.beans.DerivedTestBean"));
	}
}
 
Example #13
Source File: BeanFactoryTransactionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testGetsAreNotTransactionalWithProxyFactory3() {
	ITestBean testBean = (ITestBean) factory.getBean("proxyFactory3");
	assertTrue("testBean is a full proxy", testBean instanceof DerivedTestBean);
	assertTrue(testBean instanceof TransactionalProxy);
	InvocationCounterPointcut txnCounter = (InvocationCounterPointcut) factory.getBean("txnInvocationCounterPointcut");
	InvocationCounterInterceptor preCounter = (InvocationCounterInterceptor) factory.getBean("preInvocationCounterInterceptor");
	InvocationCounterInterceptor postCounter = (InvocationCounterInterceptor) factory.getBean("postInvocationCounterInterceptor");
	txnCounter.counter = 0;
	preCounter.counter = 0;
	postCounter.counter = 0;
	doTestGetsAreNotTransactional(testBean);
	// Can't assert it's equal to 4 as the pointcut may be optimized and only invoked once
	assertTrue(0 < txnCounter.counter && txnCounter.counter <= 4);
	assertEquals(4, preCounter.counter);
	assertEquals(4, postCounter.counter);
}
 
Example #14
Source File: RequestScopedProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
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);
	}
}
 
Example #15
Source File: WebApplicationContextScopeTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testRequestScope() {
	WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_REQUEST);
	MockHttpServletRequest request = new MockHttpServletRequest();
	ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);
	try {
		assertNull(request.getAttribute(NAME));
		DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
		assertSame(bean, request.getAttribute(NAME));
		assertSame(bean, ac.getBean(NAME));
		requestAttributes.requestCompleted();
		assertTrue(bean.wasDestroyed());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example #16
Source File: JndiObjectFactoryBeanTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testLookupWithProxyInterfaceAndExpectedTypeAndNoMatch() {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	TestBean tb = new TestBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
	jof.setJndiName("foo");
	jof.setExpectedType(DerivedTestBean.class);
	jof.setProxyInterface(ITestBean.class);
	try {
		jof.afterPropertiesSet();
		fail("Should have thrown NamingException");
	}
	catch (NamingException ex) {
		assertTrue(ex.getMessage().contains("org.springframework.tests.sample.beans.DerivedTestBean"));
	}
}
 
Example #17
Source File: RequestScopedProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
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);
	}
}
 
Example #18
Source File: SessionScopeTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void destructionAtSessionTermination() throws Exception {
	MockHttpSession session = new MockHttpSession();
	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setSession(session);
	ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);

	RequestContextHolder.setRequestAttributes(requestAttributes);
	String name = "sessionScopedDisposableObject";
	assertNull(session.getAttribute(name));
	DerivedTestBean bean = (DerivedTestBean) this.beanFactory.getBean(name);
	assertEquals(session.getAttribute(name), bean);
	assertSame(bean, this.beanFactory.getBean(name));

	requestAttributes.requestCompleted();
	session.invalidate();
	assertTrue(bean.wasDestroyed());
}
 
Example #19
Source File: DefaultSingletonBeanRegistryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testDisposableBean() {
	DefaultSingletonBeanRegistry beanRegistry = new DefaultSingletonBeanRegistry();

	DerivedTestBean tb = new DerivedTestBean();
	beanRegistry.registerSingleton("tb", tb);
	beanRegistry.registerDisposableBean("tb", tb);
	assertSame(tb, beanRegistry.getSingleton("tb"));

	assertSame(tb, beanRegistry.getSingleton("tb"));
	assertEquals(1, beanRegistry.getSingletonCount());
	String[] names = beanRegistry.getSingletonNames();
	assertEquals(1, names.length);
	assertEquals("tb", names[0]);
	assertFalse(tb.wasDestroyed());

	beanRegistry.destroySingletons();
	assertEquals(0, beanRegistry.getSingletonCount());
	assertEquals(0, beanRegistry.getSingletonNames().length);
	assertTrue(tb.wasDestroyed());
}
 
Example #20
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPrototypeWithArrayConversionForConstructor() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	List<String> list = new ManagedList<>();
	list.add("myName");
	list.add("myBeanName");
	RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bd.getConstructorArgumentValues().addGenericArgumentValue(list);
	lbf.registerBeanDefinition("test", bd);
	DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test");
	assertEquals("myName", tb.getName());
	assertEquals("myBeanName", tb.getBeanName());
	DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test");
	assertTrue(tb != tb2);
	assertEquals("myName", tb2.getName());
	assertEquals("myBeanName", tb2.getBeanName());
}
 
Example #21
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPrototypeWithArrayConversionForFactoryMethod() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	List<String> list = new ManagedList<>();
	list.add("myName");
	list.add("myBeanName");
	RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bd.setFactoryMethodName("create");
	bd.getConstructorArgumentValues().addGenericArgumentValue(list);
	lbf.registerBeanDefinition("test", bd);
	DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test");
	assertEquals("myName", tb.getName());
	assertEquals("myBeanName", tb.getBeanName());
	DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test");
	assertTrue(tb != tb2);
	assertEquals("myName", tb2.getName());
	assertEquals("myBeanName", tb2.getBeanName());
}
 
Example #22
Source File: ConcurrencyThrottleInterceptorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializable() throws Exception {
	DerivedTestBean tb = new DerivedTestBean();
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setInterfaces(new Class[] {ITestBean.class});
	ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
	proxyFactory.addAdvice(cti);
	proxyFactory.setTarget(tb);
	ITestBean proxy = (ITestBean) proxyFactory.getProxy();
	proxy.getAge();

	ITestBean serializedProxy = (ITestBean) SerializationTestUtils.serializeAndDeserialize(proxy);
	Advised advised = (Advised) serializedProxy;
	ConcurrencyThrottleInterceptor serializedCti =
			(ConcurrencyThrottleInterceptor) advised.getAdvisors()[0].getAdvice();
	assertEquals(cti.getConcurrencyLimit(), serializedCti.getConcurrencyLimit());
	serializedProxy.getAge();
}
 
Example #23
Source File: PortletApplicationContextScopeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequestScope() {
	WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_REQUEST);
	MockRenderRequest request = new MockRenderRequest();
	PortletRequestAttributes requestAttributes = new PortletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);
	try {
		assertNull(request.getAttribute(NAME));
		DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
		assertSame(bean, request.getAttribute(NAME));
		assertSame(bean, ac.getBean(NAME));
		requestAttributes.requestCompleted();
		assertTrue(bean.wasDestroyed());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example #24
Source File: PortletApplicationContextScopeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSessionScope() {
	WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_SESSION);
	MockRenderRequest request = new MockRenderRequest();
	PortletRequestAttributes requestAttributes = new PortletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);
	try {
		assertNull(request.getPortletSession().getAttribute(NAME));
		DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
		assertSame(bean, request.getPortletSession().getAttribute(NAME));
		assertSame(bean, ac.getBean(NAME));
		request.getPortletSession().invalidate();
		assertTrue(bean.wasDestroyed());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example #25
Source File: PortletApplicationContextScopeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testGlobalSessionScope() {
	WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_GLOBAL_SESSION);
	MockRenderRequest request = new MockRenderRequest();
	PortletRequestAttributes requestAttributes = new PortletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);
	try {
		assertNull(request.getPortletSession().getAttribute(NAME, PortletSession.APPLICATION_SCOPE));
		DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
		assertSame(bean, request.getPortletSession().getAttribute(NAME, PortletSession.APPLICATION_SCOPE));
		assertSame(bean, ac.getBean(NAME));
		request.getPortletSession().invalidate();
		assertTrue(bean.wasDestroyed());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example #26
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrototypeWithArrayConversionForFactoryMethod() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	List<String> list = new ManagedList<String>();
	list.add("myName");
	list.add("myBeanName");
	RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bd.setFactoryMethodName("create");
	bd.getConstructorArgumentValues().addGenericArgumentValue(list);
	lbf.registerBeanDefinition("test", bd);
	DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test");
	assertEquals("myName", tb.getName());
	assertEquals("myBeanName", tb.getBeanName());
	DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test");
	assertTrue(tb != tb2);
	assertEquals("myName", tb2.getName());
	assertEquals("myBeanName", tb2.getBeanName());
}
 
Example #27
Source File: JndiObjectFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testLookupWithProxyInterfaceAndExpectedTypeAndNoMatch() {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	TestBean tb = new TestBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
	jof.setJndiName("foo");
	jof.setExpectedType(DerivedTestBean.class);
	jof.setProxyInterface(ITestBean.class);
	try {
		jof.afterPropertiesSet();
		fail("Should have thrown NamingException");
	}
	catch (NamingException ex) {
		assertTrue(ex.getMessage().indexOf("org.springframework.tests.sample.beans.DerivedTestBean") != -1);
	}
}
 
Example #28
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrototypeWithArrayConversionForConstructor() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	List<String> list = new ManagedList<String>();
	list.add("myName");
	list.add("myBeanName");
	RootBeanDefinition bd = new RootBeanDefinition(DerivedTestBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bd.getConstructorArgumentValues().addGenericArgumentValue(list);
	lbf.registerBeanDefinition("test", bd);
	DerivedTestBean tb = (DerivedTestBean) lbf.getBean("test");
	assertEquals("myName", tb.getName());
	assertEquals("myBeanName", tb.getBeanName());
	DerivedTestBean tb2 = (DerivedTestBean) lbf.getBean("test");
	assertTrue(tb != tb2);
	assertEquals("myName", tb2.getName());
	assertEquals("myBeanName", tb2.getBeanName());
}
 
Example #29
Source File: WebApplicationContextScopeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequestScope() {
	WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_REQUEST);
	MockHttpServletRequest request = new MockHttpServletRequest();
	ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);
	try {
		assertNull(request.getAttribute(NAME));
		DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
		assertSame(bean, request.getAttribute(NAME));
		assertSame(bean, ac.getBean(NAME));
		requestAttributes.requestCompleted();
		assertTrue(bean.wasDestroyed());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example #30
Source File: WebApplicationContextScopeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testSessionScope() {
	WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_SESSION);
	MockHttpServletRequest request = new MockHttpServletRequest();
	ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);
	try {
		assertNull(request.getSession().getAttribute(NAME));
		DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
		assertSame(bean, request.getSession().getAttribute(NAME));
		assertSame(bean, ac.getBean(NAME));
		request.getSession().invalidate();
		assertTrue(bean.wasDestroyed());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}