use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class WebApplicationContextScopeTests method initApplicationContext.
private WebApplicationContext initApplicationContext(String scope) {
MockServletContext sc = new MockServletContext();
GenericWebApplicationContext ac = new GenericWebApplicationContext(sc);
GenericBeanDefinition bd = new GenericBeanDefinition();
bd.setBeanClass(DerivedTestBean.class);
bd.setScope(scope);
ac.registerBeanDefinition(NAME, bd);
ac.refresh();
return ac;
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class RequestContextListenerTests method requestContextListenerWithDifferentThread.
@Test
public void requestContextListenerWithDifferentThread() {
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() {
@Override
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());
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class RequestContextListenerTests method requestContextListenerWithSameThread.
@Test
public void requestContextListenerWithSameThread() {
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());
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class RequestContextFilterTests method testFilterInvocation.
private void testFilterInvocation(final ServletException sex) throws Exception {
final MockHttpServletRequest req = new MockHttpServletRequest();
req.setAttribute("myAttr", "myValue");
final MockHttpServletResponse resp = new MockHttpServletResponse();
// Expect one invocation by the filter being tested
class DummyFilterChain implements FilterChain {
public int invocations = 0;
@Override
public void doFilter(ServletRequest req, ServletResponse resp) throws IOException, ServletException {
++invocations;
if (invocations == 1) {
assertSame("myValue", RequestContextHolder.currentRequestAttributes().getAttribute("myAttr", RequestAttributes.SCOPE_REQUEST));
if (sex != null) {
throw sex;
}
} else {
throw new IllegalStateException("Too many invocations");
}
}
}
;
DummyFilterChain fc = new DummyFilterChain();
MockFilterConfig mfc = new MockFilterConfig(new MockServletContext(), "foo");
RequestContextFilter rbf = new RequestContextFilter();
rbf.init(mfc);
try {
rbf.doFilter(req, resp, fc);
if (sex != null) {
fail();
}
} catch (ServletException ex) {
assertNotNull(sex);
}
try {
RequestContextHolder.currentRequestAttributes();
fail();
} catch (IllegalStateException ex) {
// Ok
}
assertEquals(1, fc.invocations);
}
use of org.springframework.mock.web.test.MockServletContext in project spring-framework by spring-projects.
the class DelegatingFilterProxyTests method testDelegatingFilterProxyWithTargetBeanNameAndNoApplicationContext.
@Test(expected = IllegalStateException.class)
public void testDelegatingFilterProxyWithTargetBeanNameAndNoApplicationContext() throws ServletException, IOException {
MockServletContext sc = new MockServletContext();
DelegatingFilterProxy filterProxy = new DelegatingFilterProxy("targetFilter", null);
filterProxy.init(new MockFilterConfig(sc));
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
// throws
filterProxy.doFilter(request, response, null);
}
Aggregations