Skip to content

Commit 8a3f9ff

Browse files
authored
Testing Cleanup Only - No Functionality Changes (#1110)
1 parent 475a02c commit 8a3f9ff

23 files changed

+556
-652
lines changed

src/test/java/io/nats/client/TestAuthHandler.java renamed to src/test/java/io/nats/client/AuthHandlerForTesting.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
import java.io.IOException;
1717
import java.security.GeneralSecurityException;
1818

19-
public class TestAuthHandler implements AuthHandler {
20-
private NKey nkey;
19+
public class AuthHandlerForTesting implements AuthHandler {
20+
private final NKey nkey;
2121

22-
public TestAuthHandler(NKey nkey) {
22+
public AuthHandlerForTesting(NKey nkey) {
2323
this.nkey = nkey;
2424
}
2525

26-
public TestAuthHandler() throws Exception {
26+
public AuthHandlerForTesting() throws Exception {
2727
this.nkey = NKey.createUser(null);
2828
}
2929

src/test/java/io/nats/client/AuthTests.java

Lines changed: 51 additions & 53 deletions
Large diffs are not rendered by default.

src/test/java/io/nats/client/ConnectTests.java

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
import io.nats.client.ConnectionListener.Events;
1717
import io.nats.client.NatsServerProtocolMock.ExitAt;
18+
import io.nats.client.impl.ListenerForTesting;
1819
import io.nats.client.impl.SimulateSocketDataPortException;
19-
import io.nats.client.impl.TestHandler;
2020
import org.junit.jupiter.api.Test;
2121

2222
import java.io.IOException;
@@ -35,7 +35,7 @@
3535
public class ConnectTests {
3636
@Test
3737
public void testDefaultConnection() throws Exception {
38-
try (NatsTestServer ts = new NatsTestServer(Options.DEFAULT_PORT, false)) {
38+
try (NatsTestServer ignored = new NatsTestServer(Options.DEFAULT_PORT, false)) {
3939
Connection nc = standardConnection();
4040
assertEquals(Options.DEFAULT_PORT, nc.getServerInfo().getPort());
4141
standardCloseConnection(nc);
@@ -235,7 +235,6 @@ public void testFailWrongInitialInfoOP() {
235235
@Test
236236
public void testIncompleteInitialInfo() {
237237
assertThrows(IOException.class, () -> {
238-
Connection nc = null;
239238
String badInfo = "{\"server_id\"\r\n";
240239
try (NatsServerProtocolMock ts = new NatsServerProtocolMock(null, badInfo)) {
241240
Options options = new Options.Builder().server(ts.getURI()).reconnectWait(Duration.ofDays(1)).build();
@@ -246,18 +245,18 @@ public void testIncompleteInitialInfo() {
246245

247246
@Test
248247
public void testAsyncConnection() throws Exception {
249-
TestHandler handler = new TestHandler();
248+
ListenerForTesting listener = new ListenerForTesting();
250249
Connection nc = null;
251250

252251
try (NatsTestServer ts = new NatsTestServer(false)) {
253-
Options options = new Options.Builder().server(ts.getURI()).connectionListener(handler).build();
254-
handler.prepForStatusChange(Events.CONNECTED);
252+
Options options = new Options.Builder().server(ts.getURI()).connectionListener(listener).build();
253+
listener.prepForStatusChange(Events.CONNECTED);
255254

256255
Nats.connectAsynchronously(options, false);
257256

258-
handler.waitForStatusChange(1, TimeUnit.SECONDS);
257+
listener.waitForStatusChange(1, TimeUnit.SECONDS);
259258

260-
nc = handler.getLastEventConnection();
259+
nc = listener.getLastEventConnection();
261260
assertNotNull(nc);
262261
assertConnected(nc);
263262
standardCloseConnection(nc);
@@ -266,21 +265,21 @@ public void testAsyncConnection() throws Exception {
266265

267266
@Test
268267
public void testAsyncConnectionWithReconnect() throws Exception {
269-
TestHandler handler = new TestHandler();
268+
ListenerForTesting listener = new ListenerForTesting();
270269
int port = NatsTestServer.nextPort();
271270
Options options = new Options.Builder().server("nats://localhost:" + port).maxReconnects(-1)
272-
.reconnectWait(Duration.ofMillis(100)).connectionListener(handler).build();
271+
.reconnectWait(Duration.ofMillis(100)).connectionListener(listener).build();
273272

274273
Nats.connectAsynchronously(options, true);
275274

276275
sleep(5000); // No server at this point, let it fail and try to start over
277276

278-
Connection nc = handler.getLastEventConnection(); // will be disconnected, but should be there
277+
Connection nc = listener.getLastEventConnection(); // will be disconnected, but should be there
279278
assertNotNull(nc);
280279

281-
handler.prepForStatusChange(Events.RECONNECTED);
282-
try (NatsTestServer ts = new NatsTestServer(port, false)) {
283-
standardConnectionWait(nc, handler);
280+
listener.prepForStatusChange(Events.RECONNECTED);
281+
try (NatsTestServer ignored = new NatsTestServer(port, false)) {
282+
standardConnectionWait(nc, listener);
284283
standardCloseConnection(nc);
285284
}
286285
}
@@ -297,15 +296,15 @@ public void testThrowOnAsyncWithoutListener() {
297296

298297
@Test
299298
public void testErrorOnAsync() throws Exception {
300-
TestHandler handler = new TestHandler();
299+
ListenerForTesting listener = new ListenerForTesting();
301300
Options options = new Options.Builder().server("nats://localhost:" + NatsTestServer.nextPort())
302-
.connectionListener(handler).errorListener(handler).noReconnect().build();
303-
handler.prepForStatusChange(Events.CLOSED);
301+
.connectionListener(listener).errorListener(listener).noReconnect().build();
302+
listener.prepForStatusChange(Events.CLOSED);
304303
Nats.connectAsynchronously(options, false);
305-
handler.waitForStatusChange(10, TimeUnit.SECONDS);
304+
listener.waitForStatusChange(10, TimeUnit.SECONDS);
306305

307-
assertTrue(handler.getExceptionCount() > 0);
308-
assertTrue(handler.getEventCount(Events.CLOSED) > 0);
306+
assertTrue(listener.getExceptionCount() > 0);
307+
assertTrue(listener.getEventCount(Events.CLOSED) > 0);
309308
}
310309

311310
@Test
@@ -439,6 +438,7 @@ public void testFlushBufferThreadSafety() throws Exception {
439438
CountDownLatch completedLatch = new CountDownLatch(1);
440439

441440
Thread t = new Thread("publisher") {
441+
@SuppressWarnings("ResultOfMethodCallIgnored")
442442
public void run() {
443443
byte[] payload = new byte[5];
444444
pubLatch.countDown();
@@ -489,13 +489,13 @@ public void run() {
489489
}
490490
}
491491

492-
@SuppressWarnings({"unused", "UnusedAssignment", "resource"})
492+
@SuppressWarnings({"unused", "UnusedAssignment"})
493493
@Test
494494
public void testSocketLevelException() throws Exception {
495495
int port = NatsTestServer.nextPort();
496496

497497
AtomicBoolean simExReceived = new AtomicBoolean();
498-
TestHandler th = new TestHandler();
498+
ListenerForTesting listener = new ListenerForTesting();
499499
ErrorListener el = new ErrorListener() {
500500
@Override
501501
public void exceptionOccurred(Connection conn, Exception exp) {
@@ -508,7 +508,7 @@ public void exceptionOccurred(Connection conn, Exception exp) {
508508
Options options = new Options.Builder()
509509
.server(NatsTestServer.getNatsLocalhostUri(port))
510510
.dataPortType("io.nats.client.impl.SimulateSocketDataPortException")
511-
.connectionListener(th)
511+
.connectionListener(listener)
512512
.errorListener(el)
513513
.reconnectDelayHandler(l -> Duration.ofSeconds(1))
514514
.build();
@@ -534,25 +534,25 @@ public void exceptionOccurred(Connection conn, Exception exp) {
534534
try (NatsTestServer ts = new NatsTestServer(port, false)) {
535535
try {
536536
SimulateSocketDataPortException.THROW_ON_CONNECT.set(true);
537-
th.prepForStatusChange(Events.RECONNECTED);
537+
listener.prepForStatusChange(Events.RECONNECTED);
538538
connection = Nats.connectReconnectOnConnect(options);
539-
assertTrue(th.waitForStatusChange(5, TimeUnit.SECONDS));
540-
th.prepForStatusChange(Events.DISCONNECTED);
539+
assertTrue(listener.waitForStatusChange(5, TimeUnit.SECONDS));
540+
listener.prepForStatusChange(Events.DISCONNECTED);
541541
}
542542
catch (Exception e) {
543543
fail("should have connected " + e);
544544
}
545545
}
546-
assertTrue(th.waitForStatusChange(5, TimeUnit.SECONDS));
546+
assertTrue(listener.waitForStatusChange(5, TimeUnit.SECONDS));
547547
assertTrue(simExReceived.get());
548548
simExReceived.set(false);
549549

550550
// 2. NORMAL RECONNECT
551-
th.prepForStatusChange(Events.RECONNECTED);
551+
listener.prepForStatusChange(Events.RECONNECTED);
552552
try (NatsTestServer ts = new NatsTestServer(port, false)) {
553553
SimulateSocketDataPortException.THROW_ON_CONNECT.set(true);
554554
try {
555-
assertTrue(th.waitForStatusChange(5, TimeUnit.SECONDS));
555+
assertTrue(listener.waitForStatusChange(5, TimeUnit.SECONDS));
556556
}
557557
catch (Exception e) {
558558
fail("should have reconnected " + e);

src/test/java/io/nats/client/OptionsTests.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ private static void _testDefaultOptions(Options o) {
9696
assertEquals(Options.DEFAULT_REQUEST_CLEANUP_INTERVAL, o.getRequestCleanupInterval(),
9797
"default cleanup interval");
9898

99-
assertTrue(o.getErrorListener() instanceof ErrorListenerLoggerImpl, "error handler");
100-
assertNull(o.getConnectionListener(), "disconnect handler");
99+
assertInstanceOf(ErrorListenerLoggerImpl.class, o.getErrorListener(), "error listener");
100+
assertNull(o.getConnectionListener(), "disconnect listener");
101101
assertNull(o.getStatisticsCollector(), "statistics collector");
102102
assertFalse(o.isOldRequestStyle(), "default oldstyle");
103103
}
@@ -152,7 +152,7 @@ private static void _testChainedStringOptions(Options o) {
152152

153153
@Test
154154
public void testChainedSecure() throws Exception {
155-
SSLContext ctx = TestSSLUtils.createTestSSLContext();
155+
SSLContext ctx = SslTestingHelper.createTestSSLContext();
156156
SSLContext.setDefault(ctx);
157157
Options o = new Options.Builder().secure().build();
158158
_testChainedSecure(ctx, o);
@@ -165,7 +165,7 @@ private static void _testChainedSecure(SSLContext ctx, Options o) {
165165

166166
@Test
167167
public void testChainedSSLOptions() throws Exception {
168-
SSLContext ctx = TestSSLUtils.createTestSSLContext();
168+
SSLContext ctx = SslTestingHelper.createTestSSLContext();
169169
Options o = new Options.Builder().sslContext(ctx).build();
170170
_testChainedSSLOptions(ctx, o);
171171
_testChainedSSLOptions(ctx, new Options.Builder(o).build());
@@ -239,15 +239,15 @@ public void testHttpRequestInterceptors() {
239239

240240
@Test
241241
public void testChainedErrorHandler() {
242-
TestHandler handler = new TestHandler();
243-
Options o = new Options.Builder().errorListener(handler).build();
244-
_testChainedErrorHandler(handler, o);
245-
_testChainedErrorHandler(handler, new Options.Builder(o).build());
242+
ListenerForTesting listener = new ListenerForTesting();
243+
Options o = new Options.Builder().errorListener(listener).build();
244+
_testChainedErrorListener(listener, o);
245+
_testChainedErrorListener(listener, new Options.Builder(o).build());
246246
}
247247

248-
private static void _testChainedErrorHandler(TestHandler handler, Options o) {
248+
private static void _testChainedErrorListener(ListenerForTesting listener, Options o) {
249249
assertFalse(o.isVerbose(), "default verbose"); // One from a different type
250-
assertEquals(handler, o.getErrorListener(), "chained error handler");
250+
assertEquals(listener, o.getErrorListener(), "chained error listener");
251251
}
252252

253253
@Test
@@ -260,8 +260,8 @@ public void testChainedConnectionListener() {
260260

261261
private static void _testChainedConnectionListener(ConnectionListener cHandler, Options o) {
262262
assertFalse(o.isVerbose(), "default verbose"); // One from a different type
263-
assertTrue(o.getErrorListener() instanceof ErrorListenerLoggerImpl, "error handler");
264-
assertSame(cHandler, o.getConnectionListener(), "chained connection handler");
263+
assertInstanceOf(ErrorListenerLoggerImpl.class, o.getErrorListener(), "error listener");
264+
assertSame(cHandler, o.getConnectionListener(), "chained connection listener");
265265
}
266266

267267
@Test
@@ -274,7 +274,7 @@ public void testChainedStatisticsCollector() {
274274

275275
private static void _testChainedStatisticsCollector(StatisticsCollector cHandler, Options o) {
276276
assertFalse(o.isVerbose(), "default verbose"); // One from a different type
277-
assertTrue(o.getStatisticsCollector() instanceof TestStatisticsCollector, "statistics collector");
277+
assertInstanceOf(TestStatisticsCollector.class, o.getStatisticsCollector(), "statistics collector");
278278
assertSame(cHandler, o.getStatisticsCollector(), "chained statistics collector");
279279
}
280280

@@ -334,7 +334,7 @@ private static void _testPropertiesStringOptions(Options o) {
334334
@Test
335335
public void testPropertiesSSLOptions() throws Exception {
336336
// don't use default for tests, issues with forcing algorithm exception in other tests break it
337-
SSLContext.setDefault(TestSSLUtils.createTestSSLContext());
337+
SSLContext.setDefault(SslTestingHelper.createTestSSLContext());
338338
Properties props = new Properties();
339339
props.setProperty(Options.PROP_SECURE, "true");
340340

@@ -476,7 +476,7 @@ private static void _testProperties(Options o) {
476476
assertEquals(1000, o.getPingInterval().toMillis());
477477
assertNotNull(o.getAuthHandler());
478478
assertNotNull(o.getServerPool());
479-
assertTrue(o.getServerPool() instanceof CoverageServerPool);
479+
assertInstanceOf(CoverageServerPool.class, o.getServerPool());
480480
}
481481

482482
@Test
@@ -583,32 +583,32 @@ private static void _testPropertyDurationOptions(Options o) {
583583
}
584584

585585
@Test
586-
public void testPropertyErrorHandler() {
586+
public void testPropertyErrorListener() {
587587
Properties props = new Properties();
588-
props.setProperty(Options.PROP_ERROR_LISTENER, TestHandler.class.getCanonicalName());
588+
props.setProperty(Options.PROP_ERROR_LISTENER, ListenerForTesting.class.getCanonicalName());
589589

590590
Options o = new Options.Builder(props).build();
591591
assertFalse(o.isVerbose(), "default verbose"); // One from a different type
592-
assertNotNull(o.getErrorListener(), "property error handler");
592+
assertNotNull(o.getErrorListener(), "property error listener");
593593

594594
o.getErrorListener().errorOccurred(null, "bad subject");
595-
assertEquals(((TestHandler) o.getErrorListener()).getCount(), 1, "property error handler class");
595+
assertEquals(((ListenerForTesting) o.getErrorListener()).getCount(), 1, "property error listener class");
596596
}
597597

598598
@Test
599599
public void testPropertyConnectionListeners() {
600600
Properties props = new Properties();
601-
props.setProperty(Options.PROP_CONNECTION_CB, TestHandler.class.getCanonicalName());
601+
props.setProperty(Options.PROP_CONNECTION_CB, ListenerForTesting.class.getCanonicalName());
602602

603603
Options o = new Options.Builder(props).build();
604604
assertFalse(o.isVerbose(), "default verbose"); // One from a different type
605-
assertNotNull(o.getConnectionListener(), "property connection handler");
605+
assertNotNull(o.getConnectionListener(), "property connection listener");
606606

607607
o.getConnectionListener().connectionEvent(null, Events.DISCONNECTED);
608608
o.getConnectionListener().connectionEvent(null, Events.RECONNECTED);
609609
o.getConnectionListener().connectionEvent(null, Events.CLOSED);
610610

611-
assertEquals(((TestHandler) o.getConnectionListener()).getCount(), 3, "property connect handler class");
611+
assertEquals(((ListenerForTesting) o.getConnectionListener()).getCount(), 3, "property connect listener class");
612612
}
613613

614614
@Test
@@ -654,7 +654,7 @@ public void testNonDefaultConnectOptions() {
654654

655655
@Test
656656
public void testConnectOptionsWithNameAndContext() throws Exception {
657-
SSLContext ctx = TestSSLUtils.createTestSSLContext();
657+
SSLContext ctx = SslTestingHelper.createTestSSLContext();
658658
Options o = new Options.Builder().sslContext(ctx).connectionName("c1").build();
659659
String expected = "{\"lang\":\"java\",\"version\":\"" + Nats.CLIENT_VERSION + "\",\"name\":\"c1\""
660660
+ ",\"protocol\":1,\"verbose\":false,\"pedantic\":false,\"tls_required\":true,\"echo\":true,\"headers\":true,\"no_responders\":true}";
@@ -679,7 +679,7 @@ public void testAuthConnectOptions() {
679679

680680
@Test
681681
public void testNKeyConnectOptions() throws Exception {
682-
TestAuthHandler th = new TestAuthHandler();
682+
AuthHandlerForTesting th = new AuthHandlerForTesting();
683683
byte[] nonce = "abcdefg".getBytes(StandardCharsets.UTF_8);
684684
String sig = Base64.getUrlEncoder().withoutPadding().encodeToString(th.sign(nonce));
685685

src/test/java/io/nats/client/RequestTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import io.nats.client.api.StorageType;
1717
import io.nats.client.api.StreamConfiguration;
18-
import io.nats.client.impl.TestHandler;
18+
import io.nats.client.impl.ListenerForTesting;
1919
import org.junit.jupiter.api.Test;
2020

2121
import java.io.IOException;
@@ -32,8 +32,8 @@ public class RequestTests {
3232
@Test
3333
public void testRequestNoResponder() throws Exception {
3434
try (NatsTestServer ts = new NatsTestServer(false, true)) {
35-
Options optCancel = Options.builder().server(ts.getURI()).errorListener(new TestHandler()).build();
36-
Options optReport = Options.builder().server(ts.getURI()).reportNoResponders().errorListener(new TestHandler()).build();
35+
Options optCancel = Options.builder().server(ts.getURI()).errorListener(new ListenerForTesting()).build();
36+
Options optReport = Options.builder().server(ts.getURI()).reportNoResponders().errorListener(new ListenerForTesting()).build();
3737
try (Connection ncCancel = standardConnection(optCancel);
3838
Connection ncReport = standardConnection(optReport);
3939
)

src/test/java/io/nats/client/TestSSLUtils.java renamed to src/test/java/io/nats/client/SslTestingHelper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.security.SecureRandom;
2323
import java.util.Properties;
2424

25-
public class TestSSLUtils {
25+
public class SslTestingHelper {
2626
public static String KEYSTORE_PATH = "src/test/resources/keystore.jks";
2727
public static String TRUSTSTORE_PATH = "src/test/resources/truststore.jks";
2828
public static String PASSWORD = "password";
@@ -34,10 +34,10 @@ public static KeyStore loadKeystore(String path) throws Exception {
3434

3535
public static Properties createTestSSLProperties() {
3636
Properties props = new Properties();
37-
props.setProperty(Options.PROP_KEYSTORE, TestSSLUtils.KEYSTORE_PATH);
38-
props.setProperty(Options.PROP_KEYSTORE_PASSWORD, TestSSLUtils.PASSWORD);
39-
props.setProperty(Options.PROP_TRUSTSTORE, TestSSLUtils.TRUSTSTORE_PATH);
40-
props.setProperty(Options.PROP_TRUSTSTORE_PASSWORD, TestSSLUtils.PASSWORD);
37+
props.setProperty(Options.PROP_KEYSTORE, KEYSTORE_PATH);
38+
props.setProperty(Options.PROP_KEYSTORE_PASSWORD, PASSWORD);
39+
props.setProperty(Options.PROP_TRUSTSTORE, TRUSTSTORE_PATH);
40+
props.setProperty(Options.PROP_TRUSTSTORE_PASSWORD, PASSWORD);
4141
return props;
4242
}
4343

0 commit comments

Comments
 (0)