fix #10 use org.json 20090211

This commit is contained in:
Naoyuki Kanezawa
2014-07-09 04:11:18 +09:00
parent d15d013e11
commit 85591d23cd
10 changed files with 131 additions and 66 deletions

View File

@@ -57,12 +57,12 @@
<dependency> <dependency>
<groupId>com.github.nkzawa</groupId> <groupId>com.github.nkzawa</groupId>
<artifactId>engine.io-client</artifactId> <artifactId>engine.io-client</artifactId>
<version>0.2.1</version> <version>0.2.2-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.json</groupId> <groupId>org.json</groupId>
<artifactId>json</artifactId> <artifactId>json</artifactId>
<version>20140107</version> <version>20090211</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>

View File

@@ -1,6 +1,7 @@
package com.github.nkzawa.hasbinarydata; package com.github.nkzawa.hasbinarydata;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.Iterator; import java.util.Iterator;
@@ -24,7 +25,13 @@ public class HasBinaryData {
JSONArray _obj = (JSONArray)obj; JSONArray _obj = (JSONArray)obj;
int length = _obj.length(); int length = _obj.length();
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
if (recursiveCheckForBinary(_obj.isNull(i) ? null : _obj.get(i))) { Object v;
try {
v = _obj.isNull(i) ? null : _obj.get(i);
} catch (JSONException e) {
return false;
}
if (recursiveCheckForBinary(v)) {
return true; return true;
} }
} }
@@ -33,7 +40,13 @@ public class HasBinaryData {
Iterator keys = _obj.keys(); Iterator keys = _obj.keys();
while (keys.hasNext()) { while (keys.hasNext()) {
String key = (String)keys.next(); String key = (String)keys.next();
if (recursiveCheckForBinary(_obj.get(key))) { Object v;
try {
v = _obj.get(key);
} catch (JSONException e) {
return false;
}
if (recursiveCheckForBinary(v)) {
return true; return true;
} }
} }

View File

@@ -47,18 +47,6 @@ public class Socket extends Emitter {
put(EVENT_ERROR, 1); put(EVENT_ERROR, 1);
}}; }};
private static boolean jsonArrayNativeRemove;
{
try {
jsonArrayNativeRemove = JSONArray.class.getMethod("remove", new Class[] {
int.class
}) != null;
} catch (NoSuchMethodException e) {
jsonArrayNativeRemove = false;
}
}
private boolean connected; private boolean connected;
private boolean disconnected = true; private boolean disconnected = true;
private int ids; private int ids;
@@ -170,12 +158,8 @@ public class Socket extends Emitter {
if (_args.get(_args.size() - 1) instanceof Ack) { if (_args.get(_args.size() - 1) instanceof Ack) {
logger.fine(String.format("emitting packet with ack id %d", Socket.this.ids)); logger.fine(String.format("emitting packet with ack id %d", Socket.this.ids));
Socket.this.acks.put(Socket.this.ids, (Ack)_args.remove(_args.size() - 1)); Socket.this.acks.put(Socket.this.ids, (Ack)_args.remove(_args.size() - 1));
if (jsonArrayNativeRemove) { jsonArgs = remove(jsonArgs, jsonArgs.length() - 1);
jsonArgs.remove(jsonArgs.length() - 1); packet.data = jsonArgs;
} else {
jsonArgs = remove(jsonArgs, jsonArgs.length() - 1);
packet.data = jsonArgs;
}
packet.id = Socket.this.ids++; packet.id = Socket.this.ids++;
} }
@@ -187,14 +171,16 @@ public class Socket extends Emitter {
private static JSONArray remove(JSONArray a, int pos) { private static JSONArray remove(JSONArray a, int pos) {
JSONArray na = new JSONArray(); JSONArray na = new JSONArray();
try { for (int i = 0; i < a.length(); i++){
for (int i = 0; i < a.length(); i++){ if (i != pos) {
if (i != pos) { Object v;
na.put(a.get(i)); try {
v = a.get(i);
} catch (JSONException e) {
v = null;
} }
na.put(v);
} }
} catch (Exception e) {
throw new JSONException(e);
} }
return na; return na;
} }
@@ -392,7 +378,12 @@ public class Socket extends Emitter {
int length = array.length(); int length = array.length();
Object[] data = new Object[length]; Object[] data = new Object[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
Object v = array.get(i); Object v;
try {
v = array.get(i);
} catch (JSONException e) {
v = null;
}
data[i] = v == JSONObject.NULL ? null : v; data[i] = v == JSONObject.NULL ? null : v;
} }
return data; return data;

View File

@@ -1,6 +1,7 @@
package com.github.nkzawa.socketio.parser; package com.github.nkzawa.socketio.parser;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
@@ -31,8 +32,12 @@ public class Binary {
if (data instanceof byte[]) { if (data instanceof byte[]) {
JSONObject placeholder = new JSONObject(); JSONObject placeholder = new JSONObject();
placeholder.put(KEY_PLACEHOLDER, true); try {
placeholder.put(KEY_NUM, buffers.size()); placeholder.put(KEY_PLACEHOLDER, true);
placeholder.put(KEY_NUM, buffers.size());
} catch (JSONException e) {
return null;
}
buffers.add((byte[])data); buffers.add((byte[])data);
return placeholder; return placeholder;
} else if (data instanceof JSONArray) { } else if (data instanceof JSONArray) {
@@ -40,7 +45,11 @@ public class Binary {
JSONArray _data = (JSONArray)data; JSONArray _data = (JSONArray)data;
int len = _data.length(); int len = _data.length();
for (int i = 0; i < len; i ++) { for (int i = 0; i < len; i ++) {
newData.put(i, _deconstructPacket(_data.get(i), buffers)); try {
newData.put(i, _deconstructPacket(_data.get(i), buffers));
} catch (JSONException e) {
return null;
}
} }
return newData; return newData;
} else if (data instanceof JSONObject) { } else if (data instanceof JSONObject) {
@@ -49,7 +58,11 @@ public class Binary {
Iterator<?> iterator = _data.keys(); Iterator<?> iterator = _data.keys();
while (iterator.hasNext()) { while (iterator.hasNext()) {
String key = (String)iterator.next(); String key = (String)iterator.next();
newData.put(key, _deconstructPacket(_data.get(key), buffers)); try {
newData.put(key, _deconstructPacket(_data.get(key), buffers));
} catch (JSONException e) {
return null;
}
} }
return newData; return newData;
} }
@@ -67,7 +80,11 @@ public class Binary {
JSONArray _data = (JSONArray)data; JSONArray _data = (JSONArray)data;
int len = _data.length(); int len = _data.length();
for (int i = 0; i < len; i ++) { for (int i = 0; i < len; i ++) {
_data.put(i, _reconstructPacket(_data.get(i), buffers)); try {
_data.put(i, _reconstructPacket(_data.get(i), buffers));
} catch (JSONException e) {
return null;
}
} }
return _data; return _data;
} else if (data instanceof JSONObject) { } else if (data instanceof JSONObject) {
@@ -79,7 +96,11 @@ public class Binary {
Iterator<?> iterator = _data.keys(); Iterator<?> iterator = _data.keys();
while (iterator.hasNext()) { while (iterator.hasNext()) {
String key = (String)iterator.next(); String key = (String)iterator.next();
_data.put(key, _reconstructPacket(_data.get(key), buffers)); try {
_data.put(key, _reconstructPacket(_data.get(key), buffers));
} catch (JSONException e) {
return null;
}
} }
return _data; return _data;
} }

View File

@@ -13,7 +13,7 @@ import static org.junit.Assert.assertTrue;
public class HasBinaryDataTest { public class HasBinaryDataTest {
@Test @Test
public void arrayContainsByteArray() { public void arrayContainsByteArray() throws Exception {
JSONArray arr = new JSONArray("[1, null, 2]"); JSONArray arr = new JSONArray("[1, null, 2]");
arr.put(1, "asdfasdf".getBytes(Charset.forName("UTF-8"))); arr.put(1, "asdfasdf".getBytes(Charset.forName("UTF-8")));
assertTrue(HasBinaryData.hasBinary(arr)); assertTrue(HasBinaryData.hasBinary(arr));

View File

@@ -1,6 +1,7 @@
package com.github.nkzawa.socketio.client; package com.github.nkzawa.socketio.client;
import com.github.nkzawa.emitter.Emitter; import com.github.nkzawa.emitter.Emitter;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -57,7 +58,11 @@ public class ConnectionTest extends Connection {
public void call(Object... args) { public void call(Object... args) {
Ack fn = (Ack) args[0]; Ack fn = (Ack) args[0];
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
data.put("test", true); try {
data.put("test", true);
} catch (JSONException e) {
throw new AssertionError(e);
}
fn.call(5, data); fn.call(5, data);
} }
}); });
@@ -65,9 +70,13 @@ public class ConnectionTest extends Connection {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
JSONObject data = (JSONObject)args[1]; JSONObject data = (JSONObject)args[1];
if ((Integer)args[0] == 5 && data.getBoolean("test")) { try {
socket.close(); if ((Integer)args[0] == 5 && data.getBoolean("test")) {
latch.countDown(); 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() { socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override @Override
public void call(Object... objects) { public void call(Object... objects) {
socket.emit("getAckDate", new JSONObject("{test: true}"), new Ack() { try {
@Override socket.emit("getAckDate", new JSONObject("{test: true}"), new Ack() {
public void call(Object... args) { @Override
assertThat(args[0], instanceOf(String.class)); public void call(Object... args) {
socket.close(); assertThat(args[0], instanceOf(String.class));
latch.countDown(); socket.close();
} latch.countDown();
}); }
});
} catch (JSONException e) {
throw new AssertionError(e);
}
} }
}); });
socket.connect(); socket.connect();
@@ -329,13 +342,21 @@ public class ConnectionTest extends Connection {
@Override @Override
public void call(Object... objects) { public void call(Object... objects) {
JSONObject data = new JSONObject(); 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.emit("echo", data);
socket.on("echoBack", new Emitter.Listener() { socket.on("echoBack", new Emitter.Listener() {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
assertThat(args[0], instanceOf(JSONObject.class)); 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(); socket.close();
latch.countDown(); latch.countDown();
} }
@@ -379,17 +400,25 @@ public class ConnectionTest extends Connection {
public void call(Object... args) { public void call(Object... args) {
final byte[] buf = "howdy".getBytes(Charset.forName("UTF-8")); final byte[] buf = "howdy".getBytes(Charset.forName("UTF-8"));
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
data.put("hello", "lol"); try {
data.put("message", buf); data.put("hello", "lol");
data.put("goodbye", "gotcha"); data.put("message", buf);
data.put("goodbye", "gotcha");
} catch (JSONException e) {
throw new AssertionError(e);
}
socket.emit("echo", data); socket.emit("echo", data);
socket.on("echoBack", new Emitter.Listener() { socket.on("echoBack", new Emitter.Listener() {
@Override @Override
public void call(Object... args) { public void call(Object... args) {
JSONObject a = (JSONObject)args[0]; JSONObject a = (JSONObject)args[0];
assertThat(a.getString("hello"), is("lol")); try {
assertThat((byte[])a.get("message"), is(buf)); assertThat(a.getString("hello"), is("lol"));
assertThat(a.getString("goodbye"), is("gotcha")); assertThat((byte[])a.get("message"), is(buf));
assertThat(a.getString("goodbye"), is("gotcha"));
} catch (JSONException e) {
throw new AssertionError(e);
}
socket.close(); socket.close();
latch.countDown(); latch.countDown();
} }
@@ -401,7 +430,7 @@ public class ConnectionTest extends Connection {
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void sendEventsWithByteArraysInTheCorrectOrder() throws URISyntaxException, InterruptedException { public void sendEventsWithByteArraysInTheCorrectOrder() throws Exception {
final CountDownLatch latch = new CountDownLatch(1); final CountDownLatch latch = new CountDownLatch(1);
socket = client(); socket = client();
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

View File

@@ -72,7 +72,7 @@ public class ServerConnectionTest extends Connection {
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void event() throws URISyntaxException, InterruptedException { public void event() throws Exception {
final Semaphore semaphore = new Semaphore(0); final Semaphore semaphore = new Semaphore(0);
final JSONObject obj = new JSONObject(); final JSONObject obj = new JSONObject();
@@ -102,7 +102,7 @@ public class ServerConnectionTest extends Connection {
} }
@Test(timeout = TIMEOUT) @Test(timeout = TIMEOUT)
public void ack() throws URISyntaxException, InterruptedException { public void ack() throws Exception {
final Semaphore semaphore = new Semaphore(0); final Semaphore semaphore = new Semaphore(0);
final JSONObject obj = new JSONObject(); final JSONObject obj = new JSONObject();

View File

@@ -2,6 +2,7 @@ package com.github.nkzawa.socketio.parser;
import com.github.nkzawa.emitter.Emitter; import com.github.nkzawa.emitter.Emitter;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -36,7 +37,7 @@ public class ByteArrayTest {
} }
@Test @Test
public void encodeByteArrayDeepInJson() { public void encodeByteArrayDeepInJson() throws JSONException {
JSONObject data = new JSONObject("{a: \"hi\", b: {}, c: {a: \"bye\", b: {}}}"); JSONObject data = new JSONObject("{a: \"hi\", b: {}, c: {a: \"bye\", b: {}}}");
data.getJSONObject("b").put("why", new byte[3]); data.getJSONObject("b").put("why", new byte[3]);
data.getJSONObject("c").getJSONObject("b").put("a", new byte[6]); data.getJSONObject("c").getJSONObject("b").put("a", new byte[6]);
@@ -49,7 +50,7 @@ public class ByteArrayTest {
} }
@Test @Test
public void encodeDeepBinaryJSONWithNullValue() { public void encodeDeepBinaryJSONWithNullValue() throws JSONException {
JSONObject data = new JSONObject("{a: \"b\", c: 4, e: {g: null}, h: null}"); JSONObject data = new JSONObject("{a: \"b\", c: 4, e: {g: null}, h: null}");
data.put("h", new byte[9]); data.put("h", new byte[9]);
@@ -61,7 +62,7 @@ public class ByteArrayTest {
} }
@Test @Test
public void encodeBinaryAckWithByteArray() { public void encodeBinaryAckWithByteArray() throws JSONException {
JSONArray data = new JSONArray("[a, null, {}]"); JSONArray data = new JSONArray("[a, null, {}]");
data.put(1, "xxx".getBytes(Charset.forName("UTF-8"))); data.put(1, "xxx".getBytes(Charset.forName("UTF-8")));

View File

@@ -2,6 +2,7 @@ package com.github.nkzawa.socketio.parser;
import com.github.nkzawa.emitter.Emitter; import com.github.nkzawa.emitter.Emitter;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
@@ -66,9 +67,17 @@ public class Helpers {
assertThat(actual.attachments, is(expected.attachments)); assertThat(actual.attachments, is(expected.attachments));
if (expected.data instanceof JSONArray) { 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) { } 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 { } else {
assertThat(actual.data, is(expected.data)); assertThat(actual.data, is(expected.data));
} }

View File

@@ -1,6 +1,7 @@
package com.github.nkzawa.socketio.parser; package com.github.nkzawa.socketio.parser;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;
@@ -26,7 +27,7 @@ public class ParserTest {
} }
@Test @Test
public void encodeEvent() { public void encodeEvent() throws JSONException {
Packet<JSONArray> packet1 = new Packet<JSONArray>(Parser.EVENT); Packet<JSONArray> packet1 = new Packet<JSONArray>(Parser.EVENT);
packet1.data = new JSONArray("[\"a\", 1, {}]"); packet1.data = new JSONArray("[\"a\", 1, {}]");
packet1.nsp = "/"; packet1.nsp = "/";
@@ -39,7 +40,7 @@ public class ParserTest {
} }
@Test @Test
public void encodeAck() { public void encodeAck() throws JSONException {
Packet<JSONArray> packet = new Packet<JSONArray>(Parser.ACK); Packet<JSONArray> packet = new Packet<JSONArray>(Parser.ACK);
packet.data = new JSONArray("[\"a\", 1, {}]"); packet.data = new JSONArray("[\"a\", 1, {}]");
packet.id = 123; packet.id = 123;