use of javax.servlet.ServletContext in project sling by apache.
the class ExternalServletContextWrapperTest method testGetRequestDispatcher.
/**
* Tests that the RequestDispatcher is wrapped.
*/
@Test
public void testGetRequestDispatcher() {
final RequestDispatcher rd = context.mock(RequestDispatcher.class);
final ServletContext ctx = context.mock(ServletContext.class);
context.checking(new Expectations() {
{
oneOf(ctx).getRequestDispatcher("foo.jsp");
will(returnValue(rd));
}
});
ExternalServletContextWrapper wrapper = new ExternalServletContextWrapper(ctx);
RequestDispatcher dispatcher = wrapper.getRequestDispatcher("foo.jsp");
assertTrue(dispatcher instanceof RequestDispatcherWrapper);
assertEquals(rd, ((RequestDispatcherWrapper) dispatcher).getDelegate());
}
use of javax.servlet.ServletContext in project cloudstack by apache.
the class StaticResourceServletTest method testNoSuchFile.
// negative tests
@Test
public void testNoSuchFile() throws ServletException, IOException {
final StaticResourceServlet servlet = Mockito.mock(StaticResourceServlet.class);
Mockito.doCallRealMethod().when(servlet).doGet(Matchers.any(HttpServletRequest.class), Matchers.any(HttpServletResponse.class));
final ServletContext servletContext = Mockito.mock(ServletContext.class);
Mockito.when(servletContext.getRealPath("notexisting.css")).thenReturn(new File(rootDirectory, "notexisting.css").getAbsolutePath());
Mockito.when(servlet.getServletContext()).thenReturn(servletContext);
final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getServletPath()).thenReturn("notexisting.css");
final HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
servlet.doGet(request, response);
Mockito.verify(response).setStatus(HttpServletResponse.SC_NOT_FOUND);
}
use of javax.servlet.ServletContext in project geode by apache.
the class BasicServlet method init.
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
ServletContext context = config.getServletContext();
String cbInitParam = config.getInitParameter("test.callback");
callback = (Callback) context.getAttribute(cbInitParam);
}
use of javax.servlet.ServletContext in project spring-framework by spring-projects.
the class ServletContextResourcePatternResolver method doFindPathMatchingFileResources.
/**
* Overridden version which checks for ServletContextResource
* and uses {@code ServletContext.getResourcePaths} to find
* matching resources below the web application root directory.
* In case of other resources, delegates to the superclass version.
* @see #doRetrieveMatchingServletContextResources
* @see ServletContextResource
* @see javax.servlet.ServletContext#getResourcePaths
*/
@Override
protected Set<Resource> doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException {
if (rootDirResource instanceof ServletContextResource) {
ServletContextResource scResource = (ServletContextResource) rootDirResource;
ServletContext sc = scResource.getServletContext();
String fullPattern = scResource.getPath() + subPattern;
Set<Resource> result = new LinkedHashSet<>(8);
doRetrieveMatchingServletContextResources(sc, fullPattern, scResource.getPath(), result);
return result;
} else {
return super.doFindPathMatchingFileResources(rootDirResource, subPattern);
}
}
use of javax.servlet.ServletContext in project spring-framework by spring-projects.
the class MockMvcRequestBuilders method asyncDispatch.
/**
* Create a {@link RequestBuilder} for an async dispatch from the
* {@link MvcResult} of the request that started async processing.
* <p>Usage involves performing a request that starts async processing first:
* <pre class="code">
* MvcResult mvcResult = this.mockMvc.perform(get("/1"))
* .andExpect(request().asyncStarted())
* .andReturn();
* </pre>
* <p>And then performing the async dispatch re-using the {@code MvcResult}:
* <pre class="code">
* this.mockMvc.perform(asyncDispatch(mvcResult))
* .andExpect(status().isOk())
* .andExpect(content().contentType(MediaType.APPLICATION_JSON))
* .andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
* </pre>
* @param mvcResult the result from the request that started async processing
*/
public static RequestBuilder asyncDispatch(final MvcResult mvcResult) {
// There must be an async result before dispatching
mvcResult.getAsyncResult();
return new RequestBuilder() {
@Override
public MockHttpServletRequest buildRequest(ServletContext servletContext) {
MockHttpServletRequest request = mvcResult.getRequest();
request.setDispatcherType(DispatcherType.ASYNC);
request.setAsyncStarted(false);
return request;
}
};
}
Aggregations