Mercurial > hg > Members > sugi > MessagePack-java
comparison src/test/java/org/msgpack/TestPackConvert.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.assertArrayEquals; | |
5 import static org.junit.Assert.assertTrue; | |
6 | |
7 import java.io.ByteArrayOutputStream; | |
8 import java.io.IOException; | |
9 import java.math.BigInteger; | |
10 import java.util.ArrayList; | |
11 import java.util.HashMap; | |
12 import java.util.Iterator; | |
13 import java.util.List; | |
14 import java.util.Map; | |
15 | |
16 import junit.framework.Assert; | |
17 import org.junit.Test; | |
18 import org.msgpack.packer.Packer; | |
19 import org.msgpack.type.ValueFactory; | |
20 import org.msgpack.unpacker.BufferUnpacker; | |
21 import org.msgpack.unpacker.Converter; | |
22 import org.msgpack.type.Value; | |
23 | |
24 | |
25 public class TestPackConvert extends TestSet { | |
26 | |
27 @Test @Override | |
28 public void testBoolean() throws Exception { | |
29 super.testBoolean(); | |
30 } | |
31 | |
32 @Override | |
33 public void testBoolean(boolean v) throws Exception { | |
34 MessagePack msgpack = new MessagePack(); | |
35 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
36 Packer packer = msgpack.createPacker(out); | |
37 packer.write(v); | |
38 byte[] bytes = out.toByteArray(); | |
39 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
40 unpacker.resetReadByteCount(); | |
41 Value value = unpacker.readValue(); | |
42 assertTrue(value.isBooleanValue()); | |
43 boolean ret = new Converter(value).readBoolean(); | |
44 assertEquals(v, ret); | |
45 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
46 } | |
47 | |
48 @Test @Override | |
49 public void testByte() throws Exception { | |
50 super.testByte(); | |
51 } | |
52 | |
53 @Override | |
54 public void testByte(byte v) throws Exception { | |
55 MessagePack msgpack = new MessagePack(); | |
56 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
57 Packer packer = msgpack.createPacker(out); | |
58 packer.write(v); | |
59 byte[] bytes = out.toByteArray(); | |
60 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
61 unpacker.resetReadByteCount(); | |
62 Value value = unpacker.readValue(); | |
63 assertTrue(value.isIntegerValue()); | |
64 byte ret = new Converter(value).readByte(); | |
65 assertEquals(v, ret); | |
66 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
67 } | |
68 | |
69 @Test @Override | |
70 public void testShort() throws Exception { | |
71 super.testShort(); | |
72 } | |
73 | |
74 @Override | |
75 public void testShort(short v) throws Exception { | |
76 MessagePack msgpack = new MessagePack(); | |
77 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
78 Packer packer = msgpack.createPacker(out); | |
79 packer.write(v); | |
80 byte[] bytes = out.toByteArray(); | |
81 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
82 unpacker.resetReadByteCount(); | |
83 Value value = unpacker.readValue(); | |
84 assertTrue(value.isIntegerValue()); | |
85 short ret = new Converter(value).readShort(); | |
86 assertEquals(v, ret); | |
87 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
88 } | |
89 | |
90 @Test @Override | |
91 public void testInteger() throws Exception { | |
92 super.testInteger(); | |
93 } | |
94 | |
95 @Override | |
96 public void testInteger(int v) throws Exception { | |
97 MessagePack msgpack = new MessagePack(); | |
98 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
99 Packer packer = msgpack.createPacker(out); | |
100 packer.write(v); | |
101 byte[] bytes = out.toByteArray(); | |
102 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
103 unpacker.resetReadByteCount(); | |
104 Value value = unpacker.readValue(); | |
105 assertTrue(value.isIntegerValue()); | |
106 int ret = new Converter(value).readInt(); | |
107 assertEquals(v, ret); | |
108 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
109 } | |
110 | |
111 @Test @Override | |
112 public void testLong() throws Exception { | |
113 super.testLong(); | |
114 } | |
115 | |
116 @Override | |
117 public void testLong(long v) throws Exception { | |
118 MessagePack msgpack = new MessagePack(); | |
119 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
120 Packer packer = msgpack.createPacker(out); | |
121 packer.write(v); | |
122 byte[] bytes = out.toByteArray(); | |
123 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
124 unpacker.resetReadByteCount(); | |
125 Value value = unpacker.readValue(); | |
126 assertTrue(value.isIntegerValue()); | |
127 long ret = new Converter(value).readLong(); | |
128 assertEquals(v, ret); | |
129 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
130 } | |
131 | |
132 @Test @Override | |
133 public void testFloat() throws Exception { | |
134 super.testFloat(); | |
135 } | |
136 | |
137 @Override | |
138 public void testFloat(float v) throws Exception { | |
139 MessagePack msgpack = new MessagePack(); | |
140 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
141 Packer packer = msgpack.createPacker(out); | |
142 packer.write(v); | |
143 byte[] bytes = out.toByteArray(); | |
144 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
145 unpacker.resetReadByteCount(); | |
146 Value value = unpacker.readValue(); | |
147 assertTrue(value.isFloatValue()); | |
148 float ret = new Converter(value).readFloat(); | |
149 assertEquals(v, ret, 10e-10); | |
150 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
151 } | |
152 | |
153 @Test @Override | |
154 public void testDouble() throws Exception { | |
155 super.testDouble(); | |
156 } | |
157 | |
158 @Override | |
159 public void testDouble(double v) throws Exception { | |
160 MessagePack msgpack = new MessagePack(); | |
161 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
162 Packer packer = msgpack.createPacker(out); | |
163 packer.write(v); | |
164 byte[] bytes = out.toByteArray(); | |
165 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
166 unpacker.resetReadByteCount(); | |
167 Value value = unpacker.readValue(); | |
168 assertTrue(value.isFloatValue()); | |
169 double ret = new Converter(value).readDouble(); | |
170 assertEquals(v, ret, 10e-10); | |
171 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
172 } | |
173 | |
174 @Test @Override | |
175 public void testNil() throws Exception { | |
176 MessagePack msgpack = new MessagePack(); | |
177 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
178 Packer packer = msgpack.createPacker(out); | |
179 packer.writeNil(); | |
180 byte[] bytes = out.toByteArray(); | |
181 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
182 unpacker.resetReadByteCount(); | |
183 Value value = unpacker.readValue(); | |
184 assertTrue(value.isNilValue()); | |
185 new Converter(value).readNil(); | |
186 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
187 } | |
188 | |
189 @Test @Override | |
190 public void testBigInteger() throws Exception { | |
191 super.testBigInteger(); | |
192 } | |
193 | |
194 @Override | |
195 public void testBigInteger(BigInteger v) throws Exception { | |
196 MessagePack msgpack = new MessagePack(); | |
197 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
198 Packer packer = msgpack.createPacker(out); | |
199 packer.write(v); | |
200 byte[] bytes = out.toByteArray(); | |
201 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
202 unpacker.resetReadByteCount(); | |
203 Value value = unpacker.readValue(); | |
204 BigInteger ret = new Converter(value).read(BigInteger.class); | |
205 assertEquals(v, ret); | |
206 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
207 } | |
208 | |
209 @Test @Override | |
210 public void testString() throws Exception { | |
211 super.testString(); | |
212 } | |
213 | |
214 @Override | |
215 public void testString(String v) throws Exception { | |
216 MessagePack msgpack = new MessagePack(); | |
217 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
218 Packer packer = msgpack.createPacker(out); | |
219 packer.write(v); | |
220 byte[] bytes = out.toByteArray(); | |
221 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
222 unpacker.resetReadByteCount(); | |
223 Value value = unpacker.readValue(); | |
224 String ret = new Converter(value).read(String.class); | |
225 assertEquals(v, ret); | |
226 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
227 } | |
228 | |
229 @Test @Override | |
230 public void testByteArray() throws Exception { | |
231 super.testByteArray(); | |
232 } | |
233 | |
234 @Override | |
235 public void testByteArray(byte[] v) throws Exception { | |
236 MessagePack msgpack = new MessagePack(); | |
237 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
238 Packer packer = msgpack.createPacker(out); | |
239 packer.write(v); | |
240 byte[] bytes = out.toByteArray(); | |
241 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
242 unpacker.resetReadByteCount(); | |
243 Value value = unpacker.readValue(); | |
244 byte[] ret = new Converter(value).read(byte[].class); | |
245 assertArrayEquals(v, ret); | |
246 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
247 } | |
248 | |
249 @Test @Override | |
250 public void testList() throws Exception { | |
251 super.testList(); | |
252 } | |
253 | |
254 @Override | |
255 public <E> void testList(List<E> v, Class<E> elementClass) throws Exception { | |
256 MessagePack msgpack = new MessagePack(); | |
257 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
258 Packer packer = msgpack.createPacker(out); | |
259 if (v == null) { | |
260 packer.writeNil(); | |
261 } else { | |
262 packer.writeArrayBegin(v.size()); | |
263 for (Object o : v) { | |
264 packer.write(o); | |
265 } | |
266 packer.writeArrayEnd(); | |
267 } | |
268 byte[] bytes = out.toByteArray(); | |
269 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
270 unpacker.resetReadByteCount(); | |
271 if (unpacker.trySkipNil()) { | |
272 assertEquals(null, v); | |
273 return; | |
274 } | |
275 int size = unpacker.readArrayBegin(); | |
276 List<E> ret = new ArrayList<E>(size); | |
277 for (int i = 0; i < size; ++i) { | |
278 Value value = unpacker.readValue(); | |
279 ret.add(new Converter(value).read(elementClass)); | |
280 } | |
281 unpacker.readArrayEnd(); | |
282 assertEquals(v.size(), ret.size()); | |
283 Iterator<E> v_iter = v.iterator(); | |
284 Iterator<E> ret_iter = ret.iterator(); | |
285 while (v_iter.hasNext()) { | |
286 assertEquals(v_iter.next(), ret_iter.next()); | |
287 } | |
288 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
289 } | |
290 | |
291 @Test @Override | |
292 public void testMap() throws Exception { | |
293 super.testMap(); | |
294 } | |
295 | |
296 @Override | |
297 public <K, V> void testMap(Map<K, V> v, Class<K> keyElementClass, Class<V> valueElementClass) throws Exception { | |
298 MessagePack msgpack = new MessagePack(); | |
299 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
300 Packer packer = msgpack.createPacker(out); | |
301 if (v == null) { | |
302 packer.writeNil(); | |
303 } else { | |
304 packer.writeMapBegin(v.size()); | |
305 for (Map.Entry<K, V> e : ((Map<K, V>) v).entrySet()) { | |
306 packer.write(e.getKey()); | |
307 packer.write(e.getValue()); | |
308 } | |
309 packer.writeMapEnd(); | |
310 } | |
311 byte[] bytes = out.toByteArray(); | |
312 BufferUnpacker unpacker = msgpack.createBufferUnpacker(bytes); | |
313 unpacker.resetReadByteCount(); | |
314 if (unpacker.trySkipNil()) { | |
315 assertEquals(null, v); | |
316 return; | |
317 } | |
318 int size = unpacker.readMapBegin(); | |
319 Map<K, V> ret = new HashMap<K, V>(size); | |
320 for (int i = 0; i < size; ++i) { | |
321 Value keyValue = unpacker.readValue(); | |
322 K key = new Converter(keyValue).read(keyElementClass); | |
323 Value valueValue = unpacker.readValue(); | |
324 V value = new Converter(valueValue).read(valueElementClass); | |
325 ret.put(key, value); | |
326 } | |
327 unpacker.readMapEnd(); | |
328 assertEquals(v.size(), ret.size()); | |
329 for (Map.Entry<K, V> e : ((Map<K, V>) v).entrySet()) { | |
330 Object value = ret.get(e.getKey()); | |
331 assertEquals(e.getValue(), value); | |
332 } | |
333 assertEquals(bytes.length, unpacker.getReadByteCount()); | |
334 } | |
335 | |
336 @Test | |
337 public void testPackValue() throws IOException { | |
338 MessagePack msgpack = new MessagePack(); | |
339 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
340 Packer packer = msgpack.createPacker(out); | |
341 String text = "This is Value"; | |
342 Value value = ValueFactory.createRawValue(text); | |
343 packer.write(value); | |
344 byte[] bytes = out.toByteArray(); | |
345 Assert.assertEquals(text.length() + 1,bytes.length); | |
346 Assert.assertEquals(0xa0 + text.length(), 0xff & bytes[0]); | |
347 } | |
348 | |
349 @Test | |
350 public void testPackValuePassedAsObject() throws IOException{ | |
351 MessagePack msgpack = new MessagePack(); | |
352 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
353 Packer packer = msgpack.createPacker(out); | |
354 String text = "This is Value"; | |
355 Object value = ValueFactory.createRawValue(text); | |
356 packer.write(value); // passed as object | |
357 byte[] bytes = out.toByteArray(); | |
358 Assert.assertEquals(text.length() + 1,bytes.length); | |
359 Assert.assertEquals(0xa0 + text.length(), 0xff & bytes[0]); | |
360 | |
361 } | |
362 | |
363 } |