comparison src/test/java/org/msgpack/TestCrossLang.java @ 0:cb825acd883a

first commit
author sugi
date Sat, 18 Oct 2014 15:06:15 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:cb825acd883a
1 package org.msgpack;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import static org.junit.Assert.assertFalse;
6 import static org.junit.Assert.assertArrayEquals;
7
8 import java.io.IOException;
9 import java.io.ByteArrayOutputStream;
10 import java.io.FileInputStream;
11 import java.util.Iterator;
12
13 import org.msgpack.MessagePack;
14 import org.msgpack.type.Value;
15 import org.msgpack.packer.BufferPacker;
16 import org.msgpack.unpacker.BufferUnpacker;
17
18 import org.junit.Test;
19
20
21 public class TestCrossLang {
22 private byte[] readData(String path) throws IOException {
23 ByteArrayOutputStream bo = new ByteArrayOutputStream();
24 FileInputStream input = new FileInputStream(path);
25 byte[] buffer = new byte[32*1024];
26 while(true) {
27 int count = input.read(buffer);
28 if(count < 0) {
29 break;
30 }
31 bo.write(buffer, 0, count);
32 }
33 return bo.toByteArray();
34 }
35
36 private byte[] readCompactTestData() throws IOException {
37 return readData("src/test/resources/cases_compact.mpac");
38 }
39
40 private byte[] readTestData() throws IOException {
41 return readData("src/test/resources/cases.mpac");
42 }
43
44 @Test
45 public void testReadValue() throws IOException {
46 MessagePack msgpack = new MessagePack();
47 byte[] a = readTestData();
48 byte[] b = readCompactTestData();
49
50 BufferUnpacker au = msgpack.createBufferUnpacker().wrap(a);
51 BufferUnpacker bu = msgpack.createBufferUnpacker().wrap(b);
52
53 Iterator<Value> at = au.iterator();
54 Iterator<Value> bt = bu.iterator();
55
56 while(at.hasNext()) {
57 assertTrue(bt.hasNext());
58 Value av = at.next();
59 Value bv = bt.next();
60 assertEquals(av, bv);
61 }
62 assertFalse(bt.hasNext());
63 }
64
65 @Test
66 public void testCompactSerialize() throws IOException {
67 MessagePack msgpack = new MessagePack();
68 byte[] a = readTestData();
69 byte[] b = readCompactTestData();
70
71 BufferPacker pk = msgpack.createBufferPacker();
72
73 BufferUnpacker au = msgpack.createBufferUnpacker().wrap(a);
74 for(Value av : au) {
75 pk.write(av);
76 }
77
78 byte[] c = pk.toByteArray();
79
80 assertEquals(b.length, c.length);
81 assertArrayEquals(b, c);
82 }
83 }
84