use org.json instead of Gson
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package com.github.nkzawa.socketio.client;
|
||||
|
||||
import com.github.nkzawa.emitter.Emitter;
|
||||
import com.google.gson.JsonObject;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -15,6 +15,7 @@ import java.net.URISyntaxException;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
@@ -136,10 +137,10 @@ public class ServerConnectionTest {
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void event() throws URISyntaxException, InterruptedException {
|
||||
final BlockingQueue<Object[]> events = new LinkedBlockingQueue<Object[]>();
|
||||
final Semaphore semaphore = new Semaphore(0);
|
||||
|
||||
final JsonObject obj = new JsonObject();
|
||||
obj.addProperty("foo", 1);
|
||||
final JSONObject obj = new JSONObject();
|
||||
obj.put("foo", 1);
|
||||
|
||||
socket = client();
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
@@ -150,23 +151,26 @@ public class ServerConnectionTest {
|
||||
}
|
||||
}).on("echoBack", new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
System.out.println(String.format("echoBack: %s, %s, %s", objects));
|
||||
events.offer(objects);
|
||||
public void call(Object... args) {
|
||||
System.out.println(String.format("echoBack: %s, %s, %s", args));
|
||||
assertThat(args.length, is(3));
|
||||
assertThat(args[0].toString(), is(obj.toString()));
|
||||
assertThat(args[1], is(nullValue()));
|
||||
assertThat((String)args[2], is("bar"));
|
||||
socket.disconnect();
|
||||
semaphore.release();
|
||||
}
|
||||
});
|
||||
socket.connect();
|
||||
|
||||
assertThat(events.take(), is(new Object[] {obj, null, "bar"}));
|
||||
socket.disconnect();
|
||||
semaphore.acquire();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void ack() throws URISyntaxException, InterruptedException {
|
||||
final BlockingQueue<Object[]> events = new LinkedBlockingQueue<Object[]>();
|
||||
final Semaphore semaphore = new Semaphore(0);
|
||||
|
||||
final JsonObject obj = new JsonObject();
|
||||
obj.addProperty("foo", 1);
|
||||
final JSONObject obj = new JSONObject();
|
||||
obj.put("foo", 1);
|
||||
|
||||
socket = client();
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
@@ -177,15 +181,17 @@ public class ServerConnectionTest {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
System.out.println(String.format("ack: %s, %s", args));
|
||||
events.offer(args);
|
||||
assertThat(args.length, is(2));
|
||||
assertThat(args[0].toString(), is(obj.toString()));
|
||||
assertThat((String)args[1], is("bar"));
|
||||
socket.disconnect();
|
||||
semaphore.release();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
socket.connect();
|
||||
|
||||
assertThat(events.take(), is(new Object[] {obj, "bar"}));
|
||||
socket.disconnect();
|
||||
semaphore.acquire();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.github.nkzawa.socketio.parser;
|
||||
|
||||
import com.github.nkzawa.emitter.Emitter;
|
||||
import com.google.gson.JsonParser;
|
||||
import org.json.JSONTokener;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
@@ -32,12 +32,12 @@ public class ParserTest {
|
||||
@Test
|
||||
public void event() {
|
||||
Packet packet1 = new Packet(Parser.EVENT);
|
||||
packet1.data = new JsonParser().parse("[\"a\", 1, {}]");
|
||||
packet1.data = new JSONTokener("[\"a\", 1, {}]").nextValue();
|
||||
packet1.nsp = "/";
|
||||
test(packet1);
|
||||
|
||||
Packet packet2 = new Packet(Parser.EVENT);
|
||||
packet2.data = new JsonParser().parse("[\"a\", 1, {}]");
|
||||
packet2.data = new JSONTokener("[\"a\", 1, {}]").nextValue();
|
||||
packet2.nsp = "/test";
|
||||
test(packet2);
|
||||
}
|
||||
@@ -45,7 +45,7 @@ public class ParserTest {
|
||||
@Test
|
||||
public void ack() {
|
||||
Packet packet = new Packet(Parser.ACK);
|
||||
packet.data = new JsonParser().parse("[\"a\", 1, {}]");
|
||||
packet.data = new JSONTokener("[\"a\", 1, {}]").nextValue();
|
||||
packet.id = 123;
|
||||
packet.nsp = "/";
|
||||
test(packet);
|
||||
@@ -62,7 +62,11 @@ public class ParserTest {
|
||||
Packet packet = (Packet)args[0];
|
||||
assertThat(packet.type, is(obj.type));
|
||||
assertThat(packet.id, is(obj.id));
|
||||
assertThat(packet.data, is(obj.data));
|
||||
if (packet.data == null) {
|
||||
assertThat(packet.data, is(obj.data));
|
||||
} else {
|
||||
assertThat(packet.data.toString(), is(obj.data.toString()));
|
||||
}
|
||||
assertThat(packet.nsp, is(obj.nsp));
|
||||
assertThat(packet.attachments, is(obj.attachments));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user