Skip to content

Commit

Permalink
Clean up TxValidatorTest & resource loading in other tests
Browse files Browse the repository at this point in the history
Fix inspection warnings and other minor defects (unchecked casts, bad
'assert*' usage, unused assignments/declarations, missing null checks,
object equality on enum instances, needless statement lambdas, missing
final, redundant 'toString', needless visibility) in 'TxValidatorTest'.

Also, uniformise and slightly tidy resource loading in other test
classes, replacing 'Files.readAllBytes' with Java 11 'Files.readString'
(which assumes UTF-8 as desired, instead of the default charset).
  • Loading branch information
stejbac committed Feb 2, 2024
1 parent 1e33209 commit c41903f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 93 deletions.
6 changes: 4 additions & 2 deletions core/src/test/java/bisq/core/app/BisqHelpFormatterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.io.IOException;
import java.io.PrintStream;

import java.util.Objects;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.equalTo;
Expand Down Expand Up @@ -125,11 +127,11 @@ public void testHelpFormatter() throws IOException, URISyntaxException {
.defaultsTo(AnEnum.foo);

ByteArrayOutputStream actual = new ByteArrayOutputStream();
String expected = new String(Files.readAllBytes(Paths.get(getClass().getResource("cli-output.txt").toURI())));
String expected = Files.readString(Paths.get(Objects.requireNonNull(getClass().getResource("cli-output.txt")).toURI()));
if (System.getProperty("os.name").startsWith("Windows")) {
// Load the expected content from a different file for Windows due to different path separator
// And normalize line endings to LF in case the file has CRLF line endings
expected = new String(Files.readAllBytes(Paths.get(getClass().getResource("cli-output_windows.txt").toURI())))
expected = Files.readString(Paths.get(Objects.requireNonNull(getClass().getResource("cli-output_windows.txt")).toURI()))
.replaceAll("\\r\\n?", "\n");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.util.Objects;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -52,7 +54,6 @@
import com.googlecode.jsonrpc4j.HttpException;
import com.googlecode.jsonrpc4j.JsonRpcClientException;
import com.googlecode.jsonrpc4j.RequestIDGenerator;
import kotlin.text.Charsets;

public class BitcoindClientTest {
private static final String TEST_BLOCK_HASH = "015f37a20d517645a11a6cdd316049f41bc77b4a4057b2dd092114b78147f42c";
Expand All @@ -66,7 +67,7 @@ public class BitcoindClientTest {
private boolean canConnect = true;
private ByteArrayInputStream mockResponse;
private ByteArrayInputStream mockErrorResponse;
private ByteArrayOutputStream mockOutputStream = new ByteArrayOutputStream();
private final ByteArrayOutputStream mockOutputStream = new ByteArrayOutputStream();

@BeforeEach
public void setUp() throws Exception {
Expand Down Expand Up @@ -116,7 +117,7 @@ public void testGetBlockCount_noConnection() {
}

@Test
public void testGetBlockCount_wrongCredentials() throws Exception {
public void testGetBlockCount_wrongCredentials() {
mockResponseCode = 401;
// mockResponseCustomHeaders.put("WWW-Authenticate", "[Basic realm=\"jsonrpc\"]");
assertThrows(HttpException.class, () -> client.getBlockCount());
Expand Down Expand Up @@ -218,8 +219,8 @@ private static ByteArrayInputStream toJsonIS(String json) {

private static String readFromResourcesUnPrettified(String resourceName) {
try {
var path = Paths.get(BitcoindClientTest.class.getResource(resourceName).toURI());
return new String(Files.readAllBytes(path), Charsets.UTF_8).replaceAll("(\\s+\\B|\\B\\s+|\\v)", "");
var path = Paths.get(Objects.requireNonNull(BitcoindClientTest.class.getResource(resourceName)).toURI());
return Files.readString(path).replaceAll("(\\s+\\B|\\B\\s+|\\v)", "");
} catch (Exception e) {
return "";
}
Expand Down
Loading

0 comments on commit c41903f

Please sign in to comment.