From 70c571bf22cf641572c832e1d909e6caac22023d Mon Sep 17 00:00:00 2001 From: Yegorisa Date: Thu, 28 Dec 2017 23:21:52 +0800 Subject: [PATCH] Added tests for ClusterWS, replaced Options class with mUrl variable and added test for null pointer exception when url is null --- .gitignore | 1 - .../java/com/clusterws/ChannelServerTest.java | 199 ++++++++++++++ src/test/java/com/clusterws/ChannelTest.java | 60 ++++ .../com/clusterws/ClusterWSStatesBool.java | 57 ++++ .../java/com/clusterws/ClusterWSTest.java | 257 ++++++++++++++++++ src/test/java/com/clusterws/EmitterTest.java | 67 +++++ .../com/clusterws/MessageHandlerTest.java | 163 +++++++++++ .../java/com/clusterws/PingHandlerTest.java | 36 +++ 8 files changed, 839 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/clusterws/ChannelServerTest.java create mode 100644 src/test/java/com/clusterws/ChannelTest.java create mode 100644 src/test/java/com/clusterws/ClusterWSStatesBool.java create mode 100644 src/test/java/com/clusterws/ClusterWSTest.java create mode 100644 src/test/java/com/clusterws/EmitterTest.java create mode 100644 src/test/java/com/clusterws/MessageHandlerTest.java create mode 100644 src/test/java/com/clusterws/PingHandlerTest.java diff --git a/.gitignore b/.gitignore index 052cd82..7cc091a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ /build *.iml /.gradle -/src/test /out \ No newline at end of file diff --git a/src/test/java/com/clusterws/ChannelServerTest.java b/src/test/java/com/clusterws/ChannelServerTest.java new file mode 100644 index 0000000..6d7ea33 --- /dev/null +++ b/src/test/java/com/clusterws/ChannelServerTest.java @@ -0,0 +1,199 @@ +package com.clusterws; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; + +import static org.junit.Assert.*; + +public class ChannelServerTest { + private ClusterWS mClusterWS; + private boolean mGotTheData; + private Object mReceivedData; + + @Before + public void init() throws Exception { + mClusterWS = new ClusterWS("ws://localhost:80"); + mGotTheData = false; + mReceivedData = null; + } + + @Test + public void testChannelPublishAndWatchInt() throws Exception { + mClusterWS.connect(); + Thread.sleep(1000); + mClusterWS.subscribe("testChannel") + .watch(new Channel.IChannelListener() { + @Override + public void onDataReceived(String channelName, Object data) { + mGotTheData = true; + mReceivedData = data; + } + }) + .publish(24); + Thread.sleep(1000); + + assertTrue("Did not get the data", mGotTheData); + assertEquals("Data send and data received are not the same", 24, mReceivedData); + } + + @Test + public void testChannelPublishAndWatchString() throws Exception { + mClusterWS.connect(); + Thread.sleep(1000); + mClusterWS.subscribe("testChannel") + .watch(new Channel.IChannelListener() { + @Override + public void onDataReceived(String channelName, Object data) { + mGotTheData = true; + mReceivedData = data; + } + }) + .publish("test string"); + Thread.sleep(1000); + + assertTrue("Did not get the data", mGotTheData); + assertEquals("Data send and data received are not the same", "test string", mReceivedData); + } + + @Test + public void testChannelPublishAndWatchObject() throws Exception { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("int", 30); + jsonObject.put("bool", true); + jsonObject.put("string", "CHLEN"); + JSONArray jsonArray = new JSONArray(); + jsonArray.add(30); + jsonArray.add(true); + jsonArray.add("CHLEN"); + jsonObject.put("array", jsonArray); + + mClusterWS.connect(); + Thread.sleep(1000); + mClusterWS.subscribe("testChannel") + .watch(new Channel.IChannelListener() { + @Override + public void onDataReceived(String channelName, Object data) { + mGotTheData = true; + mReceivedData = data; + } + }) + .publish(jsonObject); + Thread.sleep(1000); + + assertTrue("Did not get the data", mGotTheData); + assertEquals("Data send and data received are not the same", jsonObject, mReceivedData); + } + + @Test + public void testChannelPublishAndWatchNull() throws Exception { + mClusterWS.connect(); + Thread.sleep(1000); + mClusterWS.subscribe("testChannel") + .watch(new Channel.IChannelListener() { + @Override + public void onDataReceived(String channelName, Object data) { + mGotTheData = true; + mReceivedData = data; + } + }) + .publish(null); + Thread.sleep(1000); + + assertTrue("Did not get the data", mGotTheData); + assertEquals("Data send and data received are not the same", null, mReceivedData); + } + + @Test + public void testChannelPublishAndWatchArray() throws Exception { + ArrayList mObjectArrayList; + mObjectArrayList = new ArrayList<>(); + mObjectArrayList.add(30); + mObjectArrayList.add(false); + mObjectArrayList.add("Test message"); + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("int", 30); + jsonObject.put("bool", true); + jsonObject.put("string", "CHLEN"); + JSONArray jsonArray = new JSONArray(); + jsonArray.add(30); + jsonArray.add(true); + jsonArray.add("CHLEN"); + jsonObject.put("array", jsonArray); + + mObjectArrayList.add(jsonObject); + + mClusterWS.connect(); + Thread.sleep(1000); + mClusterWS.subscribe("testChannel") + .watch(new Channel.IChannelListener() { + @Override + public void onDataReceived(String channelName, Object data) { + mGotTheData = true; + mReceivedData = data; + } + }) + .publish(mObjectArrayList); + Thread.sleep(1000); + + assertTrue("Did not get the data", mGotTheData); + assertEquals("Data send and data received are not the same", mObjectArrayList, mReceivedData); + } + + @Test + public void testGetChannelByName() throws Exception { + mClusterWS.connect(); + Thread.sleep(1000); + mClusterWS.subscribe("testChannel"); + assertNotNull(mClusterWS.getChannelByName("testChannel")); + } + + @Test + public void testGetChannelList() throws Exception { + mClusterWS.connect(); + Thread.sleep(1000); + mClusterWS.subscribe("testChannel1"); + mClusterWS.subscribe("testChannel2"); + mClusterWS.subscribe("testChannel3"); + + assertEquals(mClusterWS.getChannels().size(), 3); + assertEquals(mClusterWS.getChannels().get(0).getChannelName(), "testChannel1"); + assertEquals(mClusterWS.getChannels().get(1).getChannelName(), "testChannel2"); + assertEquals(mClusterWS.getChannels().get(2).getChannelName(), "testChannel3"); + } + + @Test + public void testTryGetChannelByNameAfterUnsubscribing() throws Exception { + mClusterWS.connect(); + Thread.sleep(1000); + Channel channel = mClusterWS.subscribe("testChannel1"); + channel.unsubscribe(); + assertNull(mClusterWS.getChannelByName("testChannel1")); + } + + @Test + public void testResubscribeOnAllChannelsAfterReconnection() throws Exception { + mClusterWS.setReconnection(true, 1000, 2000, null); + mClusterWS.connect(); + Thread.sleep(1000); + Channel channel = mClusterWS.subscribe("testChannel") + .watch(new Channel.IChannelListener() { + @Override + public void onDataReceived(String channelName, Object data) { + mGotTheData = true; + mReceivedData = data; + } + }); + mClusterWS.disconnect(4001, "hui"); + Thread.sleep(1000); + channel.publish("testData"); + Thread.sleep(1000); + assertTrue("Did not get the data", mGotTheData); + assertEquals("Data send and data received are not the same", "testData", mReceivedData); + } + +} \ No newline at end of file diff --git a/src/test/java/com/clusterws/ChannelTest.java b/src/test/java/com/clusterws/ChannelTest.java new file mode 100644 index 0000000..429fa7f --- /dev/null +++ b/src/test/java/com/clusterws/ChannelTest.java @@ -0,0 +1,60 @@ +package com.clusterws; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; + +import java.util.ArrayList; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.when; + +public class ChannelTest { + private Channel mChannel; + private static final String CHANNEL_NAME = "testChannel"; + private static final String TEST_STRING_DATA = "testData"; + + @Mock + private ClusterWS mClusterWS; + + @Rule + public MockitoRule mockitoRule = MockitoJUnit.rule(); + + @Before + public void init(){ + mChannel = new Channel(CHANNEL_NAME,mClusterWS); + } + + @Test + public void watchAndOnMessage() throws Exception { + mChannel.watch(new Channel.IChannelListener() { + @Override + public void onDataReceived(String channelName, Object data) { + assertEquals("Data is not the same",TEST_STRING_DATA,data); + } + }); + mChannel.onMessage(TEST_STRING_DATA); + } + + + @Test + public void unsubscribe() throws Exception { + ArrayList channelArrayList = new ArrayList<>(); + channelArrayList.add(mChannel); + channelArrayList.add(new Channel("GAY",mClusterWS)); + channelArrayList.add(new Channel("SHIT",mClusterWS)); + when(mClusterWS.getChannels()).thenReturn(channelArrayList); + mChannel.unsubscribe(); + assertNotEquals("There is an old channel",CHANNEL_NAME,channelArrayList.get(0).getChannelName()); + } + + @Test + public void getChannelName() throws Exception { + assertEquals(mChannel.getChannelName(),CHANNEL_NAME); + } + + +} \ No newline at end of file diff --git a/src/test/java/com/clusterws/ClusterWSStatesBool.java b/src/test/java/com/clusterws/ClusterWSStatesBool.java new file mode 100644 index 0000000..c1ff7a3 --- /dev/null +++ b/src/test/java/com/clusterws/ClusterWSStatesBool.java @@ -0,0 +1,57 @@ +package com.clusterws; + +public class ClusterWSStatesBool { + private boolean mClosed; + private boolean mClosing; + private boolean mNotYetConnected; + private boolean mConnecting; + private boolean mOpen; + + public ClusterWSStatesBool() { + mClosed = false; + mClosing = false; + mNotYetConnected = false; + mConnecting = false; + mOpen = false; + } + + public boolean isClosed() { + return mClosed; + } + + public void setClosed(boolean closed) { + mClosed = closed; + } + + public boolean isClosing() { + return mClosing; + } + + public void setClosing(boolean closing) { + mClosing = closing; + } + + public boolean isNotYetConnected() { + return mNotYetConnected; + } + + public void setNotYetConnected(boolean notYetConnected) { + mNotYetConnected = notYetConnected; + } + + public boolean isConnecting() { + return mConnecting; + } + + public void setConnecting(boolean connecting) { + mConnecting = connecting; + } + + public boolean isOpen() { + return mOpen; + } + + public void setOpen(boolean open) { + mOpen = open; + } +} diff --git a/src/test/java/com/clusterws/ClusterWSTest.java b/src/test/java/com/clusterws/ClusterWSTest.java new file mode 100644 index 0000000..7d2b387 --- /dev/null +++ b/src/test/java/com/clusterws/ClusterWSTest.java @@ -0,0 +1,257 @@ +package com.clusterws; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import org.java_websocket.WebSocket; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.internal.matchers.Null; + +import java.util.ArrayList; + +import static org.junit.Assert.*; + +public class ClusterWSTest { + + private ClusterWS mClusterWS; + private Object receivedData; + private boolean gotTheData; + + @Before + public void init() { + mClusterWS = new ClusterWS("ws://localhost:80"); + receivedData = null; + gotTheData = false; + } + + @After + public void clearSocket() { + mClusterWS.disconnect(null, null); + mClusterWS = null; + } + + @Test + public void connect() throws Exception { + mClusterWS.connect(); + Thread.sleep(1000); + + assertEquals("Socket did not connect", WebSocket.READYSTATE.OPEN,mClusterWS.getState()); + } + + @Test + public void testOnAndSendString() throws Exception { + mClusterWS.connect(); + mClusterWS.on("String", new IEmitterListener() { + @Override + public void onDataReceived(Object data) { + gotTheData = true; + receivedData = data; + } + }); + Thread.sleep(1000); + mClusterWS.send("String", "test message"); + Thread.sleep(1000); + + assertTrue("Did not get the data", gotTheData); + assertEquals("Data send and data received are not the same", "test message", receivedData); + } + + @Test + public void testSendAndOnBoolean() throws Exception { + mClusterWS.connect(); + mClusterWS.on("String", new IEmitterListener() { + @Override + public void onDataReceived(Object data) { + gotTheData = true; + receivedData = data; + } + }); + Thread.sleep(1000); + mClusterWS.send("String", true); + Thread.sleep(1000); + + assertTrue("Did not get the data", gotTheData); + assertTrue("Data send and data received are not the same", (Boolean) receivedData); + } + + @Test + public void testSendAndOnInteger() throws Exception { + mClusterWS.connect(); + mClusterWS.on("String", new IEmitterListener() { + @Override + public void onDataReceived(Object data) { + gotTheData = true; + receivedData = data; + } + }); + Thread.sleep(1000); + mClusterWS.send("String", 30); + Thread.sleep(1000); + + assertTrue("Did not get the data", gotTheData); + assertEquals("Data send and data received are not the same", 30, (int) receivedData); + } + + @Test + public void testSendAndOnNull() throws Exception { + mClusterWS.connect(); + mClusterWS.on("String", new IEmitterListener() { + @Override + public void onDataReceived(Object data) { + gotTheData = true; + if (data.equals(null)) { + receivedData = null; + } + } + }); + Thread.sleep(1000); + mClusterWS.send("String", null); + Thread.sleep(1000); + + assertTrue("Did not get the data", gotTheData); + assertNull("Data send and data received are not the same", receivedData); + } + + @Test + public void testSendAndOnObject() throws Exception { + mClusterWS.connect(); + mClusterWS.on("String", new IEmitterListener() { + @Override + public void onDataReceived(Object data) { + gotTheData = true; + receivedData = data; + } + }); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("int", 30); + jsonObject.put("bool", true); + jsonObject.put("string", "CHLEN"); + JSONArray jsonArray = new JSONArray(); + jsonArray.add(30); + jsonArray.add(true); + jsonArray.add("CHLEN"); + jsonObject.put("array", jsonArray); + Thread.sleep(1000); + mClusterWS.send("String", jsonObject); + Thread.sleep(1000); + + assertTrue("Did not get the data", gotTheData); + assertEquals("Data send and data received are not the same", jsonObject.toString(), receivedData.toString()); + } + + @Test + public void testSendAndOnArray() throws Exception { + mClusterWS.connect(); + mClusterWS.on("String", new IEmitterListener() { + @Override + public void onDataReceived(Object data) { + gotTheData = true; + receivedData = data; + } + }); + ArrayList mObjectArrayList; + mObjectArrayList = new ArrayList<>(); + mObjectArrayList.add(30); + mObjectArrayList.add(false); + mObjectArrayList.add("Test message"); + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("int", 30); + jsonObject.put("bool", true); + jsonObject.put("string", "CHLEN"); + JSONArray jsonArray = new JSONArray(); + jsonArray.add(30); + jsonArray.add(true); + jsonArray.add("CHLEN"); + jsonObject.put("array", jsonArray); + + mObjectArrayList.add(jsonObject); + Thread.sleep(1000); + mClusterWS.send("String", mObjectArrayList.toString()); + Thread.sleep(1000); + + assertTrue("Did not get the data", gotTheData); + assertEquals("Data send and data received are not the same", mObjectArrayList.toString(), receivedData.toString()); + } + + @Test + public void testDisconnect() throws Exception { + mClusterWS.connect(); + Thread.sleep(1000); + mClusterWS.disconnect(null, null); + Thread.sleep(1000); + assertEquals(WebSocket.READYSTATE.CLOSED, mClusterWS.getState()); + } + + @Test + public void testAllGetStates() throws Exception { + final com.clusterws.ClusterWSStatesBool clusterWSStatesBool = new com.clusterws.ClusterWSStatesBool(); + if (mClusterWS.getState() == WebSocket.READYSTATE.NOT_YET_CONNECTED) { + clusterWSStatesBool.setNotYetConnected(true); + } + + mClusterWS.setClusterWSListener(new IClusterWSListener() { + @Override + public void onConnected() { + if (mClusterWS.getState() == WebSocket.READYSTATE.OPEN) { + clusterWSStatesBool.setOpen(true); + } + } + + @Override + public void onError(Exception exception) { + + } + + @Override + public void onDisconnected(int code, String reason) { + + } + }); + + mClusterWS.connect(); + if (mClusterWS.getState() == WebSocket.READYSTATE.CONNECTING) { + clusterWSStatesBool.setConnecting(true); + } + Thread.sleep(1000); + mClusterWS.disconnect(1000, "Test"); + if (mClusterWS.getState() == WebSocket.READYSTATE.CLOSING) { + clusterWSStatesBool.setClosing(true); + } + Thread.sleep(1000); + if (mClusterWS.getState() == WebSocket.READYSTATE.CLOSED) { + clusterWSStatesBool.setClosed(true); + } + + assertTrue("State CREATED not working", clusterWSStatesBool.isNotYetConnected()); + //TODO Сложно поймать этот state +// assertTrue("State CONNECTING not working",clusterWSStatesBool.isConnecting()); + assertTrue("State OPEN not working", clusterWSStatesBool.isOpen()); + assertTrue("State CLOSING not working", clusterWSStatesBool.isClosing()); + assertTrue("State CLOSED not working", clusterWSStatesBool.isClosed()); + } + + @Test + public void testReconnection() throws Exception{ + mClusterWS.setReconnection(true,1000,2000,null); + mClusterWS.connect(); + Thread.sleep(1000); + mClusterWS.disconnect(3002,"test"); + Thread.sleep(2000); + assertEquals("Did not reconnect", WebSocket.READYSTATE.OPEN, mClusterWS.getState()); + } + + @Test + public void testPingPong() throws Exception{ + mClusterWS.connect(); + Thread.sleep(1900); + assertEquals("Websocket disconnected", WebSocket.READYSTATE.OPEN,mClusterWS.getState()); + } + + @Test(expected = NullPointerException.class) + public void testNullUrl() throws Exception{ + mClusterWS = new ClusterWS(null); + } + +} \ No newline at end of file diff --git a/src/test/java/com/clusterws/EmitterTest.java b/src/test/java/com/clusterws/EmitterTest.java new file mode 100644 index 0000000..1d9e2df --- /dev/null +++ b/src/test/java/com/clusterws/EmitterTest.java @@ -0,0 +1,67 @@ +package com.clusterws; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class EmitterTest { + private boolean gotMessage; + private Emitter mEmitter; + + @Before + public void init(){ + mEmitter = new Emitter(); + gotMessage = false; + } + + @After + public void deleteEmitter(){ + mEmitter = null; + } + + @Test + public void addEventListenerAndEmit() throws Exception { + mEmitter.addEventListener("test", new IEmitterListener() { + @Override + public void onDataReceived(Object data) { + gotMessage = true; + } + }); + mEmitter.emit("test","HUI"); + assertTrue(gotMessage); + } + + @Test + public void addDifferentEventListenersWithTheSameEventName() throws Exception{ + mEmitter.addEventListener("test", new IEmitterListener() { + @Override + public void onDataReceived(Object data) { + gotMessage = false; + } + }); + mEmitter.addEventListener("test", new IEmitterListener() { + @Override + public void onDataReceived(Object data) { + gotMessage = true; + } + }); + mEmitter.emit("test","HUI"); + assertTrue(gotMessage); + } + + @Test + public void removeAllEvents() throws Exception { + mEmitter.addEventListener("test", new IEmitterListener() { + @Override + public void onDataReceived(Object data) { + gotMessage = true; + } + }); + mEmitter.removeAllEvents(); + mEmitter.emit("test","HUI"); + assertFalse(gotMessage); + } + +} \ No newline at end of file diff --git a/src/test/java/com/clusterws/MessageHandlerTest.java b/src/test/java/com/clusterws/MessageHandlerTest.java new file mode 100644 index 0000000..1f71680 --- /dev/null +++ b/src/test/java/com/clusterws/MessageHandlerTest.java @@ -0,0 +1,163 @@ +package com.clusterws; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; + +import static org.junit.Assert.*; + +public class MessageHandlerTest { + private MessageHandler mMessageHandler; + private String mResult; + + @Before + public void init() { + mMessageHandler = new MessageHandler(); + mResult = null; + } + + @Test + public void messageEncodeEmitString() throws Exception { + mResult = mMessageHandler.messageEncode("test", "testString", "emit"); + assertEquals("{\"#\":[\"e\",\"test\",\"testString\"]}", mResult); + } + + @Test + public void messageEncodeEmitInt() throws Exception { + mResult = mMessageHandler.messageEncode("test", 20, "emit"); + assertEquals("{\"#\":[\"e\",\"test\",20]}", mResult); + } + + @Test + public void messageEncodeEmitBoolean() throws Exception { + mResult = mMessageHandler.messageEncode("test", true, "emit"); + assertEquals("{\"#\":[\"e\",\"test\",true]}", mResult); + } + + @Test + public void messageEncodeEmitNull() throws Exception { + mResult = mMessageHandler.messageEncode("test", null, "emit"); + assertEquals("{\"#\":[\"e\",\"test\",null]}", mResult); + } + + @Test + public void messageEncodeEmitObject() throws Exception { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("int", 30); + jsonObject.put("bool", true); + jsonObject.put("string", "CHLEN"); + JSONArray jsonArray = new JSONArray(); + jsonArray.add(30); + jsonArray.add(true); + jsonArray.add("CHLEN"); + jsonObject.put("array", jsonArray); + mResult = mMessageHandler.messageEncode("test", jsonObject, "emit"); + assertEquals("{\"#\":[\"e\",\"test\",{\"bool\":true,\"string\":\"CHLEN\",\"array\":[30,true,\"CHLEN\"],\"int\":30}]}", mResult); + } + + + @Test + public void messageEncodeEmitArray() throws Exception { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("int", 30); + jsonObject.put("bool", true); + jsonObject.put("string", "CHLEN"); + JSONArray jsonArray = new JSONArray(); + jsonArray.add(30); + jsonArray.add(true); + jsonArray.add("CHLEN"); + jsonObject.put("array", jsonArray); + + ArrayList arrayList = new ArrayList<>(); + arrayList.add(30); + arrayList.add(false); + arrayList.add("Test message"); + arrayList.add(jsonObject); + mResult = mMessageHandler.messageEncode("test", arrayList, "emit"); + assertEquals("{\"#\":[\"e\",\"test\",[30,false,\"Test message\",{\"bool\":true,\"string\":\"CHLEN\",\"array\":[30,true,\"CHLEN\"],\"int\":30}]]}", mResult); + } + + @Test + public void messageEncodeSystemSubscribe() throws Exception{ + mResult = mMessageHandler.messageEncode("subscribe","channelName","system"); + assertEquals("{\"#\":[\"s\",\"s\",\"channelName\"]}",mResult); + } + + @Test + public void messageEncodeSystemUnsubscribe() throws Exception{ + mResult = mMessageHandler.messageEncode("unsubscribe","channelName","system"); + assertEquals("{\"#\":[\"s\",\"u\",\"channelName\"]}",mResult); + } + @Test + public void messageEncodePing() throws Exception{ + mResult = mMessageHandler.messageEncode("#1",null,"ping"); + assertEquals("#1",mResult); + } + + @Test + public void messageEncodePublishString(){ + mResult = mMessageHandler.messageEncode("channelname", "testData", "publish"); + assertEquals("{\"#\":[\"p\",\"channelname\",\"testData\"]}", mResult); + } + + @Test + public void messageEncodePublishInt() throws Exception { + mResult = mMessageHandler.messageEncode("channelname", 20, "publish"); + assertEquals("{\"#\":[\"p\",\"channelname\",20]}", mResult); + + } + + @Test + public void messageEncodePublishBoolean() throws Exception { + mResult = mMessageHandler.messageEncode("channelname", true, "publish"); + assertEquals("{\"#\":[\"p\",\"channelname\",true]}", mResult); + + } + + @Test + public void messageEncodePublishNull() throws Exception { + mResult = mMessageHandler.messageEncode("channelname", null, "publish"); + assertEquals("{\"#\":[\"p\",\"channelname\",null]}", mResult); + + } + + @Test + public void messageEncodePublishObject() throws Exception { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("int", 30); + jsonObject.put("bool", true); + jsonObject.put("string", "CHLEN"); + JSONArray jsonArray = new JSONArray(); + jsonArray.add(30); + jsonArray.add(true); + jsonArray.add("CHLEN"); + jsonObject.put("array", jsonArray); + mResult = mMessageHandler.messageEncode("channelname", jsonObject, "publish"); + assertEquals("{\"#\":[\"p\",\"channelname\",{\"bool\":true,\"string\":\"CHLEN\",\"array\":[30,true,\"CHLEN\"],\"int\":30}]}", mResult); + } + + + @Test + public void messageEncodePublishArray() throws Exception { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("int", 30); + jsonObject.put("bool", true); + jsonObject.put("string", "CHLEN"); + JSONArray jsonArray = new JSONArray(); + jsonArray.add(30); + jsonArray.add(true); + jsonArray.add("CHLEN"); + jsonObject.put("array", jsonArray); + + ArrayList arrayList = new ArrayList<>(); + arrayList.add(30); + arrayList.add(false); + arrayList.add("Test message"); + arrayList.add(jsonObject); + mResult = mMessageHandler.messageEncode("channelname", arrayList, "publish"); + assertEquals("{\"#\":[\"p\",\"channelname\",[30,false,\"Test message\",{\"bool\":true,\"string\":\"CHLEN\",\"array\":[30,true,\"CHLEN\"],\"int\":30}]]}", mResult); + } +} \ No newline at end of file diff --git a/src/test/java/com/clusterws/PingHandlerTest.java b/src/test/java/com/clusterws/PingHandlerTest.java new file mode 100644 index 0000000..90ee9f9 --- /dev/null +++ b/src/test/java/com/clusterws/PingHandlerTest.java @@ -0,0 +1,36 @@ +package com.clusterws; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class PingHandlerTest { + + private PingHandler mPingHandler; + + @Before + public void init(){ + mPingHandler = new PingHandler(); + } + + @Test + public void incrementMissedPing() throws Exception { + mPingHandler.incrementMissedPing(); + assertEquals(1,mPingHandler.getMissedPing()); + } + + @Test + public void setMissedPingToZero() throws Exception { + mPingHandler.incrementMissedPing(); + mPingHandler.setMissedPingToZero(); + assertEquals(0,mPingHandler.getMissedPing()); + } + + + @Test + public void getMissedPing() throws Exception { + assertEquals(0,mPingHandler.getMissedPing()); + } + +} \ No newline at end of file