package io.dropwizard.testing.junit;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMap;
import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.cli.ServerCommand;
import io.dropwizard.lifecycle.ServerLifecycleListener;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import net.sourceforge.argparse4j.inf.Namespace;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.junit.rules.ExternalResource;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import javax.annotation.Nullable;
import java.util.Enumeration;
import static com.google.common.base.Throwables.propagate;
/**
* A JUnit rule for starting and stopping your application at the start and end of a test class.
* <p/>
* By default, the {@link Application} will be constructed using reflection to invoke the nullary
* constructor. If your application does not provide a public nullary constructor, you will need to
* override the {@link #newApplication()} method to provide your application instance(s).
*
* @param <C> the configuration type
*/
public class DropwizardAppRule<C extends Configuration> extends ExternalResource {
private final Class<? extends Application<C>> applicationClass;
private final String configPath;
private C configuration;
private Application<C> application;
private Environment environment;
private Server jettyServer;
public DropwizardAppRule(Class<? extends Application<C>> applicationClass,
@Nullable String configPath,
ConfigOverride... configOverrides) {
this.applicationClass = applicationClass;
this.configPath = configPath;
for (ConfigOverride configOverride: configOverrides) {
configOverride.addToSystemProperties();
}
}
@Override
protected void before() {
startIfRequired();
}
@Override
protected void after() {
resetConfigOverrides();
try {
jettyServer.stop();
} catch (Exception e) {
propagate(e);
}
jettyServer = null;
}
private void resetConfigOverrides() {
for (Enumeration<?> props = System.getProperties().propertyNames(); props.hasMoreElements();) {
String keyString = (String) props.nextElement();
if (keyString.startsWith("dw.")) {
System.clearProperty(keyString);
}
}
}
private void startIfRequired() {
if (jettyServer != null) {
return;
}
try {
application = newApplication();
final Bootstrap<C> bootstrap = new Bootstrap<C>(application) {
@Override
public void run(C configuration, Environment environment) throws Exception {
environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {
@Override
public void serverStarted(Server server) {
jettyServer = server;
}
});
DropwizardAppRule.this.configuration = configuration;
DropwizardAppRule.this.environment = environment;
super.run(configuration, environment);
}
};
application.initialize(bootstrap);
final ServerCommand<C> command = new ServerCommand<>(application);
ImmutableMap.Builder<String, Object> file = ImmutableMap.builder();
if (!Strings.isNullOrEmpty(configPath)) {
file.put("file", configPath);
}
final Namespace namespace = new Namespace(file.build());
command.run(bootstrap, namespace);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public C getConfiguration() {
return configuration;
}
public int getLocalPort() {
return ((ServerConnector) jettyServer.getConnectors()[0]).getLocalPort();
}
public int getAdminPort() {
return ((ServerConnector) jettyServer.getConnectors()[1]).getLocalPort();
}
public Application<C> newApplication() {
try {
return applicationClass.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public <A extends Application<C>> A getApplication() {
return (A) application;
}
public Environment getEnvironment() {
return environment;
}
}