forked from socketio/socket.io-client-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.java
More file actions
96 lines (86 loc) · 3.37 KB
/
Copy pathHelpers.java
File metadata and controls
96 lines (86 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package io.socket.parser;
import io.socket.emitter.Emitter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.skyscreamer.jsonassert.JSONAssert;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(JUnit4.class)
public class Helpers {
private static Parser.Encoder encoder = new IOParser.Encoder();
private static Packet<String> errorPacket = new Packet<String>(Parser.ERROR, "parser error");
public static void test(final Packet obj) {
encoder.encode(obj, new Parser.Encoder.Callback() {
@Override
public void call(Object[] encodedPackets) {
Parser.Decoder decoder = new IOParser.Decoder();
decoder.onDecoded(new Parser.Decoder.Callback() {
@Override
public void call(Packet packet) {
assertPacket(packet, obj);
}
});
decoder.add((String)encodedPackets[0]);
}
});
}
public static void testDecodeError(final String errorMessage) {
Parser.Decoder decoder = new IOParser.Decoder();
decoder.onDecoded(new IOParser.Decoder.Callback() {
@Override
public void call(Packet packet) {
assertPacket(errorPacket, packet);
}
});
decoder.add(errorMessage);
}
@SuppressWarnings("unchecked")
public static void testBin(final Packet obj) {
final Object originalData = obj.data;
encoder.encode(obj, new Parser.Encoder.Callback() {
@Override
public void call(Object[] encodedPackets) {
Parser.Decoder decoder = new IOParser.Decoder();
decoder.onDecoded(new Parser.Decoder.Callback() {
@Override
public void call(Packet packet) {
obj.data = originalData;
obj.attachments = -1;
assertPacket(packet, obj);
}
});
for (Object packet : encodedPackets) {
if (packet instanceof String) {
decoder.add((String)packet);
} else if (packet instanceof byte[]) {
decoder.add((byte[])packet);
}
}
}
});
}
public static void assertPacket(Packet expected, Packet actual) {
assertThat(actual.type, is(expected.type));
assertThat(actual.id, is(expected.id));
assertThat(actual.nsp, is(expected.nsp));
assertThat(actual.attachments, is(expected.attachments));
if (expected.data instanceof JSONArray) {
try {
JSONAssert.assertEquals((JSONArray)expected.data, (JSONArray)actual.data, true);
} catch (JSONException e) {
throw new AssertionError(e);
}
} else if (expected.data instanceof JSONObject) {
try {
JSONAssert.assertEquals((JSONObject)expected.data, (JSONObject)actual.data, true);
} catch (JSONException e) {
throw new AssertionError(e);
}
} else {
assertThat(actual.data, is(expected.data));
}
}
}