Search in sources :

Example 16 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class WebApplicationContextScopeTests method testSessionScope.

@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);
    }
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 17 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class WebApplicationContextScopeTests method testApplicationScope.

@Test
public void testApplicationScope() {
    WebApplicationContext ac = initApplicationContext(WebApplicationContext.SCOPE_APPLICATION);
    assertNull(ac.getServletContext().getAttribute(NAME));
    DerivedTestBean bean = ac.getBean(NAME, DerivedTestBean.class);
    assertSame(bean, ac.getServletContext().getAttribute(NAME));
    assertSame(bean, ac.getBean(NAME));
    new ContextCleanupListener().contextDestroyed(new ServletContextEvent(ac.getServletContext()));
    assertTrue(bean.wasDestroyed());
}
Also used : DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) ContextCleanupListener(org.springframework.web.context.ContextCleanupListener) ServletContextEvent(javax.servlet.ServletContextEvent) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 18 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class RequestScopedProxyTests method testDestructionAtRequestCompletion.

@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);
    }
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Example 19 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class ConcurrencyThrottleInterceptorTests method testSerializable.

@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();
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Advised(org.springframework.aop.framework.Advised) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Example 20 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class BeanUtilsTests method testCopyPropertiesWithDifferentTypes1.

@Test
public void testCopyPropertiesWithDifferentTypes1() throws Exception {
    DerivedTestBean tb = new DerivedTestBean();
    tb.setName("rod");
    tb.setAge(32);
    tb.setTouchy("touchy");
    TestBean tb2 = new TestBean();
    assertTrue("Name empty", tb2.getName() == null);
    assertTrue("Age empty", tb2.getAge() == 0);
    assertTrue("Touchy empty", tb2.getTouchy() == null);
    BeanUtils.copyProperties(tb, tb2);
    assertTrue("Name copied", tb2.getName().equals(tb.getName()));
    assertTrue("Age copied", tb2.getAge() == tb.getAge());
    assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy()));
}
Also used : DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Aggregations

DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)27 Test (org.junit.Test)26 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)9 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)6 ITestBean (org.springframework.tests.sample.beans.ITestBean)6 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)6 TestBean (org.springframework.tests.sample.beans.TestBean)6 PropertyEditorSupport (java.beans.PropertyEditorSupport)4 WebApplicationContext (org.springframework.web.context.WebApplicationContext)3 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)3 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)2 ManagedList (org.springframework.beans.factory.support.ManagedList)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 MockHttpSession (org.springframework.mock.web.test.MockHttpSession)2 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)2 InputStream (java.io.InputStream)1 Serializable (java.io.Serializable)1 HashSet (java.util.HashSet)1