@Test
 public void doesNotDefaultExceptionMappers() throws Exception {
   http.setRegisterDefaultExceptionMappers(false);
   assertThat(http.getRegisterDefaultExceptionMappers()).isFalse();
   Environment environment =
       new Environment(
           "test",
           Jackson.newObjectMapper(),
           Validation.buildDefaultValidatorFactory().getValidator(),
           new MetricRegistry(),
           ClassLoader.getSystemClassLoader());
   http.build(environment);
   for (Object singleton : environment.jersey().getResourceConfig().getSingletons()) {
     assertThat(singleton).isNotInstanceOf(ExceptionMapper.class);
   }
 }
 @Test
 public void loadsServerPushConfig() throws Exception {
   final ServerPushFilterFactory serverPush = http.getServerPush();
   assertThat(serverPush.isEnabled()).isTrue();
   assertThat(serverPush.getRefererHosts()).contains("dropwizard.io");
   assertThat(serverPush.getRefererPorts()).contains(8445);
 }
 @Test
 public void registersDefaultExceptionMappers() throws Exception {
   assertThat(http.getRegisterDefaultExceptionMappers()).isTrue();
   Environment environment =
       new Environment(
           "test",
           Jackson.newObjectMapper(),
           Validation.buildDefaultValidatorFactory().getValidator(),
           new MetricRegistry(),
           ClassLoader.getSystemClassLoader());
   http.build(environment);
   Set<Object> singletons = environment.jersey().getResourceConfig().getSingletons();
   assertThat(singletons).hasAtLeastOneElementOfType(LoggingExceptionMapper.class);
   assertThat(singletons).hasAtLeastOneElementOfType(ConstraintViolationExceptionMapper.class);
   assertThat(singletons).hasAtLeastOneElementOfType(JsonProcessingExceptionMapper.class);
   assertThat(singletons).hasAtLeastOneElementOfType(EarlyEofExceptionMapper.class);
 }
 @Test
 public void hasAdminContextPath() throws Exception {
   assertThat(http.getAdminContextPath()).isEqualTo("/admin");
 }
 @Test
 public void hasApplicationContextPath() throws Exception {
   assertThat(http.getApplicationContextPath()).isEqualTo("/app");
 }
 @Test
 public void hasAMinimumNumberOfThreads() throws Exception {
   assertThat(http.getMinThreads()).isEqualTo(89);
 }
 @Test
 public void hasAMaximumNumberOfThreads() throws Exception {
   assertThat(http.getMaxThreads()).isEqualTo(101);
 }
 @Test
 public void loadsGzipConfig() throws Exception {
   assertThat(http.getGzipFilterFactory().isEnabled()).isFalse();
 }
  @Test
  public void testGracefulShutdown() throws Exception {
    ObjectMapper objectMapper = Jackson.newObjectMapper();
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    MetricRegistry metricRegistry = new MetricRegistry();
    Environment environment =
        new Environment(
            "test", objectMapper, validator, metricRegistry, ClassLoader.getSystemClassLoader());

    CountDownLatch requestReceived = new CountDownLatch(1);
    CountDownLatch shutdownInvoked = new CountDownLatch(1);

    environment.jersey().register(new TestResource(requestReceived, shutdownInvoked));

    final ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
    final Server server = http.build(environment);

    ((AbstractNetworkConnector) server.getConnectors()[0]).setPort(0);

    ScheduledFuture<Void> cleanup =
        executor.schedule(
            new Callable<Void>() {
              @Override
              public Void call() throws Exception {
                if (!server.isStopped()) {
                  server.stop();
                }
                executor.shutdownNow();
                return null;
              }
            },
            5,
            TimeUnit.SECONDS);

    server.start();

    final int port = ((AbstractNetworkConnector) server.getConnectors()[0]).getLocalPort();

    Future<String> futureResult =
        executor.submit(
            new Callable<String>() {
              @Override
              public String call() throws Exception {
                URL url = new URL("http://localhost:" + port + "/app/test");
                URLConnection connection = url.openConnection();
                connection.connect();
                return CharStreams.toString(new InputStreamReader(connection.getInputStream()));
              }
            });

    requestReceived.await();

    Future<Void> serverStopped =
        executor.submit(
            new Callable<Void>() {
              @Override
              public Void call() throws Exception {
                server.stop();
                return null;
              }
            });

    Connector[] connectors = server.getConnectors();
    assertThat(connectors).isNotEmpty();
    assertThat(connectors[0]).isInstanceOf(NetworkConnector.class);
    NetworkConnector connector = (NetworkConnector) connectors[0];

    // wait for server to close the connectors
    while (true) {
      if (!connector.isOpen()) {
        shutdownInvoked.countDown();
        break;
      }
      Thread.sleep(5);
    }

    String result = futureResult.get();
    assertThat(result).isEqualTo("test");

    serverStopped.get();

    // cancel the cleanup future since everything succeeded
    cleanup.cancel(false);
    executor.shutdownNow();
  }