Sitelet https://github.com/studentyb/java/commit/f4ceca0a9cac780cd98a9f4e3527c05b68403bd0
Skip to content

Commit f4ceca0

Browse files
committed
fix tests
1 parent 4a5d203 commit f4ceca0

7 files changed

Lines changed: 82 additions & 52 deletions

File tree

‎java/srcTest/com/pubnub/api/ChannelGroupTest.java‎

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import org.json.JSONArray;
44
import org.json.JSONException;
55
import org.json.JSONObject;
6-
import org.junit.AfterClass;
76
import org.junit.Before;
7+
import org.junit.BeforeClass;
88
import org.junit.Test;
99

10+
import java.util.UUID;
1011
import java.util.concurrent.CountDownLatch;
1112
import java.util.concurrent.TimeUnit;
1213

@@ -20,11 +21,17 @@ public class ChannelGroupTest {
2021
String channelGroup;
2122
String channelNamespace;
2223

24+
25+
@BeforeClass
26+
public static void cleanup() throws InterruptedException, JSONException {
27+
TestHelper.cleanup();
28+
}
29+
2330
@Before
2431
public void setUp() {
2532
pubnub.setOrigin("dara24.devbuild");
2633
pubnub.setCacheBusting(false);
27-
channelGroup = "jtest-" + Math.random();
34+
channelGroup = UUID.randomUUID().toString().substring(0, 8);
2835
channelNamespace = "jtest-namespace";
2936
}
3037

@@ -396,37 +403,4 @@ public void testRemoveNamespacedGroup() throws InterruptedException, JSONExcepti
396403
assertJSONArrayHasNo(group, groups);
397404
assertEquals(channelNamespace, result.getString("namespace"));
398405
}
399-
400-
@AfterClass
401-
public static void tearDown() throws InterruptedException, JSONException {
402-
Pubnub pubnub = new Pubnub("demo", "demo");
403-
pubnub.setOrigin("dara24.devbuild");
404-
pubnub.setCacheBusting(false);
405-
406-
final CountDownLatch latch1 = new CountDownLatch(1);
407-
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
408-
409-
pubnub.channelGroupListGroups(cb1);
410-
latch1.await(10, TimeUnit.SECONDS);
411-
412-
JSONObject result = (JSONObject) cb1.getResponse();
413-
JSONArray groups = result.getJSONArray("groups");
414-
415-
for (int i = 0; i < groups.length(); i++) {
416-
final String group = groups.getString(i);
417-
418-
if (group.startsWith("jtest")) {
419-
CountDownLatch latch = new CountDownLatch(1);
420-
pubnub.channelGroupRemoveGroup(group, new TestHelper.SimpleCallback(latch) {
421-
@Override
422-
public void successCallback(String channel, Object message) {
423-
System.out.println("Successfully removed group " + group);
424-
super.successCallback(channel, message);
425-
}
426-
});
427-
latch.await(10, TimeUnit.SECONDS);
428-
}
429-
430-
}
431-
}
432406
}

‎java/srcTest/com/pubnub/api/HereNowTest.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.Before;
77
import org.junit.Test;
88

9+
import java.util.UUID;
910
import java.util.concurrent.CountDownLatch;
1011
import java.util.concurrent.TimeUnit;
1112

@@ -15,13 +16,12 @@
1516
public class HereNowTest {
1617
Pubnub pubnub;
1718
String group;
18-
String auth_key;
1919

20-
double random;
20+
String random;
2121

2222
@Before
2323
public void setUp() throws InterruptedException {
24-
random = Math.random();
24+
random = UUID.randomUUID().toString().substring(0, 8);
2525

2626
pubnub = new Pubnub("demo", "demo", "demo");
2727
pubnub.setOrigin("dara24.devbuild");

‎java/srcTest/com/pubnub/api/NamespaceTest.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.Before;
77
import org.junit.Test;
88

9+
import java.util.UUID;
910
import java.util.concurrent.CountDownLatch;
1011
import java.util.concurrent.TimeUnit;
1112

@@ -16,14 +17,14 @@
1617

1718
public class NamespaceTest {
1819
Pubnub pubnub = new Pubnub("demo", "demo");
19-
double random;
20+
String random;
2021

2122
@Before
2223
public void setUp() {
2324
pubnub.setOrigin("dara24.devbuild");
2425
pubnub.setCacheBusting(false);
2526

26-
random = Math.random();
27+
random = UUID.randomUUID().toString().substring(0, 8);
2728
}
2829

2930
@Test

‎java/srcTest/com/pubnub/api/StateTest.java‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.junit.Before;
66
import org.junit.Test;
77

8+
import java.util.UUID;
89
import java.util.concurrent.CountDownLatch;
910
import java.util.concurrent.TimeUnit;
1011

@@ -14,13 +15,12 @@ public class StateTest {
1415
Pubnub pubnub;
1516
String channel;
1617
String group;
17-
String auth_key;
1818

19-
double random;
19+
String random;
2020

2121
@Before
2222
public void setUp() throws InterruptedException {
23-
random = Math.random();
23+
random = UUID.randomUUID().toString().substring(0, 8);
2424

2525
pubnub = new Pubnub("demo", "demo");
2626
pubnub.setOrigin("dara24.devbuild");
@@ -74,12 +74,10 @@ public void testStateForChannelGroup()
7474
state.put("age", 32);
7575

7676
final CountDownLatch latch1 = new CountDownLatch(1);
77-
final CountDownLatch latch2 = new CountDownLatch(1);
7877
final CountDownLatch latch3 = new CountDownLatch(1);
7978
final CountDownLatch latch4 = new CountDownLatch(1);
8079

8180
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
82-
final TestHelper.SubscribeCallback cb2 = new TestHelper.SubscribeCallback(latch2);
8381
final TestHelper.SimpleCallback cb3 = new TestHelper.SimpleCallback(latch3);
8482
final TestHelper.SimpleCallback cb4 = new TestHelper.SimpleCallback(latch4);
8583

@@ -88,9 +86,6 @@ public void testStateForChannelGroup()
8886
pubnub.channelGroupAddChannel(group, channel, cb1);
8987
latch1.await(10, TimeUnit.SECONDS);
9088

91-
pubnub.channelGroupSubscribe(group, cb2);
92-
latch2.await(10, TimeUnit.SECONDS);
93-
9489
pubnub.channelGroupSetState(group, pubnub.getUUID(), state, cb3);
9590
latch3.await(10, TimeUnit.SECONDS);
9691

‎java/srcTest/com/pubnub/api/SubscribeGroupTest.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.junit.rules.ExpectedException;
55

66
import java.util.Hashtable;
7+
import java.util.UUID;
78
import java.util.concurrent.CountDownLatch;
89
import java.util.concurrent.TimeUnit;
910

@@ -12,7 +13,7 @@
1213

1314
public class SubscribeGroupTest {
1415
Pubnub pubnub = new Pubnub("demo", "demo");
15-
double random;
16+
String random;
1617

1718
@Rule
1819
public ExpectedException thrown = ExpectedException.none();
@@ -27,7 +28,7 @@ public void setUp() throws PubnubException {
2728
pubnub.channelGroupRemoveGroup("jtest2", new TestHelper.SimpleCallback());
2829
pubnub.channelGroupRemoveGroup("jtest3", new TestHelper.SimpleCallback());
2930

30-
random = Math.random();
31+
random = UUID.randomUUID().toString().substring(0, 8);
3132
}
3233

3334
@Test

‎java/srcTest/com/pubnub/api/TestHelper.java‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.pubnub.api;
22

3+
4+
import org.json.JSONArray;
35
import org.json.JSONException;
46
import org.json.JSONObject;
57

68
import java.util.concurrent.CountDownLatch;
9+
import java.util.concurrent.TimeUnit;
710

811
public class TestHelper {
912
static class SimpleCallback extends Callback {
@@ -109,4 +112,59 @@ public void errorCallback(String channel, PubnubError error) {
109112
}
110113

111114
}
115+
116+
public static void cleanup() throws InterruptedException, JSONException {
117+
Pubnub pubnub = new Pubnub("demo", "demo");
118+
pubnub.setOrigin("dara24.devbuild");
119+
pubnub.setCacheBusting(false);
120+
121+
final CountDownLatch latch1 = new CountDownLatch(1);
122+
final CountDownLatch latch2 = new CountDownLatch(1);
123+
final TestHelper.SimpleCallback cb1 = new TestHelper.SimpleCallback(latch1);
124+
final TestHelper.SimpleCallback cb2 = new TestHelper.SimpleCallback(latch2);
125+
126+
pubnub.channelGroupListGroups(cb1);
127+
latch1.await(10, TimeUnit.SECONDS);
128+
129+
JSONObject result = (JSONObject) cb1.getResponse();
130+
JSONArray groups = result.getJSONArray("groups");
131+
132+
for (int i = 0; i < groups.length(); i++) {
133+
final String group = groups.getString(i);
134+
135+
if (group.startsWith("jtest")) {
136+
CountDownLatch latch = new CountDownLatch(1);
137+
pubnub.channelGroupRemoveGroup(group, new TestHelper.SimpleCallback(latch) {
138+
@Override
139+
public void successCallback(String channel, Object message) {
140+
System.out.println("Successfully removed group " + group);
141+
super.successCallback(channel, message);
142+
}
143+
});
144+
latch.await(10, TimeUnit.SECONDS);
145+
}
146+
}
147+
148+
pubnub.channelGroupListNamespaces(cb2);
149+
latch2.await(10, TimeUnit.SECONDS);
150+
151+
result = (JSONObject) cb2.getResponse();
152+
JSONArray namespaces = result.getJSONArray("namespaces");
153+
154+
for (int i = 0; i < namespaces.length(); i++) {
155+
final String namespace = namespaces.getString(i);
156+
157+
if (namespace.startsWith("jtest")) {
158+
CountDownLatch latch = new CountDownLatch(1);
159+
pubnub.channelGroupRemoveNamespace(namespace, new TestHelper.SimpleCallback(latch) {
160+
@Override
161+
public void successCallback(String channel, Object message) {
162+
System.out.println("Successfully removed namespace " + namespace);
163+
super.successCallback(channel, message);
164+
}
165+
});
166+
latch.await(10, TimeUnit.SECONDS);
167+
}
168+
}
169+
}
112170
}

‎java/srcTest/com/pubnub/api/UnsubscribeTest.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.junit.Test;
99
import org.junit.rules.ExpectedException;
1010

11+
import java.util.UUID;
1112
import java.util.concurrent.CountDownLatch;
1213
import java.util.concurrent.TimeUnit;
1314

@@ -21,7 +22,7 @@ public class UnsubscribeTest {
2122
String channel;
2223
String group;
2324

24-
double random;
25+
String random;
2526

2627
@Rule
2728
public ExpectedException thrown = ExpectedException.none();
@@ -36,7 +37,7 @@ public void setUp() {
3637
pubnub2.setOrigin("dara24.devbuild");
3738
pubnub2.setCacheBusting(false);
3839

39-
random = Math.random();
40+
random = UUID.randomUUID().toString().substring(0, 8);
4041
channel = "ch6-" + random;
4142
group = "jtest-" + random;
4243
}

0 commit comments

Comments
 (0)