All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dropwizard.testing.ConfigOverrideRandomPorts Maven / Gradle / Ivy

There is a newer version: 5.0.0-alpha.4
Show newest version
package io.dropwizard.testing;

import io.dropwizard.core.Configuration;
import io.dropwizard.core.server.DefaultServerFactory;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * Override port configuration setting for application and admin connectors and choose a random port.
 *
 * @see io.dropwizard.jetty.HttpConnectorFactory#getPort()
 * @see Configuration#getServerFactory()
 * @see DefaultServerFactory#getApplicationConnectors()
 * @see DefaultServerFactory#getAdminConnectors()
 */
public class ConfigOverrideRandomPorts extends ConfigOverride {

    private static final String SERVER_APPLICATION_CONNECTORS_PORT = "server.applicationConnectors[0].port";
    private static final String SERVER_ADMIN_CONNECTORS_PORT = "server.adminConnectors[0].port";

    private final String propertyPrefix;

    @Nullable
    private String applicationConnectorsPort = null;
    @Nullable
    private String adminConnectorsPort = null;

    ConfigOverrideRandomPorts(String propertyPrefix) {
        this.propertyPrefix = propertyPrefix.endsWith(".") ? propertyPrefix : propertyPrefix + ".";
    }

    @Override
    public void addToSystemProperties() {
        applicationConnectorsPort = System.setProperty(propertyPrefix + SERVER_APPLICATION_CONNECTORS_PORT, "0");
        adminConnectorsPort = System.setProperty(propertyPrefix + SERVER_ADMIN_CONNECTORS_PORT, "0");
    }

    @Override
    public void removeFromSystemProperties() {
        if (applicationConnectorsPort != null) {
            System.setProperty(propertyPrefix + SERVER_APPLICATION_CONNECTORS_PORT, applicationConnectorsPort);
        } else {
            System.clearProperty(propertyPrefix + SERVER_APPLICATION_CONNECTORS_PORT);
        }

        if (adminConnectorsPort != null) {
            System.setProperty(propertyPrefix + SERVER_ADMIN_CONNECTORS_PORT, adminConnectorsPort);
        } else {
            System.clearProperty(propertyPrefix + SERVER_ADMIN_CONNECTORS_PORT);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy