From 1f7c2935ee9330e511a8f7e9f77567e06ce4faf6 Mon Sep 17 00:00:00 2001 From: Qi Xi Date: Sun, 23 Apr 2017 18:04:46 +0800 Subject: [PATCH] -c argument can support multi files --- .../moco/bootstrap/arg/HttpArgs.java | 14 +++--- .../moco/bootstrap/arg/HttpsArgs.java | 12 +++--- .../moco/bootstrap/arg/SocketArgs.java | 12 +++--- .../moco/bootstrap/arg/StartArgs.java | 12 +++--- .../bootstrap/parser/StartArgsParser.java | 1 + .../dreamhead/moco/runner/FileRunner.java | 13 ++++-- .../dreamhead/moco/runner/RunnerFactory.java | 16 +++++-- .../moco/runner/watcher/MonitorFactory.java | 4 +- .../moco/bootstrap/StartArgsTest.java | 4 +- .../dreamhead/moco/runner/FileRunnerTest.java | 43 +++++++++++++++++++ 10 files changed, 96 insertions(+), 35 deletions(-) create mode 100644 moco-runner/src/test/java/com/github/dreamhead/moco/runner/FileRunnerTest.java diff --git a/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/HttpArgs.java b/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/HttpArgs.java index 74f242f4c..ed8731381 100644 --- a/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/HttpArgs.java +++ b/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/HttpArgs.java @@ -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() { @@ -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; @@ -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; } @@ -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); } } } diff --git a/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/HttpsArgs.java b/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/HttpsArgs.java index 9152f300c..948744749 100644 --- a/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/HttpsArgs.java +++ b/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/HttpsArgs.java @@ -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() { @@ -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; @@ -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; } @@ -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); } } } diff --git a/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/SocketArgs.java b/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/SocketArgs.java index 69b31315a..4f1b11f89 100644 --- a/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/SocketArgs.java +++ b/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/SocketArgs.java @@ -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() { @@ -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; @@ -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); } } } diff --git a/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/StartArgs.java b/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/StartArgs.java index 241d4e783..6ca0aca99 100644 --- a/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/StartArgs.java +++ b/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/arg/StartArgs.java @@ -12,18 +12,18 @@ public abstract class StartArgs extends ShutdownPortOption { private final ServerType type; private final Optional port; - private final Optional configurationFile; + private final Optional configurationFiles; private final Optional settings; private final Optional env; private final Optional 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); @@ -33,12 +33,12 @@ public Optional getPort() { return port; } - public Optional getConfigurationFile() { - return configurationFile; + public Optional getConfigurationFiles() { + return configurationFiles; } public boolean hasConfigurationFile() { - return this.configurationFile.isPresent(); + return this.configurationFiles.isPresent(); } public Optional getSettings() { diff --git a/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/parser/StartArgsParser.java b/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/parser/StartArgsParser.java index b2f86bc09..7a7795b7c 100644 --- a/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/parser/StartArgsParser.java +++ b/moco-runner/src/main/java/com/github/dreamhead/moco/bootstrap/parser/StartArgsParser.java @@ -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; } diff --git a/moco-runner/src/main/java/com/github/dreamhead/moco/runner/FileRunner.java b/moco-runner/src/main/java/com/github/dreamhead/moco/runner/FileRunner.java index 5c13814da..1d2e0a0fb 100644 --- a/moco-runner/src/main/java/com/github/dreamhead/moco/runner/FileRunner.java +++ b/moco-runner/src/main/java/com/github/dreamhead/moco/runner/FileRunner.java @@ -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; @@ -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 files, final StartArgs startArgs) { return new FileRunner() { @Override protected Runner newRunner() { - return newJsonRunnerWithStreams(of(toInputStream(file)), startArgs); + return newJsonRunnerWithStreams(from(files).transform(new Function() { + @Override + public InputStream apply(File input) { + return toInputStream(input); + } + }), startArgs); } }; } diff --git a/moco-runner/src/main/java/com/github/dreamhead/moco/runner/RunnerFactory.java b/moco-runner/src/main/java/com/github/dreamhead/moco/runner/RunnerFactory.java index 85aa7f42d..f71fcb643 100644 --- a/moco-runner/src/main/java/com/github/dreamhead/moco/runner/RunnerFactory.java +++ b/moco-runner/src/main/java/com/github/dreamhead/moco/runner/RunnerFactory.java @@ -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(); @@ -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 files = from(Arrays.asList(configurations)).transform(new Function() { + @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); } } diff --git a/moco-runner/src/main/java/com/github/dreamhead/moco/runner/watcher/MonitorFactory.java b/moco-runner/src/main/java/com/github/dreamhead/moco/runner/watcher/MonitorFactory.java index a9ada5315..e712c85a1 100644 --- a/moco-runner/src/main/java/com/github/dreamhead/moco/runner/watcher/MonitorFactory.java +++ b/moco-runner/src/main/java/com/github/dreamhead/moco/runner/watcher/MonitorFactory.java @@ -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 files, final FileRunner fileRunner) { + return new FilesMocoRunnerWatcher(files, createListener(fileRunner)); } public MocoRunnerWatcher createSettingWatcher(final File settingsFile, diff --git a/moco-runner/src/test/java/com/github/dreamhead/moco/bootstrap/StartArgsTest.java b/moco-runner/src/test/java/com/github/dreamhead/moco/bootstrap/StartArgsTest.java index e73a5b71b..f9d103fe2 100644 --- a/moco-runner/src/test/java/com/github/dreamhead/moco/bootstrap/StartArgsTest.java +++ b/moco-runner/src/test/java/com/github/dreamhead/moco/bootstrap/StartArgsTest.java @@ -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 @@ -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.absent())); - assertThat(args.getConfigurationFile().get(), is("foo.json")); + assertThat(args.getConfigurationFiles().get(), is(new String[]{"foo.json"})); } @Test diff --git a/moco-runner/src/test/java/com/github/dreamhead/moco/runner/FileRunnerTest.java b/moco-runner/src/test/java/com/github/dreamhead/moco/runner/FileRunnerTest.java new file mode 100644 index 000000000..294be18d7 --- /dev/null +++ b/moco-runner/src/test/java/com/github/dreamhead/moco/runner/FileRunnerTest.java @@ -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("1.json"); + changeFileContent(config1, "[{\"response\" :{" + + "\"text\" : \"foo\"" + + "}}]"); + + final File config2 = tempFolder.newFile("2.json"); + 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")); + } +} \ No newline at end of file