Skip to content

Commit fbe79ff

Browse files
acogoluegnesansd
authored andcommitted
Small cleaning up in MQTT Java tests
1 parent f2206c8 commit fbe79ff

File tree

5 files changed

+20
-31
lines changed

5 files changed

+20
-31
lines changed

deps/rabbitmq_mqtt/test/java_SUITE_data/src/test/java/com/rabbitmq/mqtt/test/MqttTest.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ public class MqttTest implements MqttCallback {
5252
private final byte[] payload = "payload".getBytes();
5353
private final String topic = "test-topic";
5454
private final String retainedTopic = "test-retained-topic";
55-
private int testDelay = 2000;
5655

57-
private volatile long lastReceipt;
5856
private volatile boolean expectConnectionFailure;
5957
private volatile boolean failOnDelivery = false;
6058

@@ -80,14 +78,12 @@ private static int getAmqpPort() {
8078
}
8179

8280
// override the 10s limit
83-
private class TestMqttConnectOptions extends MqttConnectOptions {
81+
private static class TestMqttConnectOptions extends MqttConnectOptions {
8482
private int keepAliveInterval = 60;
85-
private final String user_name = "guest";
86-
private final String password = "guest";
8783

8884
public TestMqttConnectOptions() {
89-
super.setUserName(user_name);
90-
super.setPassword(password.toCharArray());
85+
super.setUserName("guest");
86+
super.setPassword("guest".toCharArray());
9187
super.setCleanSession(true);
9288
super.setKeepAliveInterval(60);
9389
// PublishMultiple overwhelms Paho defaults
@@ -214,8 +210,7 @@ public void connectFirst() throws MqttException, IOException {
214210

215211
// ---8<---
216212
// Copy/pasted from readMqttWireMessage() in MqttInputStream.java.
217-
ByteArrayOutputStream bais = new ByteArrayOutputStream();
218-
byte first = in.readByte();
213+
in.readByte();
219214
// ---8<---
220215

221216
fail("Error expected if CONNECT is not first packet");
@@ -301,8 +296,7 @@ public void emptyPassword(TestInfo info) throws MqttException {
301296
MqttConnectOptions client_opts = options();
302297
client_opts.setPassword("".toCharArray());
303298

304-
MqttClient client = newClient(info);
305-
try {
299+
try (MqttClient client = newClient(info)) {
306300
client.connect(client_opts);
307301
fail("Authentication failure expected");
308302
} catch (MqttException ex) {
@@ -1047,7 +1041,6 @@ public void connectionLost(Throwable cause) {
10471041
}
10481042

10491043
public void messageArrived(String topic, MqttMessage message) throws Exception {
1050-
lastReceipt = System.currentTimeMillis();
10511044
receivedMessages.add(message);
10521045
if (failOnDelivery) {
10531046
throw new Exception("unexpected delivery on topic " + topic);
@@ -1062,6 +1055,7 @@ private Integer receivedMessagesSize() {
10621055

10631056
private void waitForTestDelay() {
10641057
try {
1058+
int testDelay = 2000;
10651059
Thread.sleep(testDelay);
10661060
} catch (InterruptedException e) {
10671061
throw new RuntimeException(e);

deps/rabbitmq_mqtt/test/java_SUITE_data/src/test/java/com/rabbitmq/mqtt/test/MqttV5Test.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import static org.eclipse.paho.mqttv5.common.packet.MqttReturnCode.RETURN_CODE_BAD_USERNAME_OR_PASSWORD;
1313

1414
import com.rabbitmq.client.*;
15-
import java.io.ByteArrayOutputStream;
1615
import java.io.DataInputStream;
1716
import java.io.IOException;
1817
import java.io.OutputStream;
@@ -55,7 +54,6 @@ public class MqttV5Test implements MqttCallback {
5554
private final byte[] payload = "payload".getBytes();
5655
private final String topic = "test-topic";
5756
private final String retainedTopic = "test-retained-topic";
58-
private int testDelay = 2000;
5957

6058
private Connection conn;
6159
private Channel ch;
@@ -78,14 +76,12 @@ private static int getAmqpPort() {
7876
}
7977

8078
// override the 10s limit
81-
private class TestMqttConnectOptions extends MqttConnectionOptions {
79+
private static class TestMqttConnectOptions extends MqttConnectionOptions {
8280
private int keepAliveInterval = 60;
83-
private final String user_name = "guest";
84-
private final String password = "guest";
8581

8682
public TestMqttConnectOptions() {
87-
super.setUserName(user_name);
88-
super.setPassword(password.getBytes());
83+
super.setUserName("guest");
84+
super.setPassword("guest".getBytes());
8985
super.setTopicAliasMaximum(0);
9086
super.setKeepAliveInterval(keepAliveInterval);
9187
}
@@ -211,8 +207,7 @@ public void connectFirst() throws MqttException, IOException {
211207

212208
// ---8<---
213209
// Copy/pasted from readMqttWireMessage() in MqttInputStream.java.
214-
ByteArrayOutputStream bais = new ByteArrayOutputStream();
215-
byte first = in.readByte();
210+
in.readByte();
216211
// ---8<---
217212

218213
fail("Error expected if CONNECT is not first packet");
@@ -823,7 +818,7 @@ public Socket createSocket() {
823818
}
824819

825820
@Test
826-
public void topicAuthorisationVariableExpansion(TestInfo info) throws Exception {
821+
public void topicAuthorisationVariableExpansion() throws Exception {
827822
final String client_id = "client-id-variable-expansion";
828823
MqttClient client = newConnectedClient(client_id);
829824
client.setCallback(this);
@@ -894,7 +889,7 @@ public void interopA2M(TestInfo info)
894889
// [MQTT-3.1.3-6]
895890
// RabbitMQ allows a Client to supply a ClientId that has a length of zero bytes.
896891
@Test
897-
public void emptyClientId(TestInfo info) throws MqttException, InterruptedException {
892+
public void emptyClientId() throws MqttException, InterruptedException {
898893
String emptyClientId = "";
899894
MqttClient client = newConnectedClient(emptyClientId);
900895
MqttClient client2 = newConnectedClient(emptyClientId);
@@ -955,6 +950,7 @@ private Integer receivedMessagesSize() {
955950

956951
private void waitForTestDelay() {
957952
try {
953+
int testDelay = 2000;
958954
Thread.sleep(testDelay);
959955
} catch (InterruptedException e) {
960956
throw new RuntimeException(e);

deps/rabbitmq_mqtt/test/java_SUITE_data/src/test/java/com/rabbitmq/mqtt/test/tls/MqttSSLTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ private static String getHost() {
4545
}
4646

4747
// override 10s limit
48-
private class MyConnOpts extends MqttConnectOptions {
48+
private static class MyConnOpts extends MqttConnectOptions {
4949
private int keepAliveInterval = 60;
5050

5151
@Override

deps/rabbitmq_mqtt/test/java_SUITE_data/src/test/java/com/rabbitmq/mqtt/test/tls/MqttV5SSLTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private static String getHost() {
4747
}
4848

4949
// override 10s limit
50-
private class MyConnOpts extends MqttConnectionOptions {
50+
private static class MyConnOpts extends MqttConnectionOptions {
5151
private int keepAliveInterval = 60;
5252

5353
@Override

deps/rabbitmq_mqtt/test/java_SUITE_data/src/test/java/com/rabbitmq/mqtt/test/tls/MutualAuth.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
//
77
package com.rabbitmq.mqtt.test.tls;
88

9-
import java.io.FileInputStream;
9+
import static java.nio.file.Files.newInputStream;
10+
1011
import java.io.IOException;
12+
import java.nio.file.Paths;
1113
import java.security.KeyStore;
1214
import java.security.KeyStoreException;
1315
import java.security.NoSuchAlgorithmException;
@@ -33,11 +35,10 @@ private static TrustManagerFactory getServerTrustManagerFactory()
3335
throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException {
3436
String keystorePath = System.getProperty("test-keystore.ca");
3537
char[] trustPhrase = getStringProperty("test-keystore.password").toCharArray();
36-
MutualAuth dummy = new MutualAuth();
3738

3839
// Server TrustStore
3940
KeyStore tks = KeyStore.getInstance("JKS");
40-
tks.load(new FileInputStream(keystorePath), trustPhrase);
41+
tks.load(newInputStream(Paths.get(keystorePath)), trustPhrase);
4142

4243
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
4344
tmf.init(tks);
@@ -51,12 +52,11 @@ public static SSLContext getSSLContextWithClientCert() throws IOException {
5152

5253
String p12Path = System.getProperty("test-client-cert.path");
5354

54-
MutualAuth dummy = new MutualAuth();
5555
try {
5656
SSLContext sslContext = getVanillaSSLContext();
5757
// Client Keystore
5858
KeyStore ks = KeyStore.getInstance("PKCS12");
59-
ks.load(new FileInputStream(p12Path), clientPhrase);
59+
ks.load(newInputStream(Paths.get(p12Path)), clientPhrase);
6060
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
6161
kmf.init(ks, clientPhrase);
6262

@@ -69,7 +69,6 @@ public static SSLContext getSSLContextWithClientCert() throws IOException {
6969
}
7070

7171
private static SSLContext getVanillaSSLContext() throws NoSuchAlgorithmException {
72-
SSLContext result = null;
7372
List<String> xs = Arrays.asList("TLSv1.2", "TLSv1.1", "TLSv1");
7473
for (String x : xs) {
7574
try {

0 commit comments

Comments
 (0)