From 04eed1b7f4c5551746c883aa640ef5279ba9f30e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 28 Jul 2016 22:50:55 +0300 Subject: [PATCH] Fix some potential NPEs in examples and tests --- .../ldap/InMemoryDirectoryServiceFactory.java | 2 +- .../extras/jms/xa/JMSXDeliveryCountTest.java | 6 ++--- .../integration/client/JournalCrashTest.java | 23 ++++--------------- .../integration/stomp/v11/StompV11Test.java | 5 ++-- .../integration/stomp/v12/StompV12Test.java | 5 ++-- 5 files changed, 13 insertions(+), 28 deletions(-) diff --git a/examples/features/standard/security-ldap/src/main/java/org/apache/activemq/artemis/jms/example/ldap/InMemoryDirectoryServiceFactory.java b/examples/features/standard/security-ldap/src/main/java/org/apache/activemq/artemis/jms/example/ldap/InMemoryDirectoryServiceFactory.java index ce48693e67..d7ebde4b84 100644 --- a/examples/features/standard/security-ldap/src/main/java/org/apache/activemq/artemis/jms/example/ldap/InMemoryDirectoryServiceFactory.java +++ b/examples/features/standard/security-ldap/src/main/java/org/apache/activemq/artemis/jms/example/ldap/InMemoryDirectoryServiceFactory.java @@ -89,7 +89,7 @@ public class InMemoryDirectoryServiceFactory implements DirectoryServiceFactory */ @Override public void init(String name) throws Exception { - if ((directoryService != null) && directoryService.isStarted()) { + if ((directoryService == null) || directoryService.isStarted()) { return; } diff --git a/tests/extra-tests/src/test/java/org/apache/activemq/artemis/tests/extras/jms/xa/JMSXDeliveryCountTest.java b/tests/extra-tests/src/test/java/org/apache/activemq/artemis/tests/extras/jms/xa/JMSXDeliveryCountTest.java index 1d71598cec..3c1c0b58c7 100644 --- a/tests/extra-tests/src/test/java/org/apache/activemq/artemis/tests/extras/jms/xa/JMSXDeliveryCountTest.java +++ b/tests/extra-tests/src/test/java/org/apache/activemq/artemis/tests/extras/jms/xa/JMSXDeliveryCountTest.java @@ -671,12 +671,10 @@ public class JMSXDeliveryCountTest extends JMSTestBase { if (tm == null) { failed = true; } - - if (!tm.getText().equals("testing" + i)) { + else if (!tm.getText().equals("testing" + i)) { failed = true; } - - if (tm.getIntProperty("JMSXDeliveryCount") != j + 1) { + else if (tm.getIntProperty("JMSXDeliveryCount") != j + 1) { failed = true; } } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/client/JournalCrashTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/client/JournalCrashTest.java index 774fb1b2c6..e340d38059 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/client/JournalCrashTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/client/JournalCrashTest.java @@ -108,10 +108,7 @@ public class JournalCrashTest extends ActiveMQTestBase { } public void sendMessages(final int start, final int end) throws Exception { - ClientSession session = null; - try { - - session = factory.createSession(false, false); + try (ClientSession session = factory.createSession(false, false)) { try { session.createQueue(QUEUE, QUEUE, true); @@ -132,9 +129,6 @@ public class JournalCrashTest extends ActiveMQTestBase { session.close(); // server.stop(); -- this test was not supposed to stop the server, it should crash } - finally { - session.close(); - } } @Test @@ -146,11 +140,10 @@ public class JournalCrashTest extends ActiveMQTestBase { printJournal(); - ClientSession session = null; - try { - startServer(); + startServer(); + + try (ClientSession session = factory.createSession(true, true)) { - session = factory.createSession(true, true); ClientConsumer consumer = session.createConsumer(QUEUE); session.start(); @@ -165,14 +158,6 @@ public class JournalCrashTest extends ActiveMQTestBase { } session.close(); } - finally { - try { - session.close(); - } - catch (Throwable ignored) { - } - } - } /** diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/v11/StompV11Test.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/v11/StompV11Test.java index b3d1461135..f539cd6f4d 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/v11/StompV11Test.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/v11/StompV11Test.java @@ -64,8 +64,9 @@ public class StompV11Test extends StompV11TestBase { @After public void tearDown() throws Exception { try { - log.debug("Connection 11 : " + connV11.isConnected()); - if (connV11 != null && connV11.isConnected()) { + boolean connected = connV11 != null && connV11.isConnected(); + log.debug("Connection 11 : " + connected); + if (connected) { connV11.disconnect(); } } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/v12/StompV12Test.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/v12/StompV12Test.java index 3b9c991db3..adebae6a54 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/v12/StompV12Test.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/stomp/v12/StompV12Test.java @@ -67,8 +67,9 @@ public class StompV12Test extends StompV11TestBase { @After public void tearDown() throws Exception { try { - log.debug("Connection 1.2 : " + connV12.isConnected()); - if (connV12 != null && connV12.isConnected()) { + boolean connected = connV12 != null && connV12.isConnected(); + log.debug("Connection 1.2 : " + connected); + if (connected) { connV12.disconnect(); } }