Files
socket.io-client-java/src/test/java/io/socket/hasbinary/HasBinaryTest.java
2015-08-31 03:23:03 +09:00

74 lines
2.3 KiB
Java

package io.socket.hasbinary;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.nio.charset.Charset;
import static org.junit.Assert.assertTrue;
@RunWith(JUnit4.class)
public class HasBinaryTest {
@Test
public void byteArray() {
assertTrue(HasBinary.hasBinary(new byte[0]));
}
@Test
public void anArrayThatDoesNotContainByteArray() throws JSONException {
JSONArray arr = new JSONArray("[1, \"cool\", 2]");
assertTrue(!HasBinary.hasBinary(arr));
}
@Test
public void anArrayContainsByteArray() throws JSONException {
JSONArray arr = new JSONArray("[1, null, 2]");
arr.put(1, "asdfasdf".getBytes(Charset.forName("UTF-8")));
assertTrue(HasBinary.hasBinary(arr));
}
@Test
public void anObjectThatDoesNotContainByteArray() throws JSONException {
JSONObject ob = new JSONObject("{\"a\": \"a\", \"b\": [], \"c\": 1234}");
assertTrue(!HasBinary.hasBinary(ob));
}
@Test
public void anObjectThatContainsByteArray() throws JSONException {
JSONObject ob = new JSONObject("{\"a\": \"a\", \"b\": null, \"c\": 1234}");
ob.put("b", "abc".getBytes(Charset.forName("UTF-8")));
assertTrue(HasBinary.hasBinary(ob));
}
@Test
public void testNull() {
assertTrue(!HasBinary.hasBinary(null));
}
@Test
public void aComplexObjectThatContainsNoBinary() throws JSONException {
JSONObject ob = new JSONObject();
ob.put("x", new JSONArray("[\"a\", \"b\", 123]"));
ob.put("y", JSONObject.NULL);
ob.put("z", new JSONObject("{\"a\": \"x\", \"b\": \"y\", \"c\": 3, \"d\": null}"));
ob.put("w", new JSONArray());
assertTrue(!HasBinary.hasBinary(ob));
}
@Test
public void aComplexObjectThatContainsBinary() throws JSONException {
JSONObject ob = new JSONObject();
ob.put("x", new JSONArray("[\"a\", \"b\", 123]"));
ob.put("y", JSONObject.NULL);
ob.put("z", new JSONObject("{\"a\": \"x\", \"b\": \"y\", \"c\": 3, \"d\": null}"));
ob.put("w", new JSONArray());
ob.put("bin", "xxx".getBytes(Charset.forName("UTF-8")));
assertTrue(HasBinary.hasBinary(ob));
}
}