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

Add exception to TestStepFinished TestRunFinished #122

Merged
merged 9 commits into from
Dec 16, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
.idea/
node_modules
.idea
*.iml
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed
- Add exception to TestStepFinished TestRunFinished ([#122](https://github.com/cucumber/messages/pull/122))

## [20.0.0] - 2022-11-14
### Changed
- BREAKING CHANGE: Add `workerId` field to TestCaseStarted message ([#34](https://github.com/cucumber/messages/pull/34))
- Add `workerId` field to TestCaseStarted message ([#34](https://github.com/cucumber/messages/pull/34))
- [Java] Enabled reproducible builds

### Fixed
Expand Down
13 changes: 10 additions & 3 deletions go/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ type Envelope struct {
UndefinedParameterType *UndefinedParameterType `json:"undefinedParameterType,omitempty"`
}

type Exception struct {
Type string `json:"type"`
Message string `json:"message,omitempty"`
}

type GherkinDocument struct {
Uri string `json:"uri,omitempty"`
Feature *Feature `json:"feature,omitempty"`
Expand Down Expand Up @@ -329,6 +334,7 @@ type TestRunFinished struct {
Message string `json:"message,omitempty"`
Success bool `json:"success"`
Timestamp *Timestamp `json:"timestamp"`
Exception *Exception `json:"exception,omitempty"`
}

type TestRunStarted struct {
Expand All @@ -343,9 +349,10 @@ type TestStepFinished struct {
}

type TestStepResult struct {
Duration *Duration `json:"duration"`
Message string `json:"message,omitempty"`
Status TestStepResultStatus `json:"status"`
Duration *Duration `json:"duration"`
Message string `json:"message,omitempty"`
Status TestStepResultStatus `json:"status"`
Exception *Exception `json:"exception,omitempty"`
}

type TestStepStarted struct {
Expand Down
57 changes: 57 additions & 0 deletions java/src/generated/java/io/cucumber/messages/types/Exception.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.cucumber.messages.types;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Optional;

import static java.util.Collections.unmodifiableList;
import static java.util.Objects.requireNonNull;

// Generated code
@SuppressWarnings("unused")
public final class Exception {
private final String type;
private final String message;

public Exception(
String type,
String message
) {
this.type = requireNonNull(type, "Exception.type cannot be null");
this.message = message;
}

public String getType() {
return type;
}

public Optional<String> getMessage() {
return Optional.ofNullable(message);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Exception that = (Exception) o;
return
type.equals(that.type) &&
Objects.equals(message, that.message);
}

@Override
public int hashCode() {
return Objects.hash(
type,
message
);
}

@Override
public String toString() {
return "Exception{" +
"type=" + type +
", message=" + message +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ public final class TestRunFinished {
private final String message;
private final Boolean success;
private final Timestamp timestamp;
private final Exception exception;

public TestRunFinished(
String message,
Boolean success,
Timestamp timestamp
Timestamp timestamp,
Exception exception
) {
this.message = message;
this.success = requireNonNull(success, "TestRunFinished.success cannot be null");
this.timestamp = requireNonNull(timestamp, "TestRunFinished.timestamp cannot be null");
this.exception = exception;
}

public Optional<String> getMessage() {
Expand All @@ -36,6 +39,10 @@ public Timestamp getTimestamp() {
return timestamp;
}

public Optional<Exception> getException() {
return Optional.ofNullable(exception);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -44,15 +51,17 @@ public boolean equals(Object o) {
return
Objects.equals(message, that.message) &&
success.equals(that.success) &&
timestamp.equals(that.timestamp);
timestamp.equals(that.timestamp) &&
Objects.equals(exception, that.exception);
}

@Override
public int hashCode() {
return Objects.hash(
message,
success,
timestamp
timestamp,
exception
);
}

Expand All @@ -62,6 +71,7 @@ public String toString() {
"message=" + message +
", success=" + success +
", timestamp=" + timestamp +
", exception=" + exception +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ public final class TestStepResult {
private final Duration duration;
private final String message;
private final TestStepResultStatus status;
private final Exception exception;

public TestStepResult(
Duration duration,
String message,
TestStepResultStatus status
TestStepResultStatus status,
Exception exception
) {
this.duration = requireNonNull(duration, "TestStepResult.duration cannot be null");
this.message = message;
this.status = requireNonNull(status, "TestStepResult.status cannot be null");
this.exception = exception;
}

public Duration getDuration() {
Expand All @@ -36,6 +39,10 @@ public TestStepResultStatus getStatus() {
return status;
}

public Optional<Exception> getException() {
return Optional.ofNullable(exception);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -44,15 +51,17 @@ public boolean equals(Object o) {
return
duration.equals(that.duration) &&
Objects.equals(message, that.message) &&
status.equals(that.status);
status.equals(that.status) &&
Objects.equals(exception, that.exception);
}

@Override
public int hashCode() {
return Objects.hash(
duration,
message,
status
status,
exception
);
}

Expand All @@ -62,6 +71,7 @@ public String toString() {
"duration=" + duration +
", message=" + message +
", status=" + status +
", exception=" + exception +
'}';
}
}
34 changes: 34 additions & 0 deletions java/src/main/java/io/cucumber/messages/Convertor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.cucumber.messages;

import io.cucumber.messages.types.Duration;
import io.cucumber.messages.types.Exception;
import io.cucumber.messages.types.Timestamp;

public final class Convertor {

private Convertor(){

}

public static Exception toMessage(Throwable t) {
return new Exception(t.getClass().getName(), t.getMessage());
}

public static Timestamp toMessage(java.time.Instant instant) {
return new Timestamp(instant.getEpochSecond(), (long) instant.getNano());
}

public static Duration toMessage(java.time.Duration duration) {
return new Duration(duration.getSeconds(), (long) duration.getNano());
}

public static java.time.Instant toInstant(Timestamp timestamp) {
return java.time.Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
}

public static java.time.Duration toDuration(Duration duration) {
return java.time.Duration.ofSeconds(duration.getSeconds(), duration.getNanos());
}


}
10 changes: 6 additions & 4 deletions java/src/main/java/io/cucumber/messages/TimeConversion.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@
import io.cucumber.messages.types.Duration;
import io.cucumber.messages.types.Timestamp;

@Deprecated
@SuppressWarnings("unused")
public final class TimeConversion {

private TimeConversion(){

}

public static Timestamp javaInstantToTimestamp(java.time.Instant instant) {
return new Timestamp(instant.getEpochSecond(), (long) instant.getNano());
return Convertor.toMessage(instant);
}

public static Duration javaDurationToDuration(java.time.Duration duration) {
return new Duration(duration.getSeconds(), (long) duration.getNano());
return Convertor.toMessage(duration);
}

public static java.time.Instant timestampToJavaInstant(Timestamp timestamp) {
return java.time.Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
return Convertor.toInstant(timestamp);
}

public static java.time.Duration durationToJavaDuration(Duration duration) {
return java.time.Duration.ofSeconds(duration.getSeconds(), duration.getNanos());
return Convertor.toDuration(duration);
}
}
45 changes: 45 additions & 0 deletions java/src/test/java/io/cucumber/messages/ConvertorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.cucumber.messages;

import io.cucumber.messages.types.Duration;
import io.cucumber.messages.types.Exception;
import io.cucumber.messages.types.Timestamp;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;

class ConvertorTest {

@Test
void convertsExceptionToMessage() {
Exception e = Convertor.toMessage(new RuntimeException("Hello world!"));
Exception e2 = Convertor.toMessage(new RuntimeException());
assertAll(
() -> assertEquals(Optional.of("Hello world!"), e.getMessage()),
() -> assertEquals(Optional.empty(), e2.getMessage()),
() -> assertEquals("java.lang.RuntimeException", e.getType()),
() -> assertEquals("java.lang.RuntimeException", e2.getType())
);
}

@Test
void convertsToAndFromTimestamp() {
java.time.Instant javaInstant = java.time.Instant.now();
Timestamp timestamp = Convertor.toMessage(javaInstant);
java.time.Instant javaInstantAgain = Convertor.toInstant(timestamp);

assertEquals(javaInstant, javaInstantAgain);
}

@Test
void convertsToAndFromDuration() {
java.time.Duration javaDuration = java.time.Duration.ofSeconds(3, 161000);
Duration duration = Convertor.toMessage(javaDuration);
java.time.Duration javaDurationAgain = Convertor.toDuration(duration);

assertEquals(javaDuration, javaDurationAgain);
}

}
33 changes: 0 additions & 33 deletions java/src/test/java/io/cucumber/messages/TimeConversionTest.java

This file was deleted.

Loading