Skip to content

Commit

Permalink
Collect stats in SummaryPrinter and AndroidLogcatReporter
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkorstanje committed May 16, 2018
1 parent 05774ef commit 118e732
Show file tree
Hide file tree
Showing 18 changed files with 188 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.RuntimeOptionsFactory;
import cucumber.runtime.Stats;
import cucumber.runtime.UndefinedStepsTracker;
import cucumber.runtime.formatter.AndroidInstrumentationReporter;
import cucumber.runtime.formatter.AndroidLogcatReporter;
import cucumber.runtime.io.ResourceLoader;
Expand Down Expand Up @@ -88,13 +90,18 @@ public CucumberExecutor(final Arguments arguments, final Instrumentation instrum
this.instrumentation = instrumentation;
this.classLoader = context.getClassLoader();
this.classFinder = createDexClassFinder(context);
this.runtimeOptions = createRuntimeOptions(context);
this.runtimeOptions = createRuntimeOptions(context).noSummaryPrinter();

ResourceLoader resourceLoader = new AndroidResourceLoader(context);
this.runtime = new Runtime(resourceLoader, classLoader, createBackends(), runtimeOptions);
AndroidInstrumentationReporter instrumentationReporter = new AndroidInstrumentationReporter(runtime, instrumentation);
UndefinedStepsTracker undefinedStepsTracker = new UndefinedStepsTracker();
undefinedStepsTracker.setEventPublisher(runtime.getEventBus());
Stats stats = new Stats();
stats.setEventPublisher(runtime.getEventBus());

AndroidInstrumentationReporter instrumentationReporter = new AndroidInstrumentationReporter(undefinedStepsTracker, instrumentation);
runtimeOptions.addPlugin(instrumentationReporter);
runtimeOptions.addPlugin(new AndroidLogcatReporter(runtime, TAG));
runtimeOptions.addPlugin(new AndroidLogcatReporter(stats, undefinedStepsTracker, TAG));

List<CucumberFeature> cucumberFeatures = runtimeOptions.cucumberFeatures(resourceLoader, runtime.getEventBus());
this.pickleEvents = FeatureCompiler.compile(cucumberFeatures, this.runtime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import cucumber.api.event.TestSourceRead;
import cucumber.api.event.TestStepFinished;
import cucumber.api.formatter.Formatter;
import cucumber.runtime.Runtime;
import cucumber.runtime.UndefinedStepsTracker;
import cucumber.runtime.Utils;

import java.io.PrintWriter;
Expand Down Expand Up @@ -68,11 +68,6 @@ static class StatusCodes {
*/
private final TestSourcesModel testSources = new TestSourcesModel();

/**
* The current cucumber runtime.
*/
private final Runtime runtime;

/**
* The instrumentation to report to.
*/
Expand Down Expand Up @@ -144,14 +139,16 @@ public void receive(TestCaseFinished event) {
}
};

private final UndefinedStepsTracker undefinedStepsTracker;


/**
* Creates a new instance for the given parameters
*
* @param runtime the {@link cucumber.runtime.Runtime} to use
* @param instrumentation the {@link android.app.Instrumentation} to report statuses to
*/
public AndroidInstrumentationReporter(final Runtime runtime, final Instrumentation instrumentation) {
this.runtime = runtime;
public AndroidInstrumentationReporter(final UndefinedStepsTracker undefinedStepsTracker, final Instrumentation instrumentation) {
this.undefinedStepsTracker = undefinedStepsTracker;
this.instrumentation = instrumentation;
}

Expand Down Expand Up @@ -242,7 +239,7 @@ private Bundle createBundle(final String path, final String testCaseName) {
* @return string representation of the snippet
*/
private String getLastSnippet() {
return runtime.getSnippets().get(runtime.getSnippets().size() - 1);
return undefinedStepsTracker.getSnippets().get(undefinedStepsTracker.getSnippets().size() - 1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@
import cucumber.api.event.TestRunFinished;
import cucumber.api.event.TestStepStarted;
import cucumber.api.formatter.Formatter;
import cucumber.runtime.Runtime;
import cucumber.runtime.Stats;
import cucumber.runtime.UndefinedStepsTracker;

/**
* Logs information about the currently executed statements to androids logcat.
*/
public final class AndroidLogcatReporter implements Formatter {

/**
* The {@link cucumber.runtime.Runtime} to get the errors and snippets from for writing them to the logcat at the end of the execution.
*/
private final Runtime runtime;
public final class AndroidLogcatReporter implements Formatter {

/**
* The log tag to be used when logging to logcat.
Expand All @@ -35,6 +31,10 @@ public void receive(TestCaseStarted event) {
}
};

private final Stats stats;

private final UndefinedStepsTracker undefinedStepsTracker;

/**
* The event handler that logs the {@link TestStepStarted} events.
*/
Expand All @@ -55,11 +55,11 @@ public void receive(TestStepStarted event) {

@Override
public void receive(TestRunFinished event) {
for (final Throwable throwable : runtime.getErrors()) {
for (final Throwable throwable : stats.getErrors()) {
Log.e(logTag, throwable.toString());
}

for (final String snippet : runtime.getSnippets()) {
for (final String snippet : undefinedStepsTracker.getSnippets()) {
Log.w(logTag, snippet);
}
}
Expand All @@ -68,11 +68,12 @@ public void receive(TestRunFinished event) {
/**
* Creates a new instance for the given parameters.
*
* @param runtime the {@link cucumber.runtime.Runtime} to get the errors and snippets from
* @param undefinedStepsTracker
* @param logTag the tag to use for logging to logcat
*/
public AndroidLogcatReporter(final Runtime runtime, final String logTag) {
this.runtime = runtime;
public AndroidLogcatReporter(Stats stats, UndefinedStepsTracker undefinedStepsTracker, final String logTag) {
this.stats = stats;
this.undefinedStepsTracker = undefinedStepsTracker;
this.logTag = logTag;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import cucumber.api.Result;
import cucumber.api.TestCase;
import cucumber.api.event.TestSourceRead;
import cucumber.runtime.Runtime;
import cucumber.runtime.UndefinedStepsTracker;
import cucumber.runtime.formatter.AndroidInstrumentationReporter.StatusCodes;
import edu.emory.mathcs.backport.java.util.Collections;
import org.junit.Before;
Expand All @@ -39,7 +39,7 @@ public class AndroidInstrumentationReporterTest {
@Rule
public final ExpectedException expectedException = ExpectedException.none();

private final Runtime runtime = mock(Runtime.class);
private final UndefinedStepsTracker runtime = mock(UndefinedStepsTracker.class);
private final Instrumentation instrumentation = mock(Instrumentation.class);

private final TestSourceRead testSourceRead = new TestSourceRead(
Expand Down
6 changes: 1 addition & 5 deletions core/src/main/java/cucumber/api/SummaryPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@
* @see Plugin
*/
public interface SummaryPrinter extends Plugin {
/**
* Handles the printing of the summary to the console
* @param summaryPrinting Provides method to print the stats, errors and snippets
*/
void print(SummaryPrintingInterface summaryPrinting);

}
31 changes: 0 additions & 31 deletions core/src/main/java/cucumber/api/SummaryPrintingInterface.java

This file was deleted.

5 changes: 2 additions & 3 deletions core/src/main/java/cucumber/api/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public class Main {

public static void main(String[] argv) throws Throwable {
public static void main(String[] argv) {
byte exitstatus = run(argv, Thread.currentThread().getContextClassLoader());
System.exit(exitstatus);
}
Expand All @@ -25,9 +25,8 @@ public static void main(String[] argv) throws Throwable {
* @param argv runtime options. See details in the {@code cucumber.api.cli.Usage.txt} resource.
* @param classLoader classloader used to load the runtime
* @return 0 if execution was successful, 1 if it was not (test failures)
* @throws IOException if resources couldn't be loaded during the run.
*/
public static byte run(String[] argv, ClassLoader classLoader) throws IOException {
public static byte run(String[] argv, ClassLoader classLoader) {
RuntimeOptions runtimeOptions = new RuntimeOptions(new ArrayList<String>(asList(argv)));

ResourceLoader resourceLoader = new MultiLoader(classLoader);
Expand Down
56 changes: 43 additions & 13 deletions core/src/main/java/cucumber/runtime/DefaultSummaryPrinter.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,48 @@
package cucumber.runtime;

import cucumber.api.SummaryPrinter;
import cucumber.api.SummaryPrintingInterface;
import cucumber.api.event.EventHandler;
import cucumber.api.event.EventPublisher;
import cucumber.api.event.TestRunFinished;
import cucumber.api.formatter.ColorAware;
import cucumber.api.formatter.Formatter;
import cucumber.api.formatter.StrictAware;

import java.io.PrintStream;
import java.util.List;

public class DefaultSummaryPrinter implements SummaryPrinter {
public class DefaultSummaryPrinter implements SummaryPrinter, ColorAware, StrictAware, Formatter {

private final Stats stats = new Stats();
private final UndefinedStepsTracker undefinedStepsTracker = new UndefinedStepsTracker();

private final PrintStream out;

public DefaultSummaryPrinter() {
this.out = System.out;
}

@Override
public void print(SummaryPrintingInterface summaryPrinting) {
private void print() {
out.println();
printStats(summaryPrinting);
printStats();
out.println();
printErrors(summaryPrinting);
printSnippets(summaryPrinting);
printErrors();
printSnippets();
}

private void printStats(SummaryPrintingInterface summaryPrinting) {
summaryPrinting.printStats(out);
private void printStats() {
stats.printStats(out);
}

private void printErrors(SummaryPrintingInterface summaryPrinting) {
for (Throwable error : summaryPrinting.getErrors()) {
private void printErrors() {
for (Throwable error : stats.getErrors()) {
error.printStackTrace(out);
out.println();
}
}

private void printSnippets(SummaryPrintingInterface summaryPrinting) {
List<String> snippets = summaryPrinting.getSnippets();
private void printSnippets() {
List<String> snippets = undefinedStepsTracker.getSnippets();
if (!snippets.isEmpty()) {
out.append("\n");
out.println("You can implement missing steps with the snippets below:");
Expand All @@ -44,4 +52,26 @@ private void printSnippets(SummaryPrintingInterface summaryPrinting) {
}
}
}

@Override
public void setEventPublisher(EventPublisher publisher) {
stats.setEventPublisher(publisher);
undefinedStepsTracker.setEventPublisher(publisher);
publisher.registerHandlerFor(TestRunFinished.class, new EventHandler<TestRunFinished>() {
@Override
public void receive(TestRunFinished event) {
print();
}
});
}

@Override
public void setMonochrome(boolean monochrome) {
stats.setMonochrome(monochrome);
}

@Override
public void setStrict(boolean strict) {
stats.setStrict(strict);
}
}
39 changes: 39 additions & 0 deletions core/src/main/java/cucumber/runtime/ExitStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cucumber.runtime;

import cucumber.api.Result;
import cucumber.api.event.EventHandler;
import cucumber.api.event.EventListener;
import cucumber.api.event.EventPublisher;
import cucumber.api.event.TestCaseFinished;

import java.util.ArrayList;
import java.util.List;

import static cucumber.api.Result.SEVERITY;
import static java.util.Collections.max;

class ExitStatus implements EventListener {
private static final byte ERRORS = 0x1;

private final List<Result> results = new ArrayList<Result>();

private final EventHandler<TestCaseFinished> testCaseFinishedHandler = new EventHandler<TestCaseFinished>() {
@Override
public void receive(TestCaseFinished event) {
results.add(event.result);
}
};

ExitStatus() {
}


@Override
public void setEventPublisher(EventPublisher publisher) {
publisher.registerHandlerFor(TestCaseFinished.class, testCaseFinishedHandler);
}

public byte exitStatus(boolean isStrict) {
return results.isEmpty() || max(results, SEVERITY).isOk(isStrict) ? 0x0 : ERRORS;
}
}
6 changes: 0 additions & 6 deletions core/src/main/java/cucumber/runtime/NullSummaryPrinter.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package cucumber.runtime;

import cucumber.api.SummaryPrinter;
import cucumber.api.SummaryPrintingInterface;

public class NullSummaryPrinter implements SummaryPrinter {

@Override
public void print(SummaryPrintingInterface summaryPrinting) {
// Do nothing
}

}
Loading

0 comments on commit 118e732

Please sign in to comment.