From 092c392bee53695008f0793c48ad159f343be270 Mon Sep 17 00:00:00 2001 From: vbeskrovnov Date: Tue, 17 Oct 2017 21:46:45 +0400 Subject: [PATCH 1/2] Throw `IllegalArgumentException` instead of NPE in unexpected place. --- .../slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java index 07b5a3ca..91223a51 100644 --- a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java +++ b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java @@ -509,6 +509,9 @@ private void stopConnectionMonitoring() { @Override public SlackMessageHandle sendMessage(SlackChannel channel, SlackPreparedMessage preparedMessage, SlackChatConfiguration chatConfiguration) { + if (channel == null) { + throw new IllegalArgumentException("Channel can't be null"); + } SlackMessageHandle handle = new SlackMessageHandle<>(getNextMessageId()); Map arguments = new HashMap<>(); arguments.put("token", authToken); From d1fc6157537dfb90c2dc34d80164ed656c7c5407 Mon Sep 17 00:00:00 2001 From: vbeskrovnov Date: Tue, 17 Oct 2017 22:00:14 +0400 Subject: [PATCH 2/2] Add test for fix --- .../impl/TestSlackWebSocketSessionImpl.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 sources/src/test/java/com/ullink/slack/simpleslackapi/impl/TestSlackWebSocketSessionImpl.java diff --git a/sources/src/test/java/com/ullink/slack/simpleslackapi/impl/TestSlackWebSocketSessionImpl.java b/sources/src/test/java/com/ullink/slack/simpleslackapi/impl/TestSlackWebSocketSessionImpl.java new file mode 100644 index 00000000..6cead10d --- /dev/null +++ b/sources/src/test/java/com/ullink/slack/simpleslackapi/impl/TestSlackWebSocketSessionImpl.java @@ -0,0 +1,23 @@ +package com.ullink.slack.simpleslackapi.impl; + +import com.ullink.slack.simpleslackapi.WebSocketContainerProvider; +import mockit.Mocked; +import org.junit.Test; + +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.fail; + +public class TestSlackWebSocketSessionImpl { + + @Test(expected = IllegalArgumentException.class) + public void testSendMessageWithNullChanel(@Mocked WebSocketContainerProvider provider) throws Exception{ + SlackWebSocketSessionImpl webSocketSession = new SlackWebSocketSessionImpl(provider, + "", false, 42L, TimeUnit.MILLISECONDS); + try { + webSocketSession.sendMessage(null, ""); + } catch (NullPointerException e) { + fail("NullPointerException unexpected here"); + } + } +}