Skip to content

Commit

Permalink
Streamline Test API - hide BQTestRuntime wrapper #142
Browse files Browse the repository at this point in the history
* removing non-deamon runtime wrapper
  • Loading branch information
andrus committed Apr 30, 2017
1 parent 44cb1ab commit 6c0d4d6
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 119 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
/** /**
* @since 0.13 * @since 0.13
*/ */
public class BQDaemonTestRuntime extends BQTestRuntime { public class BQDaemonTestRuntime {


private BQRuntime runtime;
private ExecutorService executor; private ExecutorService executor;
private Function<BQDaemonTestRuntime, Boolean> startupCheck; private Function<BQDaemonTestRuntime, Boolean> startupCheck;
private Optional<CommandOutcome> outcome; private Optional<CommandOutcome> outcome;


public BQDaemonTestRuntime(BQRuntime runtime, InMemoryPrintStream stdout, InMemoryPrintStream stderr, public BQDaemonTestRuntime(BQRuntime runtime,
Function<BQDaemonTestRuntime, Boolean> startupCheck) { Function<BQDaemonTestRuntime, Boolean> startupCheck) {
super(runtime, stdout, stderr); this.runtime = runtime;
this.startupCheck = startupCheck; this.startupCheck = startupCheck;
this.executor = Executors.newCachedThreadPool(); this.executor = Executors.newCachedThreadPool();
this.outcome = Optional.empty(); this.outcome = Optional.empty();
Expand All @@ -38,17 +39,21 @@ public Optional<CommandOutcome> getOutcome() {
return outcome; return outcome;
} }


public BQRuntime getRuntime() {
return runtime;
}

public void start(long timeout, TimeUnit unit) { public void start(long timeout, TimeUnit unit) {
start(); start();
checkStartupSucceeded(timeout, unit); checkStartupSucceeded(timeout, unit);
} }


protected void start() { protected void start() {
this.executor.submit(() -> outcome = Optional.of(run())); this.executor.submit(() -> outcome = Optional.of(runtime.run()));
} }


protected void checkStartupSucceeded(long timeout, TimeUnit unit) { protected void checkStartupSucceeded(long timeout, TimeUnit unit) {
BootLogger logger = getRuntime().getBootLogger(); BootLogger logger = runtime.getBootLogger();


Future<Boolean> startupFuture = executor.submit(() -> { Future<Boolean> startupFuture = executor.submit(() -> {


Expand Down Expand Up @@ -83,12 +88,11 @@ protected void checkStartupSucceeded(long timeout, TimeUnit unit) {
} }
} }


@Override
public void stop() { public void stop() {


BootLogger logger = getRuntime().getBootLogger(); BootLogger logger = runtime.getBootLogger();


super.stop(); runtime.shutdown();


// must interrupt execution (using "shutdown()" is not enough to stop // must interrupt execution (using "shutdown()" is not enough to stop
// Jetty for instance // Jetty for instance
Expand Down
60 changes: 0 additions & 60 deletions bootique-test/src/main/java/io/bootique/test/BQTestRuntime.java

This file was deleted.

Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.bootique.test.junit; package io.bootique.test.junit;


import io.bootique.BQRuntime; import io.bootique.BQRuntime;
import io.bootique.log.BootLogger;
import io.bootique.log.DefaultBootLogger;
import io.bootique.test.BQDaemonTestRuntime; import io.bootique.test.BQDaemonTestRuntime;
import io.bootique.test.InMemoryPrintStream;
import org.junit.ClassRule; import org.junit.ClassRule;
import org.junit.Rule; import org.junit.Rule;
import org.junit.rules.ExternalResource; import org.junit.rules.ExternalResource;
Expand Down Expand Up @@ -135,14 +132,8 @@ public BQDaemonTestRuntime start(String... args) {
*/ */
public BQDaemonTestRuntime start() { public BQDaemonTestRuntime start() {


InMemoryPrintStream stdout = new InMemoryPrintStream(System.out); BQRuntime runtime = bootique.createRuntime();
InMemoryPrintStream stderr = new InMemoryPrintStream(System.err); BQDaemonTestRuntime testRuntime = new BQDaemonTestRuntime(runtime, startupCheck);

// TODO: allow to turn off tracing, which can be either useful or annoying dependning on the context...
BootLogger bootLogger = new DefaultBootLogger(true, stdout, stderr);

BQRuntime runtime = bootique.bootLogger(bootLogger).createRuntime();
BQDaemonTestRuntime testRuntime = new BQDaemonTestRuntime(runtime, stdout, stderr, startupCheck);
runtimes.add(testRuntime); runtimes.add(testRuntime);


testRuntime.start(startupTimeout, startupTimeoutTimeUnit); testRuntime.start(startupTimeout, startupTimeoutTimeUnit);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected void testMetadata() {


testWithFactory(testFactory -> { testWithFactory(testFactory -> {
// must auto-load modules to ensure all tested module dependencies are present... // must auto-load modules to ensure all tested module dependencies are present...
BQRuntime runtime = testFactory.app().autoLoadModules().createRuntime().getRuntime(); BQRuntime runtime = testFactory.app().autoLoadModules().createRuntime();


BQModuleProvider provider = matchingProvider(); BQModuleProvider provider = matchingProvider();
String providerName = provider.name(); String providerName = provider.name();
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@


import io.bootique.BQRuntime; import io.bootique.BQRuntime;
import io.bootique.config.ConfigurationFactory; import io.bootique.config.ConfigurationFactory;
import io.bootique.test.BQTestRuntime;
import io.bootique.test.InMemoryPrintStream;
import org.junit.ClassRule; import org.junit.ClassRule;
import org.junit.Rule; import org.junit.Rule;
import org.junit.rules.ExternalResource; import org.junit.rules.ExternalResource;
Expand All @@ -27,16 +25,16 @@
*/ */
public class BQTestFactory extends ExternalResource { public class BQTestFactory extends ExternalResource {


private Collection<BQTestRuntime> runtimes; private Collection<BQRuntime> runtimes;


@Override @Override
protected void after() { protected void after() {
Collection<BQTestRuntime> localRuntimes = this.runtimes; Collection<BQRuntime> localRuntimes = this.runtimes;


if (localRuntimes != null) { if (localRuntimes != null) {
localRuntimes.forEach(runtime -> { localRuntimes.forEach(runtime -> {
try { try {
runtime.stop(); runtime.shutdown();
} catch (Exception e) { } catch (Exception e) {
// ignore... // ignore...
} }
Expand Down Expand Up @@ -68,39 +66,22 @@ public Builder app(String... args) {


public static class Builder extends BQTestRuntimeBuilder<Builder> { public static class Builder extends BQTestRuntimeBuilder<Builder> {


private Collection<BQTestRuntime> runtimes; private Collection<BQRuntime> runtimes;


private Builder(Collection<BQTestRuntime> runtimes, String[] args) { private Builder(Collection<BQRuntime> runtimes, String[] args) {
super(args); super(args);
this.runtimes = runtimes; this.runtimes = runtimes;
} }


/** /**
* @param args arguments for the test stack app. * The main build method that creates and returns a {@link BQRuntime}.
* @return a new instance of test runtime.
* @deprecated since 0.20 in favor of {@link #createRuntime()}.
*/
@Deprecated
public BQTestRuntime build(String... args) {
bootique.args(args);
return createRuntime();
}

/**
* The main build method that creates and returns a {@link BQTestRuntime}, which is a thin wrapper for
* Bootique runtime.
* *
* @return a new instance of {@link BQTestRuntime} configured in this builder. * @return a new instance of {@link BQRuntime} configured in this builder.
*/ */
public BQTestRuntime createRuntime() { public BQRuntime createRuntime() {

InMemoryPrintStream stdout = new InMemoryPrintStream(System.out);
InMemoryPrintStream stderr = new InMemoryPrintStream(System.err);

BQRuntime runtime = bootique.createRuntime(); BQRuntime runtime = bootique.createRuntime();
BQTestRuntime testRuntime = new BQTestRuntime(runtime, stdout, stderr); runtimes.add(runtime);
runtimes.add(testRuntime); return runtime;
return testRuntime;
} }
} }
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@


import com.google.inject.Inject; import com.google.inject.Inject;
import io.bootique.BQCoreModule; import io.bootique.BQCoreModule;
import io.bootique.BQRuntime;
import io.bootique.cli.Cli; import io.bootique.cli.Cli;
import io.bootique.command.CommandOutcome; import io.bootique.command.CommandOutcome;
import io.bootique.command.CommandWithMetadata; import io.bootique.command.CommandWithMetadata;
import io.bootique.log.BootLogger; import io.bootique.log.BootLogger;
import io.bootique.meta.application.CommandMetadata; import io.bootique.meta.application.CommandMetadata;
import io.bootique.test.BQTestRuntime;
import io.bootique.test.TestIO; import io.bootique.test.TestIO;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;


import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;


public class BQTestFactoryIT { public class BQTestFactoryIT {


Expand All @@ -23,22 +21,21 @@ public class BQTestFactoryIT {


@Test @Test
public void testCreateRuntime_Injection() { public void testCreateRuntime_Injection() {
BQTestRuntime runtime = testFactory.app("-x").autoLoadModules().createRuntime(); BQRuntime runtime = testFactory.app("-x").autoLoadModules().createRuntime();
assertArrayEquals(new String[]{"-x"}, runtime.getRuntime().getArgs()); assertArrayEquals(new String[]{"-x"}, runtime.getArgs());
} }


@Test @Test
public void testCreateRuntime_Streams_NoTrace() { public void testCreateRuntime_Streams_NoTrace() {


TestIO io = TestIO.noTrace(); TestIO io = TestIO.noTrace();


BQTestRuntime runtime = testFactory.app("-x") CommandOutcome result = testFactory.app("-x")
.autoLoadModules() .autoLoadModules()
.module(b -> BQCoreModule.extend(b).addCommand(XCommand.class)) .module(b -> BQCoreModule.extend(b).addCommand(XCommand.class))
.bootLogger(io.getBootLogger()) .bootLogger(io.getBootLogger())
.createRuntime(); .createRuntime()

.run();
CommandOutcome result = runtime.run();


assertTrue(result.isSuccess()); assertTrue(result.isSuccess());
assertEquals("--out--", io.getStdout().trim()); assertEquals("--out--", io.getStdout().trim());
Expand Down

0 comments on commit 6c0d4d6

Please sign in to comment.