Skip to content

Commit

Permalink
Fix sonar findings
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Jul 7, 2024
1 parent bdb1e98 commit c25de4c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/itsallcode/io/CapturingOutputStream.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.itsallcode.io;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -101,7 +102,7 @@ public String getCapturedData()
}
else
{
return this.internalStream.toString();
return this.internalStream.toString(StandardCharsets.UTF_8);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit c25de4c

Please sign in to comment.