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);
}
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);
}
}
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());
}
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());
}
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());
}
}
Aggregations