Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- java: include stacktrace in Convertor.toMessage(Throwable) ([#213](https://github.com/cucumber/messages/pull/213))

## [24.0.1] - 2023-12-21
### Fixed
Expand Down
8 changes: 8 additions & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<propertiesEncoding>UTF-8</propertiesEncoding>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
Expand Down
22 changes: 20 additions & 2 deletions java/src/main/java/io/cucumber/messages/Convertor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,47 @@
import io.cucumber.messages.types.Exception;
import io.cucumber.messages.types.Timestamp;

import java.io.PrintWriter;
import java.io.StringWriter;

import static java.util.Objects.requireNonNull;

public final class Convertor {

private Convertor(){

}

public static Exception toMessage(Throwable t) {
return new Exception(t.getClass().getName(), t.getMessage(), null);
public static Exception toMessage(Throwable throwable) {
requireNonNull(throwable, "throwable may not be null");
return new Exception(throwable.getClass().getName(), throwable.getMessage(), extractStackTrace(throwable));
}

private static String extractStackTrace(Throwable throwable) {
StringWriter stringWriter = new StringWriter();
try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
throwable.printStackTrace(printWriter);
}
return stringWriter.toString();
}

public static Timestamp toMessage(java.time.Instant instant) {
requireNonNull(instant, "instant may not be null");
return new Timestamp(instant.getEpochSecond(), (long) instant.getNano());
}

public static Duration toMessage(java.time.Duration duration) {
requireNonNull(duration, "duration may not be null");
return new Duration(duration.getSeconds(), (long) duration.getNano());
}

public static java.time.Instant toInstant(Timestamp timestamp) {
requireNonNull(timestamp, "timestamp may not be null");
return java.time.Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
}

public static java.time.Duration toDuration(Duration duration) {
requireNonNull(duration, "duration may not be null");
return java.time.Duration.ofSeconds(duration.getSeconds(), duration.getNanos());
}

Expand Down
15 changes: 12 additions & 3 deletions java/src/test/java/io/cucumber/messages/ConvertorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,27 @@

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

class ConvertorTest {

@Test
void convertsExceptionToMessage() {
Exception e = Convertor.toMessage(new RuntimeException());
assertAll(
() -> assertEquals(Optional.empty(), e.getMessage()),
() -> assertEquals("java.lang.RuntimeException", e.getType()),
() -> assertTrue(e.getStackTrace().isPresent())
);
}

@Test
void convertsExceptionWithMessageToMessage() {
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())
() -> assertTrue(e.getStackTrace().isPresent())
);
}

Expand Down