Skip to content

Commit e8f6b3b

Browse files
committed
[Core] Decouple UndefinedStepsTracker from Glue
Glue only served to pass the UndefinedStepsTracker between Runtime and Runtime. Decoupling this removes some pointless complexity. The constructor `RuntimeGlue(UndefinedStepsTracker tracker, LocalizedXStreams localizedXStreams)` has been deprecated to avoid breaking existing implementations. The Runtime will not use the provided UndefinedStepsTracker but this is consistent with the behaviour prior to #1019. Third parties interested in undefined steps can use the subscribe their own UndefinedStepsTracker to the event bus.
1 parent 786f38a commit e8f6b3b

File tree

7 files changed

+18
-37
lines changed

7 files changed

+18
-37
lines changed

core/src/main/java/cucumber/runtime/Glue.java

-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,4 @@ public interface Glue {
2626
void reportStepDefinitions(StepDefinitionReporter stepDefinitionReporter);
2727

2828
void removeScenarioScopedGlue();
29-
30-
UndefinedStepsTracker getTracker();
3129
}

core/src/main/java/cucumber/runtime/Runtime.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
public class Runtime {
2828

2929
final Stats stats; // package private to be avaiable for tests.
30-
private final UndefinedStepsTracker undefinedStepsTracker;
30+
private final UndefinedStepsTracker undefinedStepsTracker = new UndefinedStepsTracker();
3131

3232
private final RuntimeOptions runtimeOptions;
3333

@@ -59,13 +59,7 @@ public Runtime(ResourceLoader resourceLoader, ClassLoader classLoader, Collectio
5959
this.classLoader = classLoader;
6060
this.runtimeOptions = runtimeOptions;
6161
final Glue glue;
62-
if (optionalGlue == null) {
63-
this.undefinedStepsTracker = new UndefinedStepsTracker();
64-
glue = new RuntimeGlue(undefinedStepsTracker, new LocalizedXStreams(classLoader));
65-
} else {
66-
this.undefinedStepsTracker = optionalGlue.getTracker();
67-
glue = optionalGlue;
68-
}
62+
glue = optionalGlue == null ? new RuntimeGlue(new LocalizedXStreams(classLoader)) : optionalGlue;
6963
this.stats = new Stats(runtimeOptions.isMonochrome());
7064
this.bus = new EventBus(stopWatch);
7165
this.runner = new Runner(glue, bus, backends, runtimeOptions);

core/src/main/java/cucumber/runtime/RuntimeGlue.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ public class RuntimeGlue implements Glue {
1616
final List<HookDefinition> beforeHooks = new ArrayList<HookDefinition>();
1717
final List<HookDefinition> afterHooks = new ArrayList<HookDefinition>();
1818

19-
private final UndefinedStepsTracker tracker;
2019
private final LocalizedXStreams localizedXStreams;
2120

21+
public RuntimeGlue(LocalizedXStreams localizedXStreams) {
22+
this(null, localizedXStreams);
23+
}
24+
25+
@Deprecated
2226
public RuntimeGlue(UndefinedStepsTracker tracker, LocalizedXStreams localizedXStreams) {
23-
this.tracker = tracker;
2427
this.localizedXStreams = localizedXStreams;
2528
}
2629

@@ -113,7 +116,4 @@ private void removeScenarioScopedStepdefs() {
113116
}
114117
}
115118

116-
public UndefinedStepsTracker getTracker() {
117-
return tracker;
118-
}
119119
}

core/src/main/java/cucumber/runtime/UndefinedStepsTracker.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package cucumber.runtime;
22

3-
import cucumber.api.Result;
4-
import cucumber.api.TestCase;
5-
import cucumber.api.TestStep;
63
import cucumber.api.event.EventHandler;
74
import cucumber.api.event.EventListener;
85
import cucumber.api.event.EventPublisher;
@@ -32,7 +29,6 @@ public class UndefinedStepsTracker implements EventListener {
3229
private final Map<String, String> pathToSourceMap = new HashMap<String, String>();
3330
private final Map<String, FeatureStepMap> pathToStepMap = new HashMap<String, FeatureStepMap>();
3431
private boolean hasUndefinedSteps = false;
35-
private String currentUri;
3632

3733
private EventHandler<TestSourceRead> testSourceReadHandler = new EventHandler<TestSourceRead>() {
3834
@Override
@@ -160,21 +156,21 @@ private String convertToCodeKeyword(String keyword) {
160156
return keyword.replaceAll("[\\s',!]", "");
161157
}
162158

163-
private class FeatureStepMap {
164-
public final GherkinDialect dialect;
165-
public final Map<Integer, StepNode> stepMap;
159+
private static final class FeatureStepMap {
160+
final GherkinDialect dialect;
161+
final Map<Integer, StepNode> stepMap;
166162

167-
public FeatureStepMap(GherkinDialect dialect, Map<Integer, StepNode> stepMap) {
163+
FeatureStepMap(GherkinDialect dialect, Map<Integer, StepNode> stepMap) {
168164
this.dialect = dialect;
169165
this.stepMap = stepMap;
170166
}
171167
}
172168

173-
private class StepNode {
174-
public final Step step;
175-
public final StepNode previous;
169+
private static final class StepNode {
170+
final Step step;
171+
final StepNode previous;
176172

177-
public StepNode(Step step, StepNode previous) {
173+
StepNode(Step step, StepNode previous) {
178174
this.step = step;
179175
this.previous = previous;
180176
}

core/src/test/java/cucumber/runtime/RuntimeGlueTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
public class RuntimeGlueTest {
1212
@Test
1313
public void throws_duplicate_error_on_dupe_stepdefs() {
14-
RuntimeGlue glue = new RuntimeGlue(new UndefinedStepsTracker(), new LocalizedXStreams(Thread.currentThread().getContextClassLoader()));
14+
RuntimeGlue glue = new RuntimeGlue(new LocalizedXStreams(Thread.currentThread().getContextClassLoader()));
1515

1616
StepDefinition a = mock(StepDefinition.class);
1717
when(a.getPattern()).thenReturn("hello");
@@ -35,7 +35,7 @@ public void removes_glue_that_is_scenario_scoped() {
3535
// But it was too much hassle creating a better test without refactoring RuntimeGlue
3636
// and probably some of its immediate collaborators... Aslak.
3737

38-
RuntimeGlue glue = new RuntimeGlue(new UndefinedStepsTracker(), new LocalizedXStreams(Thread.currentThread().getContextClassLoader()));
38+
RuntimeGlue glue = new RuntimeGlue(new LocalizedXStreams(Thread.currentThread().getContextClassLoader()));
3939

4040
StepDefinition sd = mock(StepDefinition.class);
4141
when(sd.isScenarioScoped()).thenReturn(true);

core/src/test/java/cucumber/runtime/RuntimeTest.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -543,20 +543,14 @@ private Runtime createRuntimeWithMockedGlue(StepDefinitionMatch match, boolean i
543543
}
544544
RuntimeOptions runtimeOptions = new RuntimeOptions(args);
545545
Backend backend = mock(Backend.class);
546-
RuntimeGlue glue = mockGlue();
546+
RuntimeGlue glue = mock(RuntimeGlue.class);
547547
mockMatch(glue, match, isAmbiguous);
548548
mockHook(glue, hook, isBefore);
549549
Collection<Backend> backends = Arrays.asList(backend);
550550

551551
return new Runtime(resourceLoader, classLoader, backends, runtimeOptions, glue);
552552
}
553553

554-
private RuntimeGlue mockGlue() {
555-
RuntimeGlue glue = mock(RuntimeGlue.class);
556-
when(glue.getTracker()).thenReturn(new UndefinedStepsTracker());
557-
return glue;
558-
}
559-
560554
private void mockMatch(RuntimeGlue glue, StepDefinitionMatch match, boolean isAmbiguous) {
561555
if (isAmbiguous) {
562556
Exception exception = new AmbiguousStepDefinitionsException(mock(PickleStep.class), Arrays.asList(match, match));

core/src/test/java/cucumber/runtime/TestHelper.java

-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ private static RuntimeGlue createMockedRuntimeGlueThatMatchesTheSteps(final Map<
159159
final List<SimpleEntry<String, Result>> hooks, final List<String> hookLocations,
160160
final List<Answer<Object>> hookActions) throws Throwable {
161161
RuntimeGlue glue = mock(RuntimeGlue.class);
162-
when(glue.getTracker()).thenReturn(new UndefinedStepsTracker());
163162
TestHelper.mockSteps(glue, stepsToResult, stepsToLocation);
164163
TestHelper.mockHooks(glue, hooks, hookLocations, hookActions);
165164
return glue;

0 commit comments

Comments
 (0)