Skip to content

Commit

Permalink
-c argument can support multi files
Browse files Browse the repository at this point in the history
  • Loading branch information
macdao committed Apr 23, 2017
1 parent e7b02a1 commit 95bdc50
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

public final class HttpArgs extends StartArgs {
private HttpArgs(final Integer port, final Integer shutdownPort,
final String configurationFile, final String globalSettings,
final String env) {
super(ServerType.HTTP, port, shutdownPort, configurationFile, globalSettings, env, null);
final String[] configurationFiles, final String globalSettings,
final String env) {
super(ServerType.HTTP, port, shutdownPort, configurationFiles, globalSettings, env, null);
}

public static Builder httpArgs() {
Expand All @@ -16,7 +16,7 @@ public static Builder httpArgs() {
public static class Builder {
private Integer port;
private Integer shutdownPort;
private String configurationFile;
private String[] configurationFiles;
private String settings;
private String env;

Expand All @@ -30,8 +30,8 @@ public Builder withShutdownPort(final Integer shutdownPort) {
return this;
}

public Builder withConfigurationFile(final String configurationFile) {
this.configurationFile = configurationFile;
public Builder withConfigurationFile(final String... configurationFiles) {
this.configurationFiles = configurationFiles;
return this;
}

Expand All @@ -46,7 +46,7 @@ public Builder withEnv(final String env) {
}

public HttpArgs build() {
return new HttpArgs(port, shutdownPort, configurationFile, settings, env);
return new HttpArgs(port, shutdownPort, configurationFiles, settings, env);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import com.github.dreamhead.moco.bootstrap.ServerType;

public final class HttpsArgs extends StartArgs {
private HttpsArgs(final Integer port, final Integer shutdownPort, final String configurationFile,
private HttpsArgs(final Integer port, final Integer shutdownPort, final String[] configurationFiles,
final String globalSettings, final String env, final HttpsArg httpsArg) {
super(ServerType.HTTPS, port, shutdownPort, configurationFile, globalSettings, env, httpsArg);
super(ServerType.HTTPS, port, shutdownPort, configurationFiles, globalSettings, env, httpsArg);
}

public static Builder httpsArgs() {
Expand All @@ -16,7 +16,7 @@ public static Builder httpsArgs() {
public static class Builder {
private Integer port;
private Integer shutdownPort;
private String configurationFile;
private String[] configurationFiles;
private String settings;
private String env;
private HttpsArg httpsArg;
Expand All @@ -31,8 +31,8 @@ public Builder withShutdownPort(final Integer shutdownPort) {
return this;
}

public Builder withConfigurationFile(final String configurationFile) {
this.configurationFile = configurationFile;
public Builder withConfigurationFile(final String... configurationFiles) {
this.configurationFiles = configurationFiles;
return this;
}

Expand All @@ -52,7 +52,7 @@ public Builder withHttpsArg(final HttpsArg httpsArg) {
}

public HttpsArgs build() {
return new HttpsArgs(port, shutdownPort, configurationFile, settings, env, httpsArg);
return new HttpsArgs(port, shutdownPort, configurationFiles, settings, env, httpsArg);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import static com.github.dreamhead.moco.bootstrap.ServerType.SOCKET;

public final class SocketArgs extends StartArgs {
private SocketArgs(final Integer port, final Integer shutdownPort, final String configurationFile) {
super(SOCKET, port, shutdownPort, configurationFile, null, null, null);
private SocketArgs(final Integer port, final Integer shutdownPort, final String[] configurationFiles) {
super(SOCKET, port, shutdownPort, configurationFiles, null, null, null);
}

public static Builder socketArgs() {
Expand All @@ -14,7 +14,7 @@ public static Builder socketArgs() {
public static class Builder {
private Integer port;
private Integer shutdownPort;
private String configurationFile;
private String[] configurationFiles;

public Builder withPort(final Integer port) {
this.port = port;
Expand All @@ -26,13 +26,13 @@ public Builder withShutdownPort(final Integer shutdownPort) {
return this;
}

public Builder withConfigurationFile(final String configurationFile) {
this.configurationFile = configurationFile;
public Builder withConfigurationFile(final String... configurationFiles) {
this.configurationFiles = configurationFiles;
return this;
}

public SocketArgs build() {
return new SocketArgs(port, shutdownPort, configurationFile);
return new SocketArgs(port, shutdownPort, configurationFiles);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
public abstract class StartArgs extends ShutdownPortOption {
private final ServerType type;
private final Optional<Integer> port;
private final Optional<String> configurationFile;
private final Optional<String[]> configurationFiles;
private final Optional<String> settings;
private final Optional<String> env;
private final Optional<HttpsArg> httpsArg;

protected StartArgs(final ServerType type, final Integer port, final Integer shutdownPort,
final String configurationFile, final String globalSettings,
final String[] configurationFiles, final String globalSettings,
final String env, final HttpsArg httpsArg) {
super(shutdownPort);
this.type = type;
this.port = fromNullable(port);
this.configurationFile = fromNullable(configurationFile);
this.configurationFiles = fromNullable(configurationFiles);
this.settings = fromNullable(globalSettings);
this.env = fromNullable(env);
this.httpsArg = fromNullable(httpsArg);
Expand All @@ -33,12 +33,12 @@ public Optional<Integer> getPort() {
return port;
}

public Optional<String> getConfigurationFile() {
return configurationFile;
public Optional<String[]> getConfigurationFiles() {
return configurationFiles;
}

public boolean hasConfigurationFile() {
return this.configurationFile.isPresent();
return this.configurationFiles.isPresent();
}

public Optional<String> getSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected Option configOption() {
Option opt = new Option("c", true, "config");
opt.setType(String.class);
opt.setRequired(false);
opt.setArgs(Option.UNLIMITED_VALUES);
return opt;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.github.dreamhead.moco.runner;

import com.github.dreamhead.moco.bootstrap.arg.StartArgs;
import com.google.common.base.Function;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import static com.github.dreamhead.moco.runner.JsonRunner.newJsonRunnerWithStreams;
import static com.google.common.collect.ImmutableList.of;
import static com.google.common.collect.FluentIterable.from;

public abstract class FileRunner implements Runner {
private Runner runner;
Expand Down Expand Up @@ -38,11 +40,16 @@ public void stop() {
this.runner.stop();
}

public static FileRunner createConfigurationFileRunner(final File file, final StartArgs startArgs) {
public static FileRunner createConfigurationFileRunner(final Iterable<File> files, final StartArgs startArgs) {
return new FileRunner() {
@Override
protected Runner newRunner() {
return newJsonRunnerWithStreams(of(toInputStream(file)), startArgs);
return newJsonRunnerWithStreams(from(files).transform(new Function<File, InputStream>() {
@Override
public InputStream apply(File input) {
return toInputStream(input);
}
}), startArgs);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import com.github.dreamhead.moco.bootstrap.arg.StartArgs;
import com.github.dreamhead.moco.runner.watcher.MocoRunnerWatcher;
import com.github.dreamhead.moco.runner.watcher.MonitorFactory;
import com.google.common.base.Function;
import com.google.common.base.Optional;

import java.io.File;
import java.util.Arrays;

import static com.github.dreamhead.moco.runner.FileRunner.createConfigurationFileRunner;
import static com.github.dreamhead.moco.runner.FileRunner.createSettingFileRunner;
import static com.google.common.collect.FluentIterable.from;

public class RunnerFactory {
private final MonitorFactory monitorFactory = new MonitorFactory();
Expand Down Expand Up @@ -46,9 +49,16 @@ private Runner createDynamicSettingRunner(final StartArgs startArgs) {
}

private Runner createDynamicConfigurationRunner(final StartArgs startArgs) {
final File configuration = new File(startArgs.getConfigurationFile().get());
final FileRunner fileRunner = createConfigurationFileRunner(configuration, startArgs);
MocoRunnerWatcher fileMocoRunnerWatcher = monitorFactory.createConfigurationWatcher(configuration, fileRunner);
final String[] configurations = startArgs.getConfigurationFiles().get();
final Iterable<File> files = from(Arrays.asList(configurations)).transform(new Function<String, File>() {
@Override
public File apply(String input) {
return new File(input);
}
});

final FileRunner fileRunner = createConfigurationFileRunner(files, startArgs);
MocoRunnerWatcher fileMocoRunnerWatcher = monitorFactory.createConfigurationWatcher(files, fileRunner);
return new MonitorRunner(fileRunner, fileMocoRunnerWatcher);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public void onShutdown() {
});
}

public FileMocoRunnerWatcher createConfigurationWatcher(final File configuration, final FileRunner fileRunner) {
return new FileMocoRunnerWatcher(configuration, createListener(fileRunner));
public MocoRunnerWatcher createConfigurationWatcher(final Iterable<File> files, final FileRunner fileRunner) {
return new FilesMocoRunnerWatcher(files, createListener(fileRunner));
}

public MocoRunnerWatcher createSettingWatcher(final File settingsFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void setUp() throws Exception {
public void should_parse_start_arguments() {
StartArgs args = startArgsParser.parse(new String[]{"start", "-p", "12306", "-c", "foo.json"});
assertThat(args.getPort().get(), is(12306));
assertThat(args.getConfigurationFile().get(), is("foo.json"));
assertThat(args.getConfigurationFiles().get(), is(new String[]{"foo.json"}));
}

@Test
Expand Down Expand Up @@ -63,7 +63,7 @@ public void should_not_set_environment_with_config() {
public void should_parse_without_port() {
StartArgs args = startArgsParser.parse(new String[]{"start", "-c", "foo.json"});
assertThat(args.getPort(), is(Optional.<Integer>absent()));
assertThat(args.getConfigurationFile().get(), is("foo.json"));
assertThat(args.getConfigurationFiles().get(), is(new String[]{"foo.json"}));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.dreamhead.moco.runner;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;

import static com.github.dreamhead.moco.bootstrap.arg.HttpArgs.httpArgs;
import static com.github.dreamhead.moco.helper.RemoteTestUtils.*;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class FileRunnerTest extends AbstractRunnerTest {

@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();

@Test
public void should_load_configurations() throws Exception {
final File config1 = tempFolder.newFile();
changeFileContent(config1, "[{\"response\" :{"
+ "\"text\" : \"foo\""
+ "}}]");

final File config2 = tempFolder.newFile();
changeFileContent(config2, "[{" +
"\"request\": {\"uri\": \"/bar\"}," +
"\"response\": {\"text\": \"bar\"}" +
"}]");

RunnerFactory factory = new RunnerFactory("SHUTDOWN");
runner = factory.createRunner(httpArgs()
.withPort(port())
.withShutdownPort(9090)
.withConfigurationFile(config1.getAbsolutePath(), config2.getAbsolutePath())
.build());
runner.run();

assertThat(helper.get(root()), is("foo"));
assertThat(helper.get(remoteUrl("/bar")), is("bar"));
}
}

0 comments on commit 95bdc50

Please sign in to comment.