Skip to content

Commit a114c89

Browse files
authored
Fix StdOut in StdIo handling of Unicode. (#822 / #823)
For a better support of Unicode the StringWriter was replaced by a ByteArrayOutputStream. fixes #822 PR: #823
1 parent 98cef28 commit a114c89

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/main/java/org/junitpioneer/jupiter/StdOutputStream.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,33 @@
1010

1111
package org.junitpioneer.jupiter;
1212

13+
import java.io.ByteArrayOutputStream;
1314
import java.io.OutputStream;
14-
import java.io.StringWriter;
15-
import java.nio.charset.Charset;
1615
import java.util.Arrays;
1716

1817
abstract class StdOutputStream extends OutputStream {
1918

20-
private final StringWriter writer = new StringWriter();
19+
private final ByteArrayOutputStream out = new ByteArrayOutputStream();
2120

2221
public StdOutputStream() {
2322
// recreate default constructor to prevent compiler warning
2423
}
2524

2625
@Override
2726
public void write(int i) {
28-
writer.write(i);
27+
out.write(i);
2928
}
3029

3130
@Override
3231
public final void write(byte[] b, int off, int len) {
33-
writer.write(new String(b, Charset.defaultCharset()), off, len);
32+
out.write(b, off, len);
3433
}
3534

3635
/**
3736
* @return the string that was written to {@code System.out} or {@code System.err}
3837
*/
3938
public String capturedString() {
40-
return writer.toString();
39+
return out.toString();
4140
}
4241

4342
/**
@@ -55,7 +54,7 @@ public String capturedString() {
5554
* @return the lines that were written to {@code System.out} or {@code System.err}
5655
*/
5756
public String[] capturedLines() {
58-
var lines = writer.toString().split(StdIoExtension.SEPARATOR, -1);
57+
var lines = out.toString().split(StdIoExtension.SEPARATOR, -1);
5958
return lines[lines.length - 1].isEmpty() ? Arrays.copyOf(lines, lines.length - 1) : lines;
6059
}
6160

src/test/java/org/junitpioneer/jupiter/StdIoExtensionTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ void catchesNothing(StdIn in, TestReporter reporter) {
240240
// ParameterResolutionException: Discovered multiple competing ParameterResolvers
241241
}
242242

243+
@Test
244+
@StdIo()
245+
@DisplayName("handles Unicode properly")
246+
void handlesUnicode(StdOut out) {
247+
String s = "■━━━".repeat(100);
248+
assertThatCode(() -> System.out.println(s)).doesNotThrowAnyException();
249+
}
250+
243251
}
244252

245253
@Nested

0 commit comments

Comments
 (0)