Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 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,15 @@

public final class TestCaseStarted extends TestCaseEvent {
public final TestCase testCase;
private final long timeStampMillis;

public TestCaseStarted(Long timeStamp, TestCase 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();
}
}
}
7 changes: 7 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,19 @@

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();
}
}
20 changes: 17 additions & 3 deletions core/src/main/java/cucumber/runtime/formatter/JSONFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import cucumber.api.event.TestStepStarted;
import cucumber.api.event.WriteEvent;
import cucumber.api.formatter.NiceAppendable;
import cucumber.util.TimeUtils;
import gherkin.ast.Background;
import gherkin.ast.Feature;
import gherkin.ast.ScenarioDefinition;
Expand Down Expand Up @@ -48,7 +49,8 @@ final class JSONFormatter implements EventListener {
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private final NiceAppendable out;
private final TestSourcesModel testSources = new TestSourcesModel();

private final TimeUtils timeUtils;

private EventHandler<TestSourceRead> testSourceReadHandler = new EventHandler<TestSourceRead>() {
@Override
public void receive(TestSourceRead event) {
Expand Down Expand Up @@ -95,6 +97,13 @@ public void receive(EmbedEvent event) {
@SuppressWarnings("WeakerAccess") // Used by PluginFactory
public JSONFormatter(Appendable out) {
this.out = new NiceAppendable(out);
this.timeUtils = new TimeUtils();
}

@SuppressWarnings("WeakerAccess") // Used by PluginFactory
JSONFormatter(Appendable out, TimeUtils timeUtils) {
this.out = new NiceAppendable(out);
this.timeUtils = timeUtils;
}

@Override
Expand All @@ -119,7 +128,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 +197,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", timeUtils.getDateTimeFromTimeStamp(event.getTimeStampMillis()));

TestCase testCase = event.getTestCase();

testCaseMap.put("name", testCase.getName());
testCaseMap.put("line", testCase.getLine());
testCaseMap.put("type", "scenario");
Expand Down
15 changes: 15 additions & 0 deletions core/src/main/java/cucumber/util/TimeUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cucumber.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class TimeUtils
{
public 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;
}
}
17 changes: 13 additions & 4 deletions core/src/test/java/cucumber/runtime/RuntimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@
import java.util.Locale;
import java.util.Map;

import cucumber.util.TimeUtils;

import static cucumber.runner.TestHelper.feature;
import static cucumber.runner.TestHelper.result;
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.anyLong;
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 All @@ -68,7 +72,10 @@ public void runs_feature_with_json_formatter() {
" When s\n");
StringBuilder out = new StringBuilder();

Plugin jsonFormatter = FormatterBuilder.jsonFormatter(out);
TimeUtils timeUtils = mock(TimeUtils.class);
when(timeUtils.getDateTimeFromTimeStamp(anyLong())).thenReturn("1970-01-01T00:00:00.Z");

Plugin jsonFormatter = FormatterBuilder.jsonFormatter(out, timeUtils);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
BackendSupplier backendSupplier = new BackendSupplier() {
@Override
Expand All @@ -90,7 +97,8 @@ public List<CucumberFeature> get() {
.build()
.run();

String expected = "[\n" +
String expected = "" +
"[\n" +
" {\n" +
" \"line\": 1,\n" +
" \"elements\": [\n" +
Expand All @@ -117,6 +125,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 +149,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,14 @@
package cucumber.runtime.formatter;

import cucumber.util.TimeUtils;

public class FormatterBuilder {

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

public static JSONFormatter jsonFormatter(Appendable out, TimeUtils timeUtils) {
return new JSONFormatter(out, timeUtils);
}
}
Loading