Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion core/src/main/java/cucumber/api/event/TestCaseStarted.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@

public final class TestCaseStarted extends TestCaseEvent {
public final TestCase testCase;

private final long timeStampMillis;

@Deprecated
public TestCaseStarted(Long timeStamp, TestCase testCase) {
this(timeStamp, 0L, testCase);
}

public TestCaseStarted(Long timeStamp, Long timeStampMillis, TestCase testCase) {
super(timeStamp, testCase);
this.testCase = testCase;
this.timeStampMillis = timeStampMillis;
}

public long getTimeStampMillis() {
return timeStampMillis;
}
}
2 changes: 2 additions & 0 deletions core/src/main/java/cucumber/runner/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
public interface EventBus extends EventPublisher {

Long getTime();

Long getTimeStampMillis();

void send(Event event);

Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/cucumber/runner/TestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public TestCase(List<PickleStepTestStep> testSteps,
void run(EventBus bus) {
boolean skipNextStep = this.dryRun;
Long startTime = bus.getTime();
bus.send(new TestCaseStarted(startTime, this));
Long startTimeStampMillis = bus.getTimeStampMillis();
bus.send(new TestCaseStarted(startTime, startTimeStampMillis, this));
Scenario scenario = new Scenario(bus, this);

for (HookTestStep before : beforeHooks) {
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/cucumber/runner/ThreadLocalRunnerSupplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public void send(final Event event) {
super.send(event);
parent.send(event);
}

@Override
public Long getTimeStampMillis() {
return parent.getTimeStampMillis();
}
}

private static final class SynchronizedEventBus implements EventBus {
Expand Down Expand Up @@ -102,5 +107,10 @@ public synchronized <T extends Event> void registerHandlerFor(Class<T> eventType
public synchronized <T extends Event> void removeHandlerFor(Class<T> eventType, EventHandler<T> handler) {
delegate.removeHandlerFor(eventType, handler);
}

@Override
public Long getTimeStampMillis() {
return delegate.getTimeStampMillis();
}
}
}
6 changes: 6 additions & 0 deletions core/src/main/java/cucumber/runner/TimeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

public interface TimeService {
long time();
long timeStampMillis();

TimeService SYSTEM = new TimeService() {
@Override
public long time() {
return System.nanoTime();
}

@Override
public long timeStampMillis() {
return System.currentTimeMillis();
}
};

}
5 changes: 5 additions & 0 deletions core/src/main/java/cucumber/runner/TimeServiceEventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ public TimeServiceEventBus(TimeService stopWatch) {
public Long getTime() {
return stopWatch.time();
}

@Override
public Long getTimeStampMillis() {
return stopWatch.timeStampMillis();
}
}
23 changes: 19 additions & 4 deletions core/src/main/java/cucumber/runtime/formatter/JSONFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
import gherkin.pickles.PickleTable;
import gherkin.pickles.PickleTag;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

final class JSONFormatter implements EventListener {
private String currentFeatureFile;
Expand All @@ -48,7 +51,7 @@ final class JSONFormatter implements EventListener {
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private final NiceAppendable out;
private final TestSourcesModel testSources = new TestSourcesModel();

private EventHandler<TestSourceRead> testSourceReadHandler = new EventHandler<TestSourceRead>() {
@Override
public void receive(TestSourceRead event) {
Expand Down Expand Up @@ -96,7 +99,7 @@ public void receive(EmbedEvent event) {
public JSONFormatter(Appendable out) {
this.out = new NiceAppendable(out);
}

@Override
public void setEventPublisher(EventPublisher publisher) {
publisher.registerHandlerFor(TestSourceRead.class, testSourceReadHandler);
Expand All @@ -119,7 +122,7 @@ private void handleTestCaseStarted(TestCaseStarted event) {
featureMaps.add(currentFeatureMap);
currentElementsList = (List<Map<String, Object>>) currentFeatureMap.get("elements");
}
currentTestCaseMap = createTestCase(event.testCase);
currentTestCaseMap = createTestCase(event);
if (testSources.hasBackground(currentFeatureFile, event.testCase.getLine())) {
currentElementMap = createBackground(event.testCase);
currentElementsList.add(currentElementMap);
Expand Down Expand Up @@ -188,8 +191,13 @@ private Map<String, Object> createFeatureMap(TestCase testCase) {
return featureMap;
}

private Map<String, Object> createTestCase(TestCase testCase) {
private Map<String, Object> createTestCase(TestCaseStarted event) {
Map<String, Object> testCaseMap = new HashMap<String, Object>();

testCaseMap.put("start_timestamp", getDateTimeFromTimeStamp(event.getTimeStampMillis()));

TestCase testCase = event.getTestCase();

testCaseMap.put("name", testCase.getName());
testCaseMap.put("line", testCase.getLine());
testCaseMap.put("type", "scenario");
Expand Down Expand Up @@ -379,4 +387,11 @@ private Map<String, Object> createResultMap(Result result) {
}
return resultMap;
}

private String getDateTimeFromTimeStamp(long timeStampMillis) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.XXX");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

return sdf.format(new Date(timeStampMillis));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ public class CanonicalEventOrderTest {
static long getTime() {
return new Date().getTime();
}

private static long getTimeStampMillis() {
return System.currentTimeMillis();
}

static Event createTestCaseEvent(final String uri, final int line) {
final TestCase testCase = mock(TestCase.class);
given(testCase.getUri()).willReturn(uri);
given(testCase.getLine()).willReturn(line);
return new TestCaseStarted(getTime(), testCase);
return new TestCaseStarted(getTime(), getTimeStampMillis(), testCase);
}

private Event runStarted = new TestRunStarted(getTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ public long time() {
Long result = currentTime.get();
return result != null ? result : 0l;
}

@Override
public long timeStampMillis() {
Long result = currentTime.get();
return result != null ? result : 0l;
}

private void handleTestStepStarted(TestStepStarted event) {
long time = time();
currentTime.set(time + stepDuration);
}

}
5 changes: 5 additions & 0 deletions core/src/test/java/cucumber/runner/TimeServiceStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public long time() {
currentTime.set(result + duration);
return result;
}

@Override
public long timeStampMillis() {
return 0L;
}
}
11 changes: 8 additions & 3 deletions core/src/test/java/cucumber/runtime/RuntimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import cucumber.runner.TestHelper;
import cucumber.runner.TimeService;
import cucumber.runner.TimeServiceEventBus;
import cucumber.runner.TimeServiceStub;
import cucumber.runtime.formatter.FormatterBuilder;
import cucumber.runtime.formatter.FormatterSpy;
import cucumber.runtime.io.ClasspathResourceLoader;
Expand Down Expand Up @@ -42,12 +43,13 @@
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs;

public class RuntimeTest {
private final static long ANY_TIMESTAMP = 1234567890;
Expand Down Expand Up @@ -86,11 +88,13 @@ public List<CucumberFeature> get() {
.withBackendSupplier(backendSupplier)
.withAdditionalPlugins(jsonFormatter)
.withResourceLoader(new ClasspathResourceLoader(classLoader))
.withEventBus(new TimeServiceEventBus(new TimeServiceStub(0)))
.withFeatureSupplier(featureSupplier)
.build()
.run();

String expected = "[\n" +
String expected = "" +
"[\n" +
" {\n" +
" \"line\": 1,\n" +
" \"elements\": [\n" +
Expand All @@ -117,6 +121,7 @@ public List<CucumberFeature> get() {
" \"name\": \"scenario name\",\n" +
" \"description\": \"\",\n" +
" \"id\": \"feature-name;scenario-name\",\n" +
" \"start_timestamp\": \"1970-01-01T00:00:00.Z\",\n" +
" \"type\": \"scenario\",\n" +
" \"keyword\": \"Scenario\",\n" +
" \"steps\": [\n" +
Expand All @@ -140,7 +145,7 @@ public List<CucumberFeature> get() {
" \"tags\": []\n" +
" }\n" +
"]";
assertEquals(expected, out.toString());
assertThat(out.toString(), sameJSONAs(expected));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ public void receive(TestCaseStarted event) {
fail();
}
});
eventBus.send(new TestCaseStarted(0L, null));
eventBus.send(new TestCaseStarted(0L, 0L, null));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cucumber.runtime.formatter;


public class FormatterBuilder {

public static JSONFormatter jsonFormatter(Appendable out) {
return new JSONFormatter(out);
}

}
Loading