fix #10 use org.json 20090211
This commit is contained in:
@@ -13,7 +13,7 @@ import static org.junit.Assert.assertTrue;
|
||||
public class HasBinaryDataTest {
|
||||
|
||||
@Test
|
||||
public void arrayContainsByteArray() {
|
||||
public void arrayContainsByteArray() throws Exception {
|
||||
JSONArray arr = new JSONArray("[1, null, 2]");
|
||||
arr.put(1, "asdfasdf".getBytes(Charset.forName("UTF-8")));
|
||||
assertTrue(HasBinaryData.hasBinary(arr));
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.github.nkzawa.socketio.client;
|
||||
|
||||
import com.github.nkzawa.emitter.Emitter;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -57,7 +58,11 @@ public class ConnectionTest extends Connection {
|
||||
public void call(Object... args) {
|
||||
Ack fn = (Ack) args[0];
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("test", true);
|
||||
try {
|
||||
data.put("test", true);
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
fn.call(5, data);
|
||||
}
|
||||
});
|
||||
@@ -65,9 +70,13 @@ public class ConnectionTest extends Connection {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
JSONObject data = (JSONObject)args[1];
|
||||
if ((Integer)args[0] == 5 && data.getBoolean("test")) {
|
||||
socket.close();
|
||||
latch.countDown();
|
||||
try {
|
||||
if ((Integer)args[0] == 5 && data.getBoolean("test")) {
|
||||
socket.close();
|
||||
latch.countDown();
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -84,14 +93,18 @@ public class ConnectionTest extends Connection {
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
socket.emit("getAckDate", new JSONObject("{test: true}"), new Ack() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
assertThat(args[0], instanceOf(String.class));
|
||||
socket.close();
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
try {
|
||||
socket.emit("getAckDate", new JSONObject("{test: true}"), new Ack() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
assertThat(args[0], instanceOf(String.class));
|
||||
socket.close();
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
socket.connect();
|
||||
@@ -329,13 +342,21 @@ public class ConnectionTest extends Connection {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("date", new Date());
|
||||
try {
|
||||
data.put("date", new Date());
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
socket.emit("echo", data);
|
||||
socket.on("echoBack", new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
assertThat(args[0], instanceOf(JSONObject.class));
|
||||
assertThat(((JSONObject)args[0]).get("date"), instanceOf(String.class));
|
||||
try {
|
||||
assertThat(((JSONObject)args[0]).get("date"), instanceOf(String.class));
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
socket.close();
|
||||
latch.countDown();
|
||||
}
|
||||
@@ -379,17 +400,25 @@ public class ConnectionTest extends Connection {
|
||||
public void call(Object... args) {
|
||||
final byte[] buf = "howdy".getBytes(Charset.forName("UTF-8"));
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("hello", "lol");
|
||||
data.put("message", buf);
|
||||
data.put("goodbye", "gotcha");
|
||||
try {
|
||||
data.put("hello", "lol");
|
||||
data.put("message", buf);
|
||||
data.put("goodbye", "gotcha");
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
socket.emit("echo", data);
|
||||
socket.on("echoBack", new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
JSONObject a = (JSONObject)args[0];
|
||||
assertThat(a.getString("hello"), is("lol"));
|
||||
assertThat((byte[])a.get("message"), is(buf));
|
||||
assertThat(a.getString("goodbye"), is("gotcha"));
|
||||
try {
|
||||
assertThat(a.getString("hello"), is("lol"));
|
||||
assertThat((byte[])a.get("message"), is(buf));
|
||||
assertThat(a.getString("goodbye"), is("gotcha"));
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
socket.close();
|
||||
latch.countDown();
|
||||
}
|
||||
@@ -401,7 +430,7 @@ public class ConnectionTest extends Connection {
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void sendEventsWithByteArraysInTheCorrectOrder() throws URISyntaxException, InterruptedException {
|
||||
public void sendEventsWithByteArraysInTheCorrectOrder() throws Exception {
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
socket = client();
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
|
||||
@@ -72,7 +72,7 @@ public class ServerConnectionTest extends Connection {
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void event() throws URISyntaxException, InterruptedException {
|
||||
public void event() throws Exception {
|
||||
final Semaphore semaphore = new Semaphore(0);
|
||||
|
||||
final JSONObject obj = new JSONObject();
|
||||
@@ -102,7 +102,7 @@ public class ServerConnectionTest extends Connection {
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void ack() throws URISyntaxException, InterruptedException {
|
||||
public void ack() throws Exception {
|
||||
final Semaphore semaphore = new Semaphore(0);
|
||||
|
||||
final JSONObject obj = new JSONObject();
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.github.nkzawa.socketio.parser;
|
||||
|
||||
import com.github.nkzawa.emitter.Emitter;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -36,7 +37,7 @@ public class ByteArrayTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeByteArrayDeepInJson() {
|
||||
public void encodeByteArrayDeepInJson() throws JSONException {
|
||||
JSONObject data = new JSONObject("{a: \"hi\", b: {}, c: {a: \"bye\", b: {}}}");
|
||||
data.getJSONObject("b").put("why", new byte[3]);
|
||||
data.getJSONObject("c").getJSONObject("b").put("a", new byte[6]);
|
||||
@@ -49,7 +50,7 @@ public class ByteArrayTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeDeepBinaryJSONWithNullValue() {
|
||||
public void encodeDeepBinaryJSONWithNullValue() throws JSONException {
|
||||
JSONObject data = new JSONObject("{a: \"b\", c: 4, e: {g: null}, h: null}");
|
||||
data.put("h", new byte[9]);
|
||||
|
||||
@@ -61,7 +62,7 @@ public class ByteArrayTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeBinaryAckWithByteArray() {
|
||||
public void encodeBinaryAckWithByteArray() throws JSONException {
|
||||
JSONArray data = new JSONArray("[a, null, {}]");
|
||||
data.put(1, "xxx".getBytes(Charset.forName("UTF-8")));
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.github.nkzawa.socketio.parser;
|
||||
|
||||
import com.github.nkzawa.emitter.Emitter;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
@@ -66,9 +67,17 @@ public class Helpers {
|
||||
assertThat(actual.attachments, is(expected.attachments));
|
||||
|
||||
if (expected.data instanceof JSONArray) {
|
||||
JSONAssert.assertEquals((JSONArray)expected.data, (JSONArray)actual.data, true);
|
||||
try {
|
||||
JSONAssert.assertEquals((JSONArray)expected.data, (JSONArray)actual.data, true);
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
} else if (expected.data instanceof JSONObject) {
|
||||
JSONAssert.assertEquals((JSONObject)expected.data, (JSONObject)actual.data, true);
|
||||
try {
|
||||
JSONAssert.assertEquals((JSONObject)expected.data, (JSONObject)actual.data, true);
|
||||
} catch (JSONException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
} else {
|
||||
assertThat(actual.data, is(expected.data));
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.github.nkzawa.socketio.parser;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
@@ -26,7 +27,7 @@ public class ParserTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeEvent() {
|
||||
public void encodeEvent() throws JSONException {
|
||||
Packet<JSONArray> packet1 = new Packet<JSONArray>(Parser.EVENT);
|
||||
packet1.data = new JSONArray("[\"a\", 1, {}]");
|
||||
packet1.nsp = "/";
|
||||
@@ -39,7 +40,7 @@ public class ParserTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeAck() {
|
||||
public void encodeAck() throws JSONException {
|
||||
Packet<JSONArray> packet = new Packet<JSONArray>(Parser.ACK);
|
||||
packet.data = new JSONArray("[\"a\", 1, {}]");
|
||||
packet.id = 123;
|
||||
|
||||
Reference in New Issue
Block a user