Fix some potential NPEs in examples and tests

This commit is contained in:
Ville Skyttä 2016-07-28 22:50:55 +03:00
parent 61222233ca
commit 04eed1b7f4
5 changed files with 13 additions and 28 deletions

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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) {
}
}
}
/**

View File

@ -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();
}
}

View File

@ -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();
}
}