Skip to content

Commit

Permalink
Merge pull request dropwizard#544 from blacklocus/master
Browse files Browse the repository at this point in the history
DropwizardAppRule should not require a configPath to operate.
  • Loading branch information
mveitas committed Apr 24, 2014
2 parents 69b52b6 + 2a8736a commit a13cca8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.dropwizard.testing.junit;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import io.dropwizard.Application;
import io.dropwizard.Configuration;
Expand All @@ -14,6 +15,7 @@
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import javax.annotation.Nullable;
import java.util.Enumeration;

/**
Expand All @@ -36,7 +38,7 @@ public class DropwizardAppRule<C extends Configuration> implements TestRule {
private Server jettyServer;

public DropwizardAppRule(Class<? extends Application<C>> applicationClass,
String configPath,
@Nullable String configPath,
ConfigOverride... configOverrides) {
this.applicationClass = applicationClass;
this.configPath = configPath;
Expand Down Expand Up @@ -95,7 +97,13 @@ public void serverStarted(Server server) {

application.initialize(bootstrap);
final ServerCommand<C> command = new ServerCommand<>(application);
final Namespace namespace = new Namespace(ImmutableMap.<String, Object>of("file", configPath));

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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.dropwizard.testing.junit;

import com.google.common.collect.ImmutableMap;
import com.sun.jersey.api.client.Client;
import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Map;

public class DropwizardAppRuleWithoutConfigTest {

@ClassRule
public static final DropwizardAppRule<Configuration> RULE = new DropwizardAppRule<>(TestApplication.class, null);

Client client = new Client();

@Test
public void runWithoutConfigFile() {
Map response = client.resource("http://localhost:" + RULE.getLocalPort() + "/test").get(Map.class);
Assert.assertEquals(ImmutableMap.of("color", "orange"), response);
}

static class TestApplication extends Application<Configuration> {
@Override
public void initialize(Bootstrap<Configuration> bootstrap) {
}

@Override
public void run(Configuration configuration, Environment environment) throws Exception {
environment.jersey().register(new TestResource());
}
}

@Path("test")
@Produces(MediaType.APPLICATION_JSON)
public static class TestResource {
@GET
public Response get() {
return Response.ok(ImmutableMap.of("color", "orange")).build();
}
}
}

0 comments on commit a13cca8

Please sign in to comment.