This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for ClusterWS, replaced Options class with mUrl variable …
…and added test for null pointer exception when url is null
- Loading branch information
Showing
8 changed files
with
839 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,4 @@ | |
/build | ||
*.iml | ||
/.gradle | ||
/src/test | ||
/out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Object> 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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Channel> 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); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.