Search in sources :

Example 61 with ServletContext

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());
}
Also used : Expectations(org.jmock.Expectations) RequestDispatcherWrapper(org.apache.sling.engine.impl.helper.ExternalServletContextWrapper.RequestDispatcherWrapper) ServletContext(javax.servlet.ServletContext) RequestDispatcher(javax.servlet.RequestDispatcher) Test(org.junit.Test)

Example 62 with ServletContext

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) File(java.io.File) Test(org.junit.Test)

Example 63 with ServletContext

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);
}
Also used : ServletContext(javax.servlet.ServletContext)

Example 64 with ServletContext

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);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) UrlResource(org.springframework.core.io.UrlResource) Resource(org.springframework.core.io.Resource) ServletContext(javax.servlet.ServletContext)

Example 65 with ServletContext

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;
        }
    };
}
Also used : RequestBuilder(org.springframework.test.web.servlet.RequestBuilder) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletContext(javax.servlet.ServletContext)

Aggregations

ServletContext (javax.servlet.ServletContext)1059 Test (org.junit.Test)231 HttpServletRequest (javax.servlet.http.HttpServletRequest)171 IOException (java.io.IOException)138 HttpServletResponse (javax.servlet.http.HttpServletResponse)127 ServletException (javax.servlet.ServletException)95 File (java.io.File)75 ServletConfig (javax.servlet.ServletConfig)68 FilterConfig (javax.servlet.FilterConfig)65 HashMap (java.util.HashMap)63 Enumeration (java.util.Enumeration)52 InputStream (java.io.InputStream)51 ArrayList (java.util.ArrayList)49 URL (java.net.URL)47 HttpSession (javax.servlet.http.HttpSession)43 Map (java.util.Map)38 PrintWriter (java.io.PrintWriter)32 List (java.util.List)32 RequestDispatcher (javax.servlet.RequestDispatcher)30 WebApplicationContext (org.springframework.web.context.WebApplicationContext)28