diff --git a/README.md b/README.md index 13653e7..22ec708 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,14 @@ The `ExitGuard` temporarily replaces the existing security manager. From version 1.2.0 on if a security guard existed before, it serves as a delegate for all security checks with the exception of the `checkExit`. -**Warning:** The JREs Security Manager used by `ExitGuard` is deprecated and is not supported by Java 21 and later. +**Warning:** The JREs Security Manager used by `ExitGuard` is deprecated and is not supported by Java 21 and later. It still works with Java 17 but logs the following warning: + +``` +WARNING: A terminally deprecated method in java.lang.System has been called +WARNING: System::setSecurityManager has been called by ... +WARNING: Please consider reporting this to the maintainers of ... +WARNING: System::setSecurityManager will be removed in a future release +``` ## Asserting Data Sent to `System.out` diff --git a/src/main/java/org/itsallcode/io/CapturingOutputStream.java b/src/main/java/org/itsallcode/io/CapturingOutputStream.java index b31d34f..f9cd728 100644 --- a/src/main/java/org/itsallcode/io/CapturingOutputStream.java +++ b/src/main/java/org/itsallcode/io/CapturingOutputStream.java @@ -1,6 +1,7 @@ package org.itsallcode.io; import java.io.*; +import java.nio.charset.StandardCharsets; import java.util.Objects; /** @@ -9,9 +10,9 @@ */ public class CapturingOutputStream extends OutputStream implements Capturable { - private OutputStream targetStream = null; - private ByteArrayOutputStream internalStream = null; - private String captureBuffer = null; + private OutputStream targetStream; + private ByteArrayOutputStream internalStream; + private String captureBuffer; private boolean forwardOutputToTarget = true; /** @@ -69,7 +70,7 @@ public synchronized void close() throws IOException { if (this.internalStream != null) { - this.captureBuffer = this.internalStream.toString(); + this.captureBuffer = this.internalStream.toString(StandardCharsets.UTF_8); this.internalStream.close(); } this.internalStream = null; @@ -101,7 +102,7 @@ public String getCapturedData() } else { - return this.internalStream.toString(); + return this.internalStream.toString(StandardCharsets.UTF_8); } } diff --git a/src/main/java/org/itsallcode/junit/sysextensions/AbstractSystemOutputGuard.java b/src/main/java/org/itsallcode/junit/sysextensions/AbstractSystemOutputGuard.java index 41e8d9a..5dbf1fe 100644 --- a/src/main/java/org/itsallcode/junit/sysextensions/AbstractSystemOutputGuard.java +++ b/src/main/java/org/itsallcode/junit/sysextensions/AbstractSystemOutputGuard.java @@ -4,6 +4,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Executable; import java.lang.reflect.Parameter; +import java.nio.charset.StandardCharsets; import org.itsallcode.io.Capturable; import org.itsallcode.io.CapturingOutputStream; @@ -67,7 +68,8 @@ private void replaceSystemStreamWithCapturingStream(final ExtensionContext conte { final CapturingOutputStream capturingStream = new CapturingOutputStream(getSystemStream()); context.getStore(getNamespace()).put(CAPTURING_OUTPUT_STREAM_KEY, capturingStream); - final PrintStream printStream = new PrintStream(getCapturingOutputStream(context)); + final PrintStream printStream = new PrintStream(getCapturingOutputStream(context), false, + StandardCharsets.UTF_8); setSystemStream(printStream); } diff --git a/src/test/java/org/itsallcode/junit/sysextensions/TestSytemOutWithBuffer.java b/src/test/java/org/itsallcode/junit/sysextensions/TestSystemOutWithBuffer.java similarity index 85% rename from src/test/java/org/itsallcode/junit/sysextensions/TestSytemOutWithBuffer.java rename to src/test/java/org/itsallcode/junit/sysextensions/TestSystemOutWithBuffer.java index 6dae24c..49abe53 100644 --- a/src/test/java/org/itsallcode/junit/sysextensions/TestSytemOutWithBuffer.java +++ b/src/test/java/org/itsallcode/junit/sysextensions/TestSystemOutWithBuffer.java @@ -6,18 +6,12 @@ import java.io.PrintStream; import org.itsallcode.io.Capturable; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @ExtendWith(SystemOutGuard.class) -class TestSytemOutWithBuffer +class TestSystemOutWithBuffer { - @BeforeEach - void beforeEach() - { - } - @SuppressWarnings("squid:S2699") @Test void testGetCapturedRegular(final Capturable stream) throws IOException @@ -27,7 +21,7 @@ void testGetCapturedRegular(final Capturable stream) throws IOException } @Test - void testGetCapturedDataWithPrintStreamAroundSystemOut(final Capturable stream) throws IOException + void testGetCapturedDataWithPrintStreamAroundSystemOut(final Capturable stream) { final PrintStream printStream = new PrintStream(System.out); stream.capture();