Skip to content

Commit b61f824

Browse files
authored
Add exception to TestStepFinished TestRunFinished (#122)
1 parent b6db6db commit b61f824

File tree

23 files changed

+518
-72
lines changed

23 files changed

+518
-72
lines changed

.gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
node_modules/
2-
.idea/
1+
node_modules
2+
.idea
3+
*.iml

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Changed
11+
- Add exception to TestStepFinished TestRunFinished ([#122](https://github.com/cucumber/messages/pull/122))
12+
1013
## [20.0.0] - 2022-11-14
1114
### Changed
12-
- BREAKING CHANGE: Add `workerId` field to TestCaseStarted message ([#34](https://github.com/cucumber/messages/pull/34))
15+
- Add `workerId` field to TestCaseStarted message ([#34](https://github.com/cucumber/messages/pull/34))
1316
- [Java] Enabled reproducible builds
1417

1518
### Fixed

go/messages.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ type Envelope struct {
3636
UndefinedParameterType *UndefinedParameterType `json:"undefinedParameterType,omitempty"`
3737
}
3838

39+
type Exception struct {
40+
Type string `json:"type"`
41+
Message string `json:"message,omitempty"`
42+
}
43+
3944
type GherkinDocument struct {
4045
Uri string `json:"uri,omitempty"`
4146
Feature *Feature `json:"feature,omitempty"`
@@ -329,6 +334,7 @@ type TestRunFinished struct {
329334
Message string `json:"message,omitempty"`
330335
Success bool `json:"success"`
331336
Timestamp *Timestamp `json:"timestamp"`
337+
Exception *Exception `json:"exception,omitempty"`
332338
}
333339

334340
type TestRunStarted struct {
@@ -343,9 +349,10 @@ type TestStepFinished struct {
343349
}
344350

345351
type TestStepResult struct {
346-
Duration *Duration `json:"duration"`
347-
Message string `json:"message,omitempty"`
348-
Status TestStepResultStatus `json:"status"`
352+
Duration *Duration `json:"duration"`
353+
Message string `json:"message,omitempty"`
354+
Status TestStepResultStatus `json:"status"`
355+
Exception *Exception `json:"exception,omitempty"`
349356
}
350357

351358
type TestStepStarted struct {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.cucumber.messages.types;
2+
3+
import java.util.ArrayList;
4+
import java.util.Objects;
5+
import java.util.Optional;
6+
7+
import static java.util.Collections.unmodifiableList;
8+
import static java.util.Objects.requireNonNull;
9+
10+
// Generated code
11+
@SuppressWarnings("unused")
12+
public final class Exception {
13+
private final String type;
14+
private final String message;
15+
16+
public Exception(
17+
String type,
18+
String message
19+
) {
20+
this.type = requireNonNull(type, "Exception.type cannot be null");
21+
this.message = message;
22+
}
23+
24+
public String getType() {
25+
return type;
26+
}
27+
28+
public Optional<String> getMessage() {
29+
return Optional.ofNullable(message);
30+
}
31+
32+
@Override
33+
public boolean equals(Object o) {
34+
if (this == o) return true;
35+
if (o == null || getClass() != o.getClass()) return false;
36+
Exception that = (Exception) o;
37+
return
38+
type.equals(that.type) &&
39+
Objects.equals(message, that.message);
40+
}
41+
42+
@Override
43+
public int hashCode() {
44+
return Objects.hash(
45+
type,
46+
message
47+
);
48+
}
49+
50+
@Override
51+
public String toString() {
52+
return "Exception{" +
53+
"type=" + type +
54+
", message=" + message +
55+
'}';
56+
}
57+
}

java/src/generated/java/io/cucumber/messages/types/TestRunFinished.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ public final class TestRunFinished {
1313
private final String message;
1414
private final Boolean success;
1515
private final Timestamp timestamp;
16+
private final Exception exception;
1617

1718
public TestRunFinished(
1819
String message,
1920
Boolean success,
20-
Timestamp timestamp
21+
Timestamp timestamp,
22+
Exception exception
2123
) {
2224
this.message = message;
2325
this.success = requireNonNull(success, "TestRunFinished.success cannot be null");
2426
this.timestamp = requireNonNull(timestamp, "TestRunFinished.timestamp cannot be null");
27+
this.exception = exception;
2528
}
2629

2730
public Optional<String> getMessage() {
@@ -36,6 +39,10 @@ public Timestamp getTimestamp() {
3639
return timestamp;
3740
}
3841

42+
public Optional<Exception> getException() {
43+
return Optional.ofNullable(exception);
44+
}
45+
3946
@Override
4047
public boolean equals(Object o) {
4148
if (this == o) return true;
@@ -44,15 +51,17 @@ public boolean equals(Object o) {
4451
return
4552
Objects.equals(message, that.message) &&
4653
success.equals(that.success) &&
47-
timestamp.equals(that.timestamp);
54+
timestamp.equals(that.timestamp) &&
55+
Objects.equals(exception, that.exception);
4856
}
4957

5058
@Override
5159
public int hashCode() {
5260
return Objects.hash(
5361
message,
5462
success,
55-
timestamp
63+
timestamp,
64+
exception
5665
);
5766
}
5867

@@ -62,6 +71,7 @@ public String toString() {
6271
"message=" + message +
6372
", success=" + success +
6473
", timestamp=" + timestamp +
74+
", exception=" + exception +
6575
'}';
6676
}
6777
}

java/src/generated/java/io/cucumber/messages/types/TestStepResult.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,18 @@ public final class TestStepResult {
1313
private final Duration duration;
1414
private final String message;
1515
private final TestStepResultStatus status;
16+
private final Exception exception;
1617

1718
public TestStepResult(
1819
Duration duration,
1920
String message,
20-
TestStepResultStatus status
21+
TestStepResultStatus status,
22+
Exception exception
2123
) {
2224
this.duration = requireNonNull(duration, "TestStepResult.duration cannot be null");
2325
this.message = message;
2426
this.status = requireNonNull(status, "TestStepResult.status cannot be null");
27+
this.exception = exception;
2528
}
2629

2730
public Duration getDuration() {
@@ -36,6 +39,10 @@ public TestStepResultStatus getStatus() {
3639
return status;
3740
}
3841

42+
public Optional<Exception> getException() {
43+
return Optional.ofNullable(exception);
44+
}
45+
3946
@Override
4047
public boolean equals(Object o) {
4148
if (this == o) return true;
@@ -44,15 +51,17 @@ public boolean equals(Object o) {
4451
return
4552
duration.equals(that.duration) &&
4653
Objects.equals(message, that.message) &&
47-
status.equals(that.status);
54+
status.equals(that.status) &&
55+
Objects.equals(exception, that.exception);
4856
}
4957

5058
@Override
5159
public int hashCode() {
5260
return Objects.hash(
5361
duration,
5462
message,
55-
status
63+
status,
64+
exception
5665
);
5766
}
5867

@@ -62,6 +71,7 @@ public String toString() {
6271
"duration=" + duration +
6372
", message=" + message +
6473
", status=" + status +
74+
", exception=" + exception +
6575
'}';
6676
}
6777
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.cucumber.messages;
2+
3+
import io.cucumber.messages.types.Duration;
4+
import io.cucumber.messages.types.Exception;
5+
import io.cucumber.messages.types.Timestamp;
6+
7+
public final class Convertor {
8+
9+
private Convertor(){
10+
11+
}
12+
13+
public static Exception toMessage(Throwable t) {
14+
return new Exception(t.getClass().getName(), t.getMessage());
15+
}
16+
17+
public static Timestamp toMessage(java.time.Instant instant) {
18+
return new Timestamp(instant.getEpochSecond(), (long) instant.getNano());
19+
}
20+
21+
public static Duration toMessage(java.time.Duration duration) {
22+
return new Duration(duration.getSeconds(), (long) duration.getNano());
23+
}
24+
25+
public static java.time.Instant toInstant(Timestamp timestamp) {
26+
return java.time.Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
27+
}
28+
29+
public static java.time.Duration toDuration(Duration duration) {
30+
return java.time.Duration.ofSeconds(duration.getSeconds(), duration.getNanos());
31+
}
32+
33+
34+
}

java/src/main/java/io/cucumber/messages/TimeConversion.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@
33
import io.cucumber.messages.types.Duration;
44
import io.cucumber.messages.types.Timestamp;
55

6+
@Deprecated
7+
@SuppressWarnings("unused")
68
public final class TimeConversion {
79

810
private TimeConversion(){
911

1012
}
1113

1214
public static Timestamp javaInstantToTimestamp(java.time.Instant instant) {
13-
return new Timestamp(instant.getEpochSecond(), (long) instant.getNano());
15+
return Convertor.toMessage(instant);
1416
}
1517

1618
public static Duration javaDurationToDuration(java.time.Duration duration) {
17-
return new Duration(duration.getSeconds(), (long) duration.getNano());
19+
return Convertor.toMessage(duration);
1820
}
1921

2022
public static java.time.Instant timestampToJavaInstant(Timestamp timestamp) {
21-
return java.time.Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
23+
return Convertor.toInstant(timestamp);
2224
}
2325

2426
public static java.time.Duration durationToJavaDuration(Duration duration) {
25-
return java.time.Duration.ofSeconds(duration.getSeconds(), duration.getNanos());
27+
return Convertor.toDuration(duration);
2628
}
2729
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.cucumber.messages;
2+
3+
import io.cucumber.messages.types.Duration;
4+
import io.cucumber.messages.types.Exception;
5+
import io.cucumber.messages.types.Timestamp;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.Optional;
9+
10+
import static org.junit.jupiter.api.Assertions.assertAll;
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
class ConvertorTest {
14+
15+
@Test
16+
void convertsExceptionToMessage() {
17+
Exception e = Convertor.toMessage(new RuntimeException("Hello world!"));
18+
Exception e2 = Convertor.toMessage(new RuntimeException());
19+
assertAll(
20+
() -> assertEquals(Optional.of("Hello world!"), e.getMessage()),
21+
() -> assertEquals(Optional.empty(), e2.getMessage()),
22+
() -> assertEquals("java.lang.RuntimeException", e.getType()),
23+
() -> assertEquals("java.lang.RuntimeException", e2.getType())
24+
);
25+
}
26+
27+
@Test
28+
void convertsToAndFromTimestamp() {
29+
java.time.Instant javaInstant = java.time.Instant.now();
30+
Timestamp timestamp = Convertor.toMessage(javaInstant);
31+
java.time.Instant javaInstantAgain = Convertor.toInstant(timestamp);
32+
33+
assertEquals(javaInstant, javaInstantAgain);
34+
}
35+
36+
@Test
37+
void convertsToAndFromDuration() {
38+
java.time.Duration javaDuration = java.time.Duration.ofSeconds(3, 161000);
39+
Duration duration = Convertor.toMessage(javaDuration);
40+
java.time.Duration javaDurationAgain = Convertor.toDuration(duration);
41+
42+
assertEquals(javaDuration, javaDurationAgain);
43+
}
44+
45+
}

java/src/test/java/io/cucumber/messages/TimeConversionTest.java

-33
This file was deleted.

0 commit comments

Comments
 (0)