forked from msgpack/msgpack-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestCrossLang.java
More file actions
90 lines (73 loc) · 2.51 KB
/
TestCrossLang.java
File metadata and controls
90 lines (73 loc) · 2.51 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
package org.msgpack;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertArrayEquals;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.msgpack.MessagePack;
import org.msgpack.value.Value;
import org.msgpack.packer.StreamPacker;
import org.msgpack.unpacker.BufferUnpacker;
import org.junit.Test;
public class TestCrossLang {
private byte[] readData(String path) throws IOException {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
FileInputStream input = new FileInputStream(path);
byte[] buffer = new byte[32*1024];
while(true) {
int count = input.read(buffer);
if(count < 0) {
break;
}
bo.write(buffer, 0, count);
}
return bo.toByteArray();
}
private byte[] readCompactTestData() throws IOException {
return readData("src/test/resources/cases_compact.mpac");
}
private byte[] readTestData() throws IOException {
return readData("src/test/resources/cases.mpac");
}
@Test
public void testReadValue() throws IOException {
MessagePack msgpack = new MessagePack();
byte[] a = readTestData();
byte[] b = readCompactTestData();
BufferUnpacker au = new BufferUnpacker().wrap(a);
BufferUnpacker bu = new BufferUnpacker().wrap(b);
Iterator<Value> at = au.iterator();
Iterator<Value> bt = bu.iterator();
while(at.hasNext()) {
assertTrue(bt.hasNext());
Value av = at.next();
Value bv = bt.next();
assertEquals(av, bv);
}
assertFalse(bt.hasNext());
}
@Test
public void testCompactSerialize() throws IOException {
MessagePack msgpack = new MessagePack();
byte[] a = readTestData();
byte[] b = readCompactTestData();
ByteArrayOutputStream out = new ByteArrayOutputStream();
StreamPacker pk = new StreamPacker(out);
BufferUnpacker au = new BufferUnpacker().wrap(a);
for(Value av : au) {
pk.write(av);
}
byte[] c = out.toByteArray();
assertEquals(b.length, c.length);
assertArrayEquals(b, c);
}
}