Skip to content

Commit

Permalink
java: include stacktrace in Convertor.toMessage(Throwable)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkorstanje committed Mar 23, 2024
1 parent c64e85e commit 9934a96
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
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
22 changes: 19 additions & 3 deletions java/src/test/java/io/cucumber/messages/ConvertorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,40 @@
import io.cucumber.messages.types.Duration;
import io.cucumber.messages.types.Exception;
import io.cucumber.messages.types.Timestamp;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
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());
assertAll(
() -> assertEquals(Optional.empty(), e.getMessage()),
() -> assertEquals("java.lang.RuntimeException", e.getType()),
() -> assertThat(e.getStackTrace().get(), startsWith("" +
"java.lang.RuntimeException\n" +
"\tat io.cucumber.messages.ConvertorTest.convertsExceptionToMessage(ConvertorTest.java:"))
);
}

@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())
() -> assertThat(e.getStackTrace().get(), startsWith("" +
"java.lang.RuntimeException: Hello world!\n" +
"\tat io.cucumber.messages.ConvertorTest.convertsExceptionWithMessageToMessage(ConvertorTest.java:"))
);
}

Expand Down

0 comments on commit 9934a96

Please sign in to comment.