Skip to content

Commit

Permalink
#592 implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jan 29, 2018
1 parent c235a77 commit 288d112
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/main/java/org/cactoos/io/BytesOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,52 @@ public BytesOf(final Throwable error, final CharSequence charset) {
);
}

/**
* Ctor.
* @param strace The stack trace
* @since 0.29
*/
public BytesOf(final StackTraceElement... strace) {
this(strace, StandardCharsets.UTF_8);
}

/**
* Ctor.
* @param strace The stack trace
* @param charset Charset
* @since 0.29
*/
public BytesOf(final StackTraceElement[] strace, final Charset charset) {
this(strace, charset.name());
}

/**
* Ctor.
* @param strace The stack trace
* @param charset Charset
* @since 0.29
*/
public BytesOf(final StackTraceElement[] strace,
final CharSequence charset) {
this(
() -> {
try (
final ByteArrayOutputStream baos =
new ByteArrayOutputStream();
final PrintStream stream = new PrintStream(
baos, true, charset.toString()
)
) {
for (final StackTraceElement element : strace) {
stream.append(element.toString());
stream.append("\n");
}
return baos.toByteArray();
}
}
);
}

/**
* Ctor.
*
Expand Down
51 changes: 50 additions & 1 deletion src/main/java/org/cactoos/text/TextOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,62 @@ public TextOf(final char[] chars, final Charset cset) {

/**
* Ctor.
*
* @param error The exception to serialize
*/
public TextOf(final Throwable error) {
this(new BytesOf(error));
}

/**
* Ctor.
* @param error The exception to serialize
* @param charset Charset
* @since 0.29
*/
public TextOf(final Throwable error, final Charset charset) {
this(new BytesOf(error, charset));
}

/**
* Ctor.
* @param error The exception to serialize
* @param charset Charset
* @since 0.29
*/
public TextOf(final Throwable error, final CharSequence charset) {
this(new BytesOf(error, charset));
}

/**
* Ctor.
* @param strace The stacktrace to serialize
* @since 0.29
*/
public TextOf(final StackTraceElement... strace) {
this(new BytesOf(strace));
}

/**
* Ctor.
* @param strace The stacktrace to serialize
* @param charset Charset
* @since 0.29
*/
public TextOf(final StackTraceElement[] strace, final Charset charset) {
this(new BytesOf(strace, charset));
}

/**
* Ctor.
* @param strace The stacktrace to serialize
* @param charset Charset
* @since 0.29
*/
public TextOf(final StackTraceElement[] strace,
final CharSequence charset) {
this(new BytesOf(strace, charset));
}

/**
* Ctor.
*
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/cactoos/io/BytesOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,19 @@ public void printsStackTrace() {
);
}

@Test
public void printsStackTraceFromArray() {
MatcherAssert.assertThat(
"Can't print exception stacktrace from array",
new TextOf(
new BytesOf(
new IOException("").getStackTrace()
)
),
new TextHasString(
Matchers.containsString("org.cactoos.io.BytesOfTest")
)
);
}

}
13 changes: 13 additions & 0 deletions src/test/java/org/cactoos/text/TextOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,17 @@ public void readsEmptyInputStream() throws Exception {
Matchers.equalTo(content)
);
}

@Test
public void printsStackTraceFromArray() {
MatcherAssert.assertThat(
"Can't print exception stacktrace from array",
new TextOf(
new IOException("").getStackTrace()
),
new TextHasString(
Matchers.containsString("org.cactoos.text.TextOfTest")
)
);
}
}

0 comments on commit 288d112

Please sign in to comment.