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 @Override
public void init(String name) throws Exception { public void init(String name) throws Exception {
if ((directoryService != null) && directoryService.isStarted()) { if ((directoryService == null) || directoryService.isStarted()) {
return; return;
} }

View File

@ -671,12 +671,10 @@ public class JMSXDeliveryCountTest extends JMSTestBase {
if (tm == null) { if (tm == null) {
failed = true; failed = true;
} }
else if (!tm.getText().equals("testing" + i)) {
if (!tm.getText().equals("testing" + i)) {
failed = true; failed = true;
} }
else if (tm.getIntProperty("JMSXDeliveryCount") != j + 1) {
if (tm.getIntProperty("JMSXDeliveryCount") != j + 1) {
failed = true; failed = true;
} }
} }

View File

@ -108,10 +108,7 @@ public class JournalCrashTest extends ActiveMQTestBase {
} }
public void sendMessages(final int start, final int end) throws Exception { public void sendMessages(final int start, final int end) throws Exception {
ClientSession session = null; try (ClientSession session = factory.createSession(false, false)) {
try {
session = factory.createSession(false, false);
try { try {
session.createQueue(QUEUE, QUEUE, true); session.createQueue(QUEUE, QUEUE, true);
@ -132,9 +129,6 @@ public class JournalCrashTest extends ActiveMQTestBase {
session.close(); session.close();
// server.stop(); -- this test was not supposed to stop the server, it should crash // server.stop(); -- this test was not supposed to stop the server, it should crash
} }
finally {
session.close();
}
} }
@Test @Test
@ -146,11 +140,10 @@ public class JournalCrashTest extends ActiveMQTestBase {
printJournal(); printJournal();
ClientSession session = null;
try {
startServer(); startServer();
session = factory.createSession(true, true); try (ClientSession session = factory.createSession(true, true)) {
ClientConsumer consumer = session.createConsumer(QUEUE); ClientConsumer consumer = session.createConsumer(QUEUE);
session.start(); session.start();
@ -165,14 +158,6 @@ public class JournalCrashTest extends ActiveMQTestBase {
} }
session.close(); session.close();
} }
finally {
try {
session.close();
}
catch (Throwable ignored) {
}
}
} }
/** /**

View File

@ -64,8 +64,9 @@ public class StompV11Test extends StompV11TestBase {
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
try { try {
log.debug("Connection 11 : " + connV11.isConnected()); boolean connected = connV11 != null && connV11.isConnected();
if (connV11 != null && connV11.isConnected()) { log.debug("Connection 11 : " + connected);
if (connected) {
connV11.disconnect(); connV11.disconnect();
} }
} }

View File

@ -67,8 +67,9 @@ public class StompV12Test extends StompV11TestBase {
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
try { try {
log.debug("Connection 1.2 : " + connV12.isConnected()); boolean connected = connV12 != null && connV12.isConnected();
if (connV12 != null && connV12.isConnected()) { log.debug("Connection 1.2 : " + connected);
if (connected) {
connV12.disconnect(); connV12.disconnect();
} }
} }