Search in sources :

Example 1 with ServletConfig

use of javax.servlet.ServletConfig in project hadoop by apache.

the class TestConfServlet method verifyGetProperty.

private void verifyGetProperty(Configuration conf, String format, String propertyName) throws Exception {
    StringWriter sw = null;
    PrintWriter pw = null;
    ConfServlet service = null;
    try {
        service = new ConfServlet();
        ServletConfig servletConf = mock(ServletConfig.class);
        ServletContext context = mock(ServletContext.class);
        service.init(servletConf);
        when(context.getAttribute(HttpServer2.CONF_CONTEXT_ATTRIBUTE)).thenReturn(conf);
        when(service.getServletContext()).thenReturn(context);
        HttpServletRequest request = mock(HttpServletRequest.class);
        when(request.getHeader(HttpHeaders.ACCEPT)).thenReturn(TEST_FORMATS.get(format));
        when(request.getParameter("name")).thenReturn(propertyName);
        HttpServletResponse response = mock(HttpServletResponse.class);
        sw = new StringWriter();
        pw = new PrintWriter(sw);
        when(response.getWriter()).thenReturn(pw);
        // response request
        service.doGet(request, response);
        String result = sw.toString().trim();
        // in the response
        if (Strings.isNullOrEmpty(propertyName)) {
            for (String key : TEST_PROPERTIES.keySet()) {
                assertTrue(result.contains(key) && result.contains(TEST_PROPERTIES.get(key)));
            }
        } else {
            if (conf.get(propertyName) != null) {
                // if property name is not empty and property is found
                assertTrue(result.contains(propertyName));
                for (String key : TEST_PROPERTIES.keySet()) {
                    if (!key.equals(propertyName)) {
                        assertFalse(result.contains(key));
                    }
                }
            } else {
                // if property name is not empty, and it's not in configuration
                // expect proper error code and error message is set to the response
                Mockito.verify(response).sendError(Mockito.eq(HttpServletResponse.SC_NOT_FOUND), Mockito.eq("Property " + propertyName + " not found"));
            }
        }
    } finally {
        if (sw != null) {
            sw.close();
        }
        if (pw != null) {
            pw.close();
        }
        if (service != null) {
            service.destroy();
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter)

Example 2 with ServletConfig

use of javax.servlet.ServletConfig in project metrics by dropwizard.

the class HealthCheckServletTest method constructorWithRegistryAsArgumentIsUsedInPreferenceOverServletConfig.

@Test
public void constructorWithRegistryAsArgumentIsUsedInPreferenceOverServletConfig() throws Exception {
    final HealthCheckRegistry healthCheckRegistry = mock(HealthCheckRegistry.class);
    final ServletContext servletContext = mock(ServletContext.class);
    final ServletConfig servletConfig = mock(ServletConfig.class);
    when(servletConfig.getServletContext()).thenReturn(servletContext);
    final HealthCheckServlet healthCheckServlet = new HealthCheckServlet(healthCheckRegistry);
    healthCheckServlet.init(servletConfig);
    verify(servletConfig, times(1)).getServletContext();
    verify(servletContext, never()).getAttribute(eq(HealthCheckServlet.HEALTH_CHECK_REGISTRY));
}
Also used : HealthCheckRegistry(com.codahale.metrics.health.HealthCheckRegistry) ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 3 with ServletConfig

use of javax.servlet.ServletConfig in project metrics by dropwizard.

the class HealthCheckServletTest method constructorWithRegistryAsArgumentUsesServletConfigWhenNullButWrongTypeInContext.

@Test(expected = ServletException.class)
public void constructorWithRegistryAsArgumentUsesServletConfigWhenNullButWrongTypeInContext() throws Exception {
    final ServletContext servletContext = mock(ServletContext.class);
    final ServletConfig servletConfig = mock(ServletConfig.class);
    when(servletConfig.getServletContext()).thenReturn(servletContext);
    when(servletContext.getAttribute(eq(HealthCheckServlet.HEALTH_CHECK_REGISTRY))).thenReturn("IRELLEVANT_STRING");
    final HealthCheckServlet healthCheckServlet = new HealthCheckServlet(null);
    healthCheckServlet.init(servletConfig);
}
Also used : ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 4 with ServletConfig

use of javax.servlet.ServletConfig in project metrics by dropwizard.

the class MetricsServletTest method constructorWithRegistryAsArgumentIsUsedInPreferenceOverServletConfig.

@Test
public void constructorWithRegistryAsArgumentIsUsedInPreferenceOverServletConfig() throws Exception {
    final MetricRegistry metricRegistry = mock(MetricRegistry.class);
    final ServletContext servletContext = mock(ServletContext.class);
    final ServletConfig servletConfig = mock(ServletConfig.class);
    when(servletConfig.getServletContext()).thenReturn(servletContext);
    final MetricsServlet metricsServlet = new MetricsServlet(metricRegistry);
    metricsServlet.init(servletConfig);
    verify(servletConfig, times(1)).getServletContext();
    verify(servletContext, never()).getAttribute(eq(MetricsServlet.METRICS_REGISTRY));
}
Also used : ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 5 with ServletConfig

use of javax.servlet.ServletConfig in project metrics by dropwizard.

the class MetricsServletTest method constructorWithRegistryAsArgumentUsesServletConfigWhenNullButWrongTypeInContext.

@Test(expected = ServletException.class)
public void constructorWithRegistryAsArgumentUsesServletConfigWhenNullButWrongTypeInContext() throws Exception {
    final ServletContext servletContext = mock(ServletContext.class);
    final ServletConfig servletConfig = mock(ServletConfig.class);
    when(servletConfig.getServletContext()).thenReturn(servletContext);
    when(servletContext.getAttribute(eq(MetricsServlet.METRICS_REGISTRY))).thenReturn("IRELLEVANT_STRING");
    final MetricsServlet metricsServlet = new MetricsServlet(null);
    metricsServlet.init(servletConfig);
}
Also used : ServletConfig(javax.servlet.ServletConfig) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

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