Mercurial > hg > Members > sugi > MessagePack-java
comparison src/test/java/org/msgpack/template/TestByteBufferTemplate.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.template; | |
2 | |
3 import static org.junit.Assert.assertArrayEquals; | |
4 import static org.junit.Assert.assertEquals; | |
5 | |
6 import java.io.ByteArrayInputStream; | |
7 import java.io.ByteArrayOutputStream; | |
8 import java.nio.ByteBuffer; | |
9 | |
10 import org.junit.Test; | |
11 import org.msgpack.MessagePack; | |
12 import org.msgpack.TestSet; | |
13 import org.msgpack.packer.BufferPacker; | |
14 import org.msgpack.packer.Packer; | |
15 import org.msgpack.unpacker.BufferUnpacker; | |
16 import org.msgpack.unpacker.Unpacker; | |
17 | |
18 | |
19 public class TestByteBufferTemplate { | |
20 | |
21 private static byte[] toByteArray(ByteBuffer from) { | |
22 if (from == null) { | |
23 return null; | |
24 } | |
25 byte[] bytes = new byte[from.remaining()]; | |
26 from.get(bytes, from.arrayOffset() + from.position(), from.remaining()); | |
27 return bytes; | |
28 } | |
29 | |
30 @Test | |
31 public void testPackUnpack() throws Exception { | |
32 new TestPackUnpack().testByteBuffer(); | |
33 } | |
34 | |
35 @Test | |
36 public void testPackBufferUnpack() throws Exception { | |
37 new TestPackBufferUnpack().testByteBuffer(); | |
38 } | |
39 | |
40 @Test | |
41 public void testBufferPackBufferUnpack() throws Exception { | |
42 new TestBufferPackBufferUnpack().testByteBuffer(); | |
43 } | |
44 | |
45 @Test | |
46 public void testBufferPackUnpack() throws Exception { | |
47 new TestBufferPackUnpack().testByteBuffer(); | |
48 } | |
49 | |
50 private static class TestPackUnpack extends TestSet { | |
51 @Test @Override | |
52 public void testByteBuffer() throws Exception { | |
53 super.testByteBuffer(); | |
54 } | |
55 | |
56 @Override | |
57 public void testByteBuffer(ByteBuffer v) throws Exception { | |
58 MessagePack msgpack = new MessagePack(); | |
59 Template<ByteBuffer> tmpl = ByteBufferTemplate.instance; | |
60 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
61 Packer packer = msgpack.createPacker(out); | |
62 tmpl.write(packer, v); | |
63 byte[] bytes = out.toByteArray(); | |
64 Unpacker unpacker = msgpack.createUnpacker(new ByteArrayInputStream(bytes)); | |
65 unpacker.resetReadByteCount(); | |
66 ByteBuffer ret = tmpl.read(unpacker, null); | |
67 assertArrayEquals(toByteArray(v), toByteArray(ret)); | |
68 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
69 } | |
70 } | |
71 | |
72 private static class TestPackBufferUnpack extends TestSet { | |
73 @Test @Override | |
74 public void testByteBuffer() throws Exception { | |
75 super.testByteBuffer(); | |
76 } | |
77 | |
78 @Override | |
79 public void testByteBuffer(ByteBuffer v) throws Exception { | |
80 MessagePack msgpack = new MessagePack(); | |
81 Template<ByteBuffer> tmpl = ByteBufferTemplate.instance; | |
82 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
83 Packer packer = msgpack.createPacker(out); | |
84 tmpl.write(packer, v); | |
85 byte[] bytes = out.toByteArray(); | |
86 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
87 unpacker.resetReadByteCount(); | |
88 ByteBuffer ret = tmpl.read(unpacker, null); | |
89 assertArrayEquals(toByteArray(v), toByteArray(ret)); | |
90 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
91 } | |
92 } | |
93 | |
94 private static class TestBufferPackBufferUnpack extends TestSet { | |
95 @Test @Override | |
96 public void testByteBuffer() throws Exception { | |
97 super.testByteBuffer(); | |
98 } | |
99 | |
100 @Override | |
101 public void testByteBuffer(ByteBuffer v) throws Exception { | |
102 MessagePack msgpack = new MessagePack(); | |
103 Template<ByteBuffer> tmpl = ByteBufferTemplate.instance; | |
104 BufferPacker packer = msgpack.createBufferPacker(); | |
105 tmpl.write(packer, v); | |
106 byte[] bytes = packer.toByteArray(); | |
107 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
108 unpacker.resetReadByteCount(); | |
109 ByteBuffer ret = tmpl.read(unpacker, null); | |
110 assertArrayEquals(toByteArray(v), toByteArray(ret)); | |
111 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
112 } | |
113 } | |
114 | |
115 private static class TestBufferPackUnpack extends TestSet { | |
116 @Test @Override | |
117 public void testByteBuffer() throws Exception { | |
118 super.testByteBuffer(); | |
119 } | |
120 | |
121 @Override | |
122 public void testByteBuffer(ByteBuffer v) throws Exception { | |
123 MessagePack msgpack = new MessagePack(); | |
124 Template<ByteBuffer> tmpl = ByteBufferTemplate.instance; | |
125 BufferPacker packer = msgpack.createBufferPacker(); | |
126 tmpl.write(packer, v); | |
127 byte[] bytes = packer.toByteArray(); | |
128 Unpacker unpacker = msgpack.createUnpacker(new ByteArrayInputStream(bytes)); | |
129 unpacker.resetReadByteCount(); | |
130 ByteBuffer ret = tmpl.read(unpacker, null); | |
131 assertArrayEquals(toByteArray(v), toByteArray(ret)); | |
132 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
133 } | |
134 } | |
135 } |