Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JUnit] Make duplicate pickle names unique #2045

Merged
merged 3 commits into from Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions junit/src/main/java/io/cucumber/junit/Cucumber.java
Expand Up @@ -5,8 +5,6 @@
import io.cucumber.core.filter.Filters;
import io.cucumber.core.gherkin.Feature;
import io.cucumber.core.gherkin.Pickle;
import io.cucumber.core.logging.Logger;
import io.cucumber.core.logging.LoggerFactory;
import io.cucumber.core.options.Constants;
import io.cucumber.core.options.CucumberOptionsAnnotationParser;
import io.cucumber.core.options.CucumberProperties;
Expand Down Expand Up @@ -40,10 +38,14 @@

import java.time.Clock;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.function.Supplier;

import static io.cucumber.junit.FileNameCompatibleNames.uniqueSuffix;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;

/**
Expand Down Expand Up @@ -88,8 +90,6 @@
@API(status = API.Status.STABLE)
public final class Cucumber extends ParentRunner<ParentRunner<?>> {

private static final Logger log = LoggerFactory.getLogger(Cucumber.class);

private final List<ParentRunner<?>> children;
private final EventBus bus;
private final List<Feature> features;
Expand Down Expand Up @@ -171,8 +171,14 @@ public Cucumber(Class<?> clazz) throws InitializationError {
objectFactorySupplier, typeRegistryConfigurerSupplier);
this.context = new CucumberExecutionContext(bus, exitStatus, runnerSupplier);
Predicate<Pickle> filters = new Filters(runtimeOptions);

Map<Optional<String>, List<Feature>> groupedByName = features.stream()
.collect(groupingBy(Feature::getName));
this.children = features.stream()
.map(feature -> FeatureRunner.create(feature, filters, runnerSupplier, junitOptions))
.map(feature -> {
Integer uniqueSuffix = uniqueSuffix(groupedByName, feature, Feature::getName);
return FeatureRunner.create(feature, uniqueSuffix, filters, runnerSupplier, junitOptions);
})
.filter(runner -> !runner.isEmpty())
.collect(toList());
}
Expand Down
35 changes: 26 additions & 9 deletions junit/src/main/java/io/cucumber/junit/FeatureRunner.java
Expand Up @@ -14,38 +14,55 @@
import java.io.Serializable;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import static io.cucumber.junit.FileNameCompatibleNames.createName;
import static io.cucumber.junit.FileNameCompatibleNames.uniqueSuffix;
import static io.cucumber.junit.PickleRunners.withNoStepDescriptions;
import static io.cucumber.junit.PickleRunners.withStepDescriptions;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;

final class FeatureRunner extends ParentRunner<PickleRunner> {

private final List<PickleRunner> children;
private final Feature feature;
private final JUnitOptions options;
private final Integer uniqueSuffix;
private Description description;

private FeatureRunner(Feature feature, Predicate<Pickle> filter, RunnerSupplier runners, JUnitOptions options)
private FeatureRunner(
Feature feature, Integer uniqueSuffix, Predicate<Pickle> filter, RunnerSupplier runners,
JUnitOptions options
)
throws InitializationError {
super((Class<?>) null);
this.feature = feature;
this.uniqueSuffix = uniqueSuffix;
this.options = options;
String name = feature.getName().orElse("EMPTY_NAME");
this.children = feature.getPickles().stream()
.filter(filter).map(pickle -> options.stepNotifications()
? withStepDescriptions(runners, pickle, options)
: withNoStepDescriptions(name, runners, pickle, options))

Map<String, List<Pickle>> groupedByName = feature.getPickles().stream()
.collect(groupingBy(Pickle::getName));
this.children = feature.getPickles()
.stream()
.filter(filter)
.map(pickle -> {
String featureName = getName();
Integer exampleId = uniqueSuffix(groupedByName, pickle, Pickle::getName);
return options.stepNotifications()
? withStepDescriptions(runners, pickle, exampleId, options)
: withNoStepDescriptions(featureName, runners, pickle, exampleId, options);
})
.collect(toList());
}

static FeatureRunner create(
Feature feature, Predicate<Pickle> filter, RunnerSupplier runners, JUnitOptions options
Feature feature, Integer uniqueSuffix, Predicate<Pickle> filter, RunnerSupplier runners,
JUnitOptions options
) {
try {
return new FeatureRunner(feature, filter, runners, options);
return new FeatureRunner(feature, uniqueSuffix, filter, runners, options);
} catch (InitializationError e) {
throw new CucumberException("Failed to create scenario runner", e);
}
Expand Down Expand Up @@ -89,7 +106,7 @@ public String toString() {
@Override
protected String getName() {
String name = feature.getName().orElse("EMPTY_NAME");
return createName(name, options.filenameCompatibleNames());
return createName(name, uniqueSuffix, options.filenameCompatibleNames());
}

@Override
Expand Down
@@ -1,17 +1,33 @@
package io.cucumber.junit;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

final class FileNameCompatibleNames {

static String createName(String name, Integer uniqueSuffix, boolean useFilenameCompatibleNames) {
if (uniqueSuffix == null) {
return createName(name, useFilenameCompatibleNames);
}
return createName(name + " #" + uniqueSuffix + "", useFilenameCompatibleNames);
}

static String createName(final String name, boolean useFilenameCompatibleNames) {
if (useFilenameCompatibleNames) {
return makeNameFilenameCompatible(name);
}

return name;
}

private static String makeNameFilenameCompatible(String name) {
return name.replaceAll("[^A-Za-z0-9_]", "_");
}

static <V, K> Integer uniqueSuffix(Map<K, List<V>> groupedByName, V pickle, Function<V, K> nameOf) {
List<V> withSameName = groupedByName.get(nameOf.apply(pickle));
boolean makeNameUnique = withSameName.size() > 1;
return makeNameUnique ? withSameName.indexOf(pickle) + 1 : null;
}

}
31 changes: 21 additions & 10 deletions junit/src/main/java/io/cucumber/junit/PickleRunners.java
Expand Up @@ -21,18 +21,21 @@

final class PickleRunners {

static PickleRunner withStepDescriptions(RunnerSupplier runnerSupplier, Pickle pickle, JUnitOptions options) {
static PickleRunner withStepDescriptions(
RunnerSupplier runnerSupplier, Pickle pickle, Integer uniqueSuffix, JUnitOptions options
) {
try {
return new WithStepDescriptions(runnerSupplier, pickle, options);
return new WithStepDescriptions(runnerSupplier, pickle, uniqueSuffix, options);
} catch (InitializationError e) {
throw new CucumberException("Failed to create scenario runner", e);
}
}

static PickleRunner withNoStepDescriptions(
String featureName, RunnerSupplier runnerSupplier, Pickle pickle, JUnitOptions jUnitOptions
String featureName, RunnerSupplier runnerSupplier, Pickle pickle, Integer uniqueSuffix,
JUnitOptions jUnitOptions
) {
return new NoStepDescriptions(featureName, runnerSupplier, pickle, jUnitOptions);
return new NoStepDescriptions(featureName, runnerSupplier, pickle, uniqueSuffix, jUnitOptions);
}

interface PickleRunner {
Expand All @@ -51,14 +54,18 @@ static class WithStepDescriptions extends ParentRunner<Step> implements PickleRu
private final Pickle pickle;
private final JUnitOptions jUnitOptions;
private final Map<Step, Description> stepDescriptions = new HashMap<>();
private final Integer uniqueSuffix;
private Description description;

WithStepDescriptions(RunnerSupplier runnerSupplier, Pickle pickle, JUnitOptions jUnitOptions)
WithStepDescriptions(
RunnerSupplier runnerSupplier, Pickle pickle, Integer uniqueSuffix, JUnitOptions jUnitOptions
)
throws InitializationError {
super((Class<?>) null);
this.runnerSupplier = runnerSupplier;
this.pickle = pickle;
this.jUnitOptions = jUnitOptions;
this.uniqueSuffix = uniqueSuffix;
}

@Override
Expand All @@ -70,7 +77,7 @@ protected List<Step> getChildren() {

@Override
protected String getName() {
return createName(pickle.getName(), jUnitOptions.filenameCompatibleNames());
return createName(pickle.getName(), uniqueSuffix, jUnitOptions.filenameCompatibleNames());
}

@Override
Expand All @@ -86,8 +93,9 @@ public Description getDescription() {
public Description describeChild(Step step) {
Description description = stepDescriptions.get(step);
if (description == null) {
String testName = createName(step.getText(), jUnitOptions.filenameCompatibleNames());
description = Description.createTestDescription(getName(), testName, new PickleStepId(pickle, step));
String className = getName();
String name = createName(step.getText(), jUnitOptions.filenameCompatibleNames());
description = Description.createTestDescription(className, name, new PickleStepId(pickle, step));
stepDescriptions.put(step, description);
}
return description;
Expand Down Expand Up @@ -120,15 +128,18 @@ static final class NoStepDescriptions implements PickleRunner {
private final RunnerSupplier runnerSupplier;
private final Pickle pickle;
private final JUnitOptions jUnitOptions;
private final Integer uniqueSuffix;
private Description description;

NoStepDescriptions(
String featureName, RunnerSupplier runnerSupplier, Pickle pickle, JUnitOptions jUnitOptions
String featureName, RunnerSupplier runnerSupplier, Pickle pickle, Integer uniqueSuffix,
JUnitOptions jUnitOptions
) {
this.featureName = featureName;
this.runnerSupplier = runnerSupplier;
this.pickle = pickle;
this.jUnitOptions = jUnitOptions;
this.uniqueSuffix = uniqueSuffix;
}

@Override
Expand All @@ -145,7 +156,7 @@ public void run(final RunNotifier notifier) {
public Description getDescription() {
if (description == null) {
String className = createName(featureName, jUnitOptions.filenameCompatibleNames());
String name = createName(pickle.getName(), jUnitOptions.filenameCompatibleNames());
String name = createName(pickle.getName(), uniqueSuffix, jUnitOptions.filenameCompatibleNames());
description = Description.createTestDescription(className, name, new PickleId(pickle));
}
return description;
Expand Down
58 changes: 44 additions & 14 deletions junit/src/test/java/io/cucumber/junit/CucumberTest.java
Expand Up @@ -53,14 +53,14 @@ void ensureOriginalDirectory() {
@Test
void finds_features_based_on_implicit_package() throws InitializationError {
Cucumber cucumber = new Cucumber(ImplicitFeatureAndGluePath.class);
assertThat(cucumber.getChildren().size(), is(equalTo(6)));
assertThat(cucumber.getChildren().size(), is(equalTo(7)));
assertThat(cucumber.getChildren().get(1).getDescription().getDisplayName(), is(equalTo("Feature A")));
}

@Test
void finds_features_based_on_explicit_root_package() throws InitializationError {
Cucumber cucumber = new Cucumber(ExplicitFeaturePath.class);
assertThat(cucumber.getChildren().size(), is(equalTo(6)));
assertThat(cucumber.getChildren().size(), is(equalTo(7)));
assertThat(cucumber.getChildren().get(1).getDescription().getDisplayName(), is(equalTo("Feature A")));
}

Expand Down Expand Up @@ -104,28 +104,58 @@ void cucumber_can_run_features_in_parallel() throws Exception {
Request.classes(computer, ValidEmpty.class).getRunner().run(notifier);
{
InOrder order = Mockito.inOrder(listener);
order.verify(listener).testStarted(argThat(new DescriptionMatcher("Followed by some examples(Feature A)")));

order.verify(listener)
.testStarted(argThat(new DescriptionMatcher("Followed by some examples #1(Feature A)")));
order.verify(listener)
.testFinished(argThat(new DescriptionMatcher("Followed by some examples #1(Feature A)")));
order.verify(listener)
.testFinished(argThat(new DescriptionMatcher("Followed by some examples(Feature A)")));
order.verify(listener).testStarted(argThat(new DescriptionMatcher("Followed by some examples(Feature A)")));
.testStarted(argThat(new DescriptionMatcher("Followed by some examples #2(Feature A)")));
order.verify(listener)
.testFinished(argThat(new DescriptionMatcher("Followed by some examples(Feature A)")));
order.verify(listener).testStarted(argThat(new DescriptionMatcher("Followed by some examples(Feature A)")));
.testFinished(argThat(new DescriptionMatcher("Followed by some examples #2(Feature A)")));
order.verify(listener)
.testFinished(argThat(new DescriptionMatcher("Followed by some examples(Feature A)")));
.testStarted(argThat(new DescriptionMatcher("Followed by some examples #3(Feature A)")));
order.verify(listener)
.testFinished(argThat(new DescriptionMatcher("Followed by some examples #3(Feature A)")));
}
{
InOrder order = Mockito.inOrder(listener);
order.verify(listener).testStarted(argThat(new DescriptionMatcher("A(Feature B)")));
order.verify(listener).testFinished(argThat(new DescriptionMatcher("A(Feature B)")));
order.verify(listener).testStarted(argThat(new DescriptionMatcher("B(Feature B)")));
order.verify(listener).testFinished(argThat(new DescriptionMatcher("B(Feature B)")));
order.verify(listener).testStarted(argThat(new DescriptionMatcher("C(Feature B)")));
order.verify(listener).testFinished(argThat(new DescriptionMatcher("C(Feature B)")));
order.verify(listener).testStarted(argThat(new DescriptionMatcher("C(Feature B)")));
order.verify(listener).testFinished(argThat(new DescriptionMatcher("C(Feature B)")));
order.verify(listener).testStarted(argThat(new DescriptionMatcher("C(Feature B)")));
order.verify(listener).testFinished(argThat(new DescriptionMatcher("C(Feature B)")));
order.verify(listener).testStarted(argThat(new DescriptionMatcher("C #1(Feature B)")));
order.verify(listener).testFinished(argThat(new DescriptionMatcher("C #1(Feature B)")));
order.verify(listener).testStarted(argThat(new DescriptionMatcher("C #2(Feature B)")));
order.verify(listener).testFinished(argThat(new DescriptionMatcher("C #2(Feature B)")));
order.verify(listener).testStarted(argThat(new DescriptionMatcher("C #3(Feature B)")));
order.verify(listener).testFinished(argThat(new DescriptionMatcher("C #3(Feature B)")));
}
}

@Test
void cucumber_distinguishes_between_identical_features() throws Exception {
RunNotifier notifier = new RunNotifier();
RunListener listener = Mockito.mock(RunListener.class);
notifier.addListener(listener);
Request.classes(ValidEmpty.class).getRunner().run(notifier);
{
InOrder order = Mockito.inOrder(listener);

order.verify(listener)
.testStarted(
argThat(new DescriptionMatcher("A single scenario(A feature with a single scenario #1)")));
order.verify(listener)
.testFinished(
argThat(new DescriptionMatcher("A single scenario(A feature with a single scenario #1)")));

order.verify(listener)
.testStarted(
argThat(new DescriptionMatcher("A single scenario(A feature with a single scenario #2)")));
order.verify(listener)
.testFinished(
argThat(new DescriptionMatcher("A single scenario(A feature with a single scenario #2)")));

}
}

Expand Down