Search in sources :

Example 11 with DerivedTestBean

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

the class BeanFactoryTransactionTests method testGetsAreNotTransactionalWithProxyFactory3.

@Test
public void testGetsAreNotTransactionalWithProxyFactory3() throws NoSuchMethodException {
    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);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Example 12 with DerivedTestBean

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

the class WebApplicationContextScopeTests method testRequestScope.

@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);
    }
}
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 13 with DerivedTestBean

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

the class RequestScopeTests method destructionAtRequestCompletion.

@Test
public void destructionAtRequestCompletion() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    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());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Example 14 with DerivedTestBean

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

the class SessionScopeTests method destructionAtSessionTermination.

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

Example 15 with DerivedTestBean

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

the class SessionScopeTests method doTestDestructionWithSessionSerialization.

private void doTestDestructionWithSessionSerialization(boolean beanNameReset) throws Exception {
    Serializable serializedState = null;
    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();
    serializedState = session.serializeState();
    assertFalse(bean.wasDestroyed());
    serializedState = (Serializable) SerializationTestUtils.serializeAndDeserialize(serializedState);
    session = new MockHttpSession();
    session.deserializeState(serializedState);
    request = new MockHttpServletRequest();
    request.setSession(session);
    requestAttributes = new ServletRequestAttributes(request);
    RequestContextHolder.setRequestAttributes(requestAttributes);
    name = "sessionScopedDisposableObject";
    assertNotNull(session.getAttribute(name));
    bean = (DerivedTestBean) this.beanFactory.getBean(name);
    assertEquals(session.getAttribute(name), bean);
    assertSame(bean, this.beanFactory.getBean(name));
    requestAttributes.requestCompleted();
    session.invalidate();
    assertTrue(bean.wasDestroyed());
    if (beanNameReset) {
        assertNull(bean.getBeanName());
    } else {
        assertNotNull(bean.getBeanName());
    }
}
Also used : Serializable(java.io.Serializable) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.test.MockHttpSession) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean)

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