Search in sources :

Example 11 with ServletConfig

use of javax.servlet.ServletConfig in project spring-framework by spring-projects.

the class ServletContextAwareProcessorTests method servletContextAwareWithServletContextAndServletConfig.

@Test
public void servletContextAwareWithServletContextAndServletConfig() {
    ServletContext servletContext = new MockServletContext();
    ServletConfig servletConfig = new MockServletConfig(servletContext);
    ServletContextAwareProcessor processor = new ServletContextAwareProcessor(servletContext, servletConfig);
    ServletContextAwareBean bean = new ServletContextAwareBean();
    assertNull(bean.getServletContext());
    processor.postProcessBeforeInitialization(bean, "testBean");
    assertNotNull("ServletContext should have been set", bean.getServletContext());
    assertEquals(servletContext, bean.getServletContext());
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 12 with ServletConfig

use of javax.servlet.ServletConfig in project spring-framework by spring-projects.

the class ServletContextAwareProcessorTests method servletConfigAwareWithNullServletContextAndNonNullServletConfig.

@Test
public void servletConfigAwareWithNullServletContextAndNonNullServletConfig() {
    ServletContext servletContext = new MockServletContext();
    ServletConfig servletConfig = new MockServletConfig(servletContext);
    ServletContextAwareProcessor processor = new ServletContextAwareProcessor(null, servletConfig);
    ServletConfigAwareBean bean = new ServletConfigAwareBean();
    assertNull(bean.getServletConfig());
    processor.postProcessBeforeInitialization(bean, "testBean");
    assertNotNull("ServletConfig should have been set", bean.getServletConfig());
    assertEquals(servletConfig, bean.getServletConfig());
}
Also used : ServletContextAwareProcessor(org.springframework.web.context.support.ServletContextAwareProcessor) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) ServletContext(javax.servlet.ServletContext) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 13 with ServletConfig

use of javax.servlet.ServletConfig in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method parameterDispatchingController.

@Test
public void parameterDispatchingController() throws Exception {
    final MockServletContext servletContext = new MockServletContext();
    final MockServletConfig servletConfig = new MockServletConfig(servletContext);
    WebApplicationContext webAppContext = initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            wac.setServletContext(servletContext);
            AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
            wac.getBeanFactory().registerResolvableDependency(ServletConfig.class, servletConfig);
        }
    }, MyParameterDispatchingController.class);
    MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    HttpSession session = request.getSession();
    getServlet().service(request, response);
    assertEquals("myView", response.getContentAsString());
    assertSame(servletContext, request.getAttribute("servletContext"));
    assertSame(servletConfig, request.getAttribute("servletConfig"));
    assertSame(session.getId(), request.getAttribute("sessionId"));
    assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
    assertSame(request.getLocale(), request.getAttribute("locale"));
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    response = new MockHttpServletResponse();
    session = request.getSession();
    getServlet().service(request, response);
    assertEquals("myView", response.getContentAsString());
    assertSame(servletContext, request.getAttribute("servletContext"));
    assertSame(servletConfig, request.getAttribute("servletConfig"));
    assertSame(session.getId(), request.getAttribute("sessionId"));
    assertSame(request.getRequestURI(), request.getAttribute("requestUri"));
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "other");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myOtherView", response.getContentAsString());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("view", "my");
    request.addParameter("lang", "de");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myLangView", response.getContentAsString());
    request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
    request.addParameter("surprise", "!");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("mySurpriseView", response.getContentAsString());
    MyParameterDispatchingController deserialized = (MyParameterDispatchingController) SerializationTestUtils.serializeAndDeserialize(webAppContext.getBean(MyParameterDispatchingController.class.getSimpleName()));
    assertNotNull(deserialized.request);
    assertNotNull(deserialized.session);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(org.springframework.mock.web.test.MockServletConfig) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 14 with ServletConfig

use of javax.servlet.ServletConfig in project swagger-core by swagger-api.

the class ApiListingResourceTest method shouldHandleErrorServletConfig_issue1691.

@Test
public void shouldHandleErrorServletConfig_issue1691() throws JsonProcessingException {
    ServletConfig sc = new ServletConfig() {

        @Override
        public String getServletName() {
            throw new RuntimeException("test_1691");
        }

        @Override
        public ServletContext getServletContext() {
            throw new RuntimeException("test_1691");
        }

        @Override
        public String getInitParameter(String name) {
            throw new RuntimeException("test_1691");
        }

        @Override
        public Enumeration getInitParameterNames() {
            throw new RuntimeException("test_1691");
        }
    };
    ApiListingResource a = new ApiListingResource();
    try {
        a.getListing(null, sc, null, null, "json");
    } catch (RuntimeException e) {
        // test will fail before, no need to mock Response
        if (e.getCause() instanceof ClassNotFoundException) {
            return;
        }
        throw e;
    }
}
Also used : ServletConfig(javax.servlet.ServletConfig) ApiListingResource(io.swagger.jaxrs.listing.ApiListingResource) Test(org.testng.annotations.Test)

Example 15 with ServletConfig

use of javax.servlet.ServletConfig in project guice by google.

the class ServletDefinitionTest method testServletInitAndConfig.

public final void testServletInitAndConfig() throws ServletException {
    Injector injector = createMock(Injector.class);
    Binding binding = createMock(Binding.class);
    expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
    expect(injector.getBinding(Key.get(HttpServlet.class))).andReturn(binding);
    final HttpServlet mockServlet = new HttpServlet() {
    };
    expect(injector.getInstance(Key.get(HttpServlet.class))).andReturn(mockServlet).anyTimes();
    replay(injector, binding);
    //some init params
    //noinspection SSBasedInspection
    final Map<String, String> initParams = new ImmutableMap.Builder<String, String>().put("ahsd", "asdas24dok").put("ahssd", "asdasd124ok").build();
    String pattern = "/*";
    final ServletDefinition servletDefinition = new ServletDefinition(Key.get(HttpServlet.class), UriPatternType.get(UriPatternType.SERVLET, pattern), initParams, null);
    ServletContext servletContext = createMock(ServletContext.class);
    final String contextName = "thing__!@@44__SRV" + getClass();
    expect(servletContext.getServletContextName()).andReturn(contextName);
    replay(servletContext);
    servletDefinition.init(servletContext, injector, Sets.<HttpServlet>newIdentityHashSet());
    assertNotNull(mockServlet.getServletContext());
    assertEquals(contextName, mockServlet.getServletContext().getServletContextName());
    assertEquals(Key.get(HttpServlet.class).toString(), mockServlet.getServletName());
    final ServletConfig servletConfig = mockServlet.getServletConfig();
    final Enumeration names = servletConfig.getInitParameterNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        assertTrue(initParams.containsKey(name));
        assertEquals(initParams.get(name), servletConfig.getInitParameter(name));
    }
    verify(injector, binding, servletContext);
}
Also used : Binding(com.google.inject.Binding) Enumeration(java.util.Enumeration) Injector(com.google.inject.Injector) HttpServlet(javax.servlet.http.HttpServlet) ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ServletConfig (javax.servlet.ServletConfig)69 ServletContext (javax.servlet.ServletContext)52 Enumeration (java.util.Enumeration)37 BeforeMethod (org.testng.annotations.BeforeMethod)21 ServletException (javax.servlet.ServletException)20 Test (org.junit.Test)15 IOException (java.io.IOException)12 BlockingIOCometSupport (org.atmosphere.container.BlockingIOCometSupport)9 MockServletConfig (org.springframework.mock.web.test.MockServletConfig)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 MockServletContext (org.springframework.mock.web.test.MockServletContext)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 AtmosphereFramework (org.atmosphere.runtime.AtmosphereFramework)6 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)6 HttpServlet (javax.servlet.http.HttpServlet)5 SimpleBroadcaster (org.atmosphere.util.SimpleBroadcaster)5 UnavailableException (javax.servlet.UnavailableException)4 PrintWriter (java.io.PrintWriter)3 AsynchronousProcessor (org.atmosphere.runtime.AsynchronousProcessor)3 AtmosphereRequest (org.atmosphere.runtime.AtmosphereRequest)3