Skip to content

Commit

Permalink
Close leaking streams using try-with-resources in tests -2 [HZ-3391] (h…
Browse files Browse the repository at this point in the history
  • Loading branch information
orcunc authored Oct 9, 2023
1 parent 19f5451 commit 2688200
Show file tree
Hide file tree
Showing 47 changed files with 536 additions and 576 deletions.
11 changes: 7 additions & 4 deletions hazelcast/src/main/java/com/hazelcast/gcp/GcpAuthenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
import com.hazelcast.internal.json.JsonObject;
import com.hazelcast.spi.utils.RestClient;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
Expand Down Expand Up @@ -73,8 +74,10 @@ String refreshAccessToken(String privateKeyPath, long currentTimeMs) {

private String createBody(String privateKeyPath, long currentTimeMs)
throws Exception {
JsonObject privateKeyJson = Json.parse(
new InputStreamReader(new FileInputStream(privateKeyPath), StandardCharsets.UTF_8)).asObject();
JsonObject privateKeyJson;
try (Reader reader = Files.newBufferedReader(Paths.get(privateKeyPath))) {
privateKeyJson = Json.parse(reader).asObject();
}
String privateKey = privateKeyJson.get("private_key").asString();
String clientEmail = privateKeyJson.get("client_email").asString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@

package com.hazelcast.client.protocol;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.reflections.Reflections;

import com.hazelcast.client.impl.protocol.task.AbstractMessageTask;
import com.hazelcast.client.impl.protocol.task.AddBackupListenerMessageTask;
import com.hazelcast.client.impl.protocol.task.AddClusterViewListenerMessageTask;
Expand Down Expand Up @@ -70,15 +54,27 @@
import com.hazelcast.sql.impl.client.SqlFetchMessageTask;
import com.hazelcast.test.HazelcastParallelClassRunner;
import com.hazelcast.test.annotation.QuickTest;

import javassist.ClassPool;
import javassist.NotFoundException;
import javassist.bytecode.BadBytecode;
import javassist.bytecode.ClassFile;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.CodeIterator;
import javassist.bytecode.MethodInfo;
import javassist.bytecode.Mnemonic;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.reflections.Reflections;

import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

/**
* Verifies the {@code getRequiredPermission()} method doesn't simply return null in client {@link MessageTask} instances.
Expand Down Expand Up @@ -152,7 +148,7 @@ private void assertGetRequiredPermission(String clsname) throws Exception {
assertFalse(clsname + " returns null in getRequiredPermission()", returnsNull);
}

private boolean doesGetRequiredPermissionSimpleReturnNull(String clsname) throws NotFoundException, Exception, BadBytecode {
private boolean doesGetRequiredPermissionSimpleReturnNull(String clsname) throws Exception {
if (clsname == null) {
fail("Class with getRequiredPermission() method implementation not found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand All @@ -53,15 +51,15 @@ public class ClientCompatibilityNullTest_2_0 {

@Before
public void setUp() throws IOException {
File file = new File(getClass().getResource("/2.0.protocol.compatibility.null.binary").getFile());
InputStream inputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
inputStream.read(data);
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
try (InputStream inputStream = getClass().getResourceAsStream("/2.0.protocol.compatibility.null.binary")) {
assert inputStream != null;
byte[] data = inputStream.readAllBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand All @@ -53,15 +51,15 @@ public class ClientCompatibilityNullTest_2_0_1 {

@Before
public void setUp() throws IOException {
File file = new File(getClass().getResource("/2.0.1.protocol.compatibility.null.binary").getFile());
InputStream inputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
inputStream.read(data);
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
try (InputStream inputStream = getClass().getResourceAsStream("/2.0.1.protocol.compatibility.null.binary")) {
assert inputStream != null;
byte[] data = inputStream.readAllBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand All @@ -53,15 +51,15 @@ public class ClientCompatibilityNullTest_2_1 {

@Before
public void setUp() throws IOException {
File file = new File(getClass().getResource("/2.1.protocol.compatibility.null.binary").getFile());
InputStream inputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
inputStream.read(data);
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
try (InputStream inputStream = getClass().getResourceAsStream("/2.1.protocol.compatibility.null.binary")) {
assert inputStream != null;
byte[] data = inputStream.readAllBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand All @@ -53,15 +51,15 @@ public class ClientCompatibilityNullTest_2_2 {

@Before
public void setUp() throws IOException {
File file = new File(getClass().getResource("/2.2.protocol.compatibility.null.binary").getFile());
InputStream inputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
inputStream.read(data);
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
try (InputStream inputStream = getClass().getResourceAsStream("/2.2.protocol.compatibility.null.binary")) {
assert inputStream != null;
byte[] data = inputStream.readAllBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand All @@ -53,15 +51,15 @@ public class ClientCompatibilityNullTest_2_3 {

@Before
public void setUp() throws IOException {
File file = new File(getClass().getResource("/2.3.protocol.compatibility.null.binary").getFile());
InputStream inputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
inputStream.read(data);
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
try (InputStream inputStream = getClass().getResourceAsStream("/2.3.protocol.compatibility.null.binary")) {
assert inputStream != null;
byte[] data = inputStream.readAllBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand All @@ -53,15 +51,15 @@ public class ClientCompatibilityNullTest_2_4 {

@Before
public void setUp() throws IOException {
File file = new File(getClass().getResource("/2.4.protocol.compatibility.null.binary").getFile());
InputStream inputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
inputStream.read(data);
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
try (InputStream inputStream = getClass().getResourceAsStream("/2.4.protocol.compatibility.null.binary")) {
assert inputStream != null;
byte[] data = inputStream.readAllBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand All @@ -53,15 +51,15 @@ public class ClientCompatibilityNullTest_2_5 {

@Before
public void setUp() throws IOException {
File file = new File(getClass().getResource("/2.5.protocol.compatibility.null.binary").getFile());
InputStream inputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
inputStream.read(data);
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
try (InputStream inputStream = getClass().getResourceAsStream("/2.5.protocol.compatibility.null.binary")) {
assert inputStream != null;
byte[] data = inputStream.readAllBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand All @@ -53,15 +51,15 @@ public class ClientCompatibilityNullTest_2_6 {

@Before
public void setUp() throws IOException {
File file = new File(getClass().getResource("/2.6.protocol.compatibility.null.binary").getFile());
InputStream inputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
inputStream.read(data);
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
try (InputStream inputStream = getClass().getResourceAsStream("/2.6.protocol.compatibility.null.binary")) {
assert inputStream != null;
byte[] data = inputStream.readAllBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand All @@ -53,15 +51,15 @@ public class ClientCompatibilityNullTest_2_7 {

@Before
public void setUp() throws IOException {
File file = new File(getClass().getResource("/2.7.protocol.compatibility.null.binary").getFile());
InputStream inputStream = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
inputStream.read(data);
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
try (InputStream inputStream = getClass().getResourceAsStream("/2.7.protocol.compatibility.null.binary")) {
assert inputStream != null;
byte[] data = inputStream.readAllBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
ClientMessageReader reader = new ClientMessageReader(0);
while (reader.readFrom(buffer, true)) {
clientMessages.add(reader.getClientMessage());
reader.reset();
}
}
}

Expand Down
Loading

0 comments on commit 2688200

Please sign in to comment.