io.cucumber.core.plugin.CanonicalEventOrder Maven / Gradle / Ivy
package io.cucumber.core.plugin;
import io.cucumber.plugin.event.Event;
import io.cucumber.plugin.event.SnippetsSuggestedEvent;
import io.cucumber.plugin.event.StepDefinedEvent;
import io.cucumber.plugin.event.TestCaseEvent;
import io.cucumber.plugin.event.TestRunFinished;
import io.cucumber.plugin.event.TestRunStarted;
import io.cucumber.plugin.event.TestSourceParsed;
import io.cucumber.plugin.event.TestSourceRead;
import java.util.Comparator;
import java.util.List;
import static java.util.Arrays.asList;
/**
* When pickles are executed in parallel events can be produced with a partial
* ordering.
*
* The canonical order is the order in which these events would have been
* generated had cucumber executed these pickles in a serial fashion.
*
* In canonical order events are first ordered by type:
*
* - TestRunStarted
*
- TestSourceRead
*
- SnippetsSuggestedEvent
*
- TestCaseEvent
*
- TestRunFinished
*
*
* Then TestCaseEvents are ordered by
*
* - uri
*
- line
*
- timestamp
*
*/
final class CanonicalEventOrder implements Comparator {
private static final FixedEventOrderComparator fixedOrder = new FixedEventOrderComparator();
private static final TestCaseEventComparator testCaseOrder = new TestCaseEventComparator();
@Override
public int compare(Event a, Event b) {
int fixedOrder = CanonicalEventOrder.fixedOrder.compare(a, b);
if (fixedOrder != 0) {
return fixedOrder;
}
if (!(a instanceof TestCaseEvent && b instanceof TestCaseEvent)) {
return fixedOrder;
}
return testCaseOrder.compare((TestCaseEvent) a, (TestCaseEvent) b);
}
private static final class FixedEventOrderComparator implements Comparator {
private final List> fixedOrder = asList(
TestRunStarted.class,
TestSourceRead.class,
TestSourceParsed.class,
SnippetsSuggestedEvent.class,
StepDefinedEvent.class,
TestCaseEvent.class,
TestRunFinished.class);
@Override
public int compare(final Event a, final Event b) {
return Integer.compare(requireInFixOrder(a.getClass()), requireInFixOrder(b.getClass()));
}
private int requireInFixOrder(Class o) {
int index = findInFixedOrder(o);
if (index < 0) {
throw new IllegalStateException(o + " was not in " + fixedOrder);
}
return index;
}
private int findInFixedOrder(Class o) {
for (int i = 0; i < fixedOrder.size(); i++) {
if (fixedOrder.get(i).isAssignableFrom(o)) {
return i;
}
}
return -1;
}
}
private static final class TestCaseEventComparator implements Comparator {
@Override
public int compare(TestCaseEvent a, TestCaseEvent b) {
int uri = a.getTestCase().getUri().compareTo(b.getTestCase().getUri());
if (uri != 0) {
return uri;
}
int line = Integer.compare(
a.getTestCase().getLocation().getLine(),
b.getTestCase().getLocation().getLine());
if (line != 0) {
return line;
}
return a.getInstant().compareTo(b.getInstant());
}
}
}