Skip to content
This repository was archived by the owner on Nov 2, 2020. It is now read-only.

Commit 70c571b

Browse files
committed
Added tests for ClusterWS, replaced Options class with mUrl variable and added test for null pointer exception when url is null
1 parent 1496cf4 commit 70c571b

File tree

8 files changed

+839
-1
lines changed

8 files changed

+839
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
/build
33
*.iml
44
/.gradle
5-
/src/test
65
/out
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
package com.clusterws;
2+
3+
import com.alibaba.fastjson.JSONArray;
4+
import com.alibaba.fastjson.JSONObject;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import java.util.ArrayList;
9+
10+
import static org.junit.Assert.*;
11+
12+
public class ChannelServerTest {
13+
private ClusterWS mClusterWS;
14+
private boolean mGotTheData;
15+
private Object mReceivedData;
16+
17+
@Before
18+
public void init() throws Exception {
19+
mClusterWS = new ClusterWS("ws://localhost:80");
20+
mGotTheData = false;
21+
mReceivedData = null;
22+
}
23+
24+
@Test
25+
public void testChannelPublishAndWatchInt() throws Exception {
26+
mClusterWS.connect();
27+
Thread.sleep(1000);
28+
mClusterWS.subscribe("testChannel")
29+
.watch(new Channel.IChannelListener() {
30+
@Override
31+
public void onDataReceived(String channelName, Object data) {
32+
mGotTheData = true;
33+
mReceivedData = data;
34+
}
35+
})
36+
.publish(24);
37+
Thread.sleep(1000);
38+
39+
assertTrue("Did not get the data", mGotTheData);
40+
assertEquals("Data send and data received are not the same", 24, mReceivedData);
41+
}
42+
43+
@Test
44+
public void testChannelPublishAndWatchString() throws Exception {
45+
mClusterWS.connect();
46+
Thread.sleep(1000);
47+
mClusterWS.subscribe("testChannel")
48+
.watch(new Channel.IChannelListener() {
49+
@Override
50+
public void onDataReceived(String channelName, Object data) {
51+
mGotTheData = true;
52+
mReceivedData = data;
53+
}
54+
})
55+
.publish("test string");
56+
Thread.sleep(1000);
57+
58+
assertTrue("Did not get the data", mGotTheData);
59+
assertEquals("Data send and data received are not the same", "test string", mReceivedData);
60+
}
61+
62+
@Test
63+
public void testChannelPublishAndWatchObject() throws Exception {
64+
JSONObject jsonObject = new JSONObject();
65+
jsonObject.put("int", 30);
66+
jsonObject.put("bool", true);
67+
jsonObject.put("string", "CHLEN");
68+
JSONArray jsonArray = new JSONArray();
69+
jsonArray.add(30);
70+
jsonArray.add(true);
71+
jsonArray.add("CHLEN");
72+
jsonObject.put("array", jsonArray);
73+
74+
mClusterWS.connect();
75+
Thread.sleep(1000);
76+
mClusterWS.subscribe("testChannel")
77+
.watch(new Channel.IChannelListener() {
78+
@Override
79+
public void onDataReceived(String channelName, Object data) {
80+
mGotTheData = true;
81+
mReceivedData = data;
82+
}
83+
})
84+
.publish(jsonObject);
85+
Thread.sleep(1000);
86+
87+
assertTrue("Did not get the data", mGotTheData);
88+
assertEquals("Data send and data received are not the same", jsonObject, mReceivedData);
89+
}
90+
91+
@Test
92+
public void testChannelPublishAndWatchNull() throws Exception {
93+
mClusterWS.connect();
94+
Thread.sleep(1000);
95+
mClusterWS.subscribe("testChannel")
96+
.watch(new Channel.IChannelListener() {
97+
@Override
98+
public void onDataReceived(String channelName, Object data) {
99+
mGotTheData = true;
100+
mReceivedData = data;
101+
}
102+
})
103+
.publish(null);
104+
Thread.sleep(1000);
105+
106+
assertTrue("Did not get the data", mGotTheData);
107+
assertEquals("Data send and data received are not the same", null, mReceivedData);
108+
}
109+
110+
@Test
111+
public void testChannelPublishAndWatchArray() throws Exception {
112+
ArrayList<Object> mObjectArrayList;
113+
mObjectArrayList = new ArrayList<>();
114+
mObjectArrayList.add(30);
115+
mObjectArrayList.add(false);
116+
mObjectArrayList.add("Test message");
117+
118+
JSONObject jsonObject = new JSONObject();
119+
jsonObject.put("int", 30);
120+
jsonObject.put("bool", true);
121+
jsonObject.put("string", "CHLEN");
122+
JSONArray jsonArray = new JSONArray();
123+
jsonArray.add(30);
124+
jsonArray.add(true);
125+
jsonArray.add("CHLEN");
126+
jsonObject.put("array", jsonArray);
127+
128+
mObjectArrayList.add(jsonObject);
129+
130+
mClusterWS.connect();
131+
Thread.sleep(1000);
132+
mClusterWS.subscribe("testChannel")
133+
.watch(new Channel.IChannelListener() {
134+
@Override
135+
public void onDataReceived(String channelName, Object data) {
136+
mGotTheData = true;
137+
mReceivedData = data;
138+
}
139+
})
140+
.publish(mObjectArrayList);
141+
Thread.sleep(1000);
142+
143+
assertTrue("Did not get the data", mGotTheData);
144+
assertEquals("Data send and data received are not the same", mObjectArrayList, mReceivedData);
145+
}
146+
147+
@Test
148+
public void testGetChannelByName() throws Exception {
149+
mClusterWS.connect();
150+
Thread.sleep(1000);
151+
mClusterWS.subscribe("testChannel");
152+
assertNotNull(mClusterWS.getChannelByName("testChannel"));
153+
}
154+
155+
@Test
156+
public void testGetChannelList() throws Exception {
157+
mClusterWS.connect();
158+
Thread.sleep(1000);
159+
mClusterWS.subscribe("testChannel1");
160+
mClusterWS.subscribe("testChannel2");
161+
mClusterWS.subscribe("testChannel3");
162+
163+
assertEquals(mClusterWS.getChannels().size(), 3);
164+
assertEquals(mClusterWS.getChannels().get(0).getChannelName(), "testChannel1");
165+
assertEquals(mClusterWS.getChannels().get(1).getChannelName(), "testChannel2");
166+
assertEquals(mClusterWS.getChannels().get(2).getChannelName(), "testChannel3");
167+
}
168+
169+
@Test
170+
public void testTryGetChannelByNameAfterUnsubscribing() throws Exception {
171+
mClusterWS.connect();
172+
Thread.sleep(1000);
173+
Channel channel = mClusterWS.subscribe("testChannel1");
174+
channel.unsubscribe();
175+
assertNull(mClusterWS.getChannelByName("testChannel1"));
176+
}
177+
178+
@Test
179+
public void testResubscribeOnAllChannelsAfterReconnection() throws Exception {
180+
mClusterWS.setReconnection(true, 1000, 2000, null);
181+
mClusterWS.connect();
182+
Thread.sleep(1000);
183+
Channel channel = mClusterWS.subscribe("testChannel")
184+
.watch(new Channel.IChannelListener() {
185+
@Override
186+
public void onDataReceived(String channelName, Object data) {
187+
mGotTheData = true;
188+
mReceivedData = data;
189+
}
190+
});
191+
mClusterWS.disconnect(4001, "hui");
192+
Thread.sleep(1000);
193+
channel.publish("testData");
194+
Thread.sleep(1000);
195+
assertTrue("Did not get the data", mGotTheData);
196+
assertEquals("Data send and data received are not the same", "testData", mReceivedData);
197+
}
198+
199+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.clusterws;
2+
3+
import org.junit.Before;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.mockito.Mock;
7+
import org.mockito.junit.MockitoJUnit;
8+
import org.mockito.junit.MockitoRule;
9+
10+
import java.util.ArrayList;
11+
12+
import static org.junit.Assert.*;
13+
import static org.mockito.Mockito.when;
14+
15+
public class ChannelTest {
16+
private Channel mChannel;
17+
private static final String CHANNEL_NAME = "testChannel";
18+
private static final String TEST_STRING_DATA = "testData";
19+
20+
@Mock
21+
private ClusterWS mClusterWS;
22+
23+
@Rule
24+
public MockitoRule mockitoRule = MockitoJUnit.rule();
25+
26+
@Before
27+
public void init(){
28+
mChannel = new Channel(CHANNEL_NAME,mClusterWS);
29+
}
30+
31+
@Test
32+
public void watchAndOnMessage() throws Exception {
33+
mChannel.watch(new Channel.IChannelListener() {
34+
@Override
35+
public void onDataReceived(String channelName, Object data) {
36+
assertEquals("Data is not the same",TEST_STRING_DATA,data);
37+
}
38+
});
39+
mChannel.onMessage(TEST_STRING_DATA);
40+
}
41+
42+
43+
@Test
44+
public void unsubscribe() throws Exception {
45+
ArrayList<Channel> channelArrayList = new ArrayList<>();
46+
channelArrayList.add(mChannel);
47+
channelArrayList.add(new Channel("GAY",mClusterWS));
48+
channelArrayList.add(new Channel("SHIT",mClusterWS));
49+
when(mClusterWS.getChannels()).thenReturn(channelArrayList);
50+
mChannel.unsubscribe();
51+
assertNotEquals("There is an old channel",CHANNEL_NAME,channelArrayList.get(0).getChannelName());
52+
}
53+
54+
@Test
55+
public void getChannelName() throws Exception {
56+
assertEquals(mChannel.getChannelName(),CHANNEL_NAME);
57+
}
58+
59+
60+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.clusterws;
2+
3+
public class ClusterWSStatesBool {
4+
private boolean mClosed;
5+
private boolean mClosing;
6+
private boolean mNotYetConnected;
7+
private boolean mConnecting;
8+
private boolean mOpen;
9+
10+
public ClusterWSStatesBool() {
11+
mClosed = false;
12+
mClosing = false;
13+
mNotYetConnected = false;
14+
mConnecting = false;
15+
mOpen = false;
16+
}
17+
18+
public boolean isClosed() {
19+
return mClosed;
20+
}
21+
22+
public void setClosed(boolean closed) {
23+
mClosed = closed;
24+
}
25+
26+
public boolean isClosing() {
27+
return mClosing;
28+
}
29+
30+
public void setClosing(boolean closing) {
31+
mClosing = closing;
32+
}
33+
34+
public boolean isNotYetConnected() {
35+
return mNotYetConnected;
36+
}
37+
38+
public void setNotYetConnected(boolean notYetConnected) {
39+
mNotYetConnected = notYetConnected;
40+
}
41+
42+
public boolean isConnecting() {
43+
return mConnecting;
44+
}
45+
46+
public void setConnecting(boolean connecting) {
47+
mConnecting = connecting;
48+
}
49+
50+
public boolean isOpen() {
51+
return mOpen;
52+
}
53+
54+
public void setOpen(boolean open) {
55+
mOpen = open;
56+
}
57+
}

0 commit comments

Comments
 (0)