0
|
1 package org.msgpack;
|
|
2
|
|
3 import junit.framework.Assert;
|
|
4 import org.junit.Before;
|
|
5 import org.junit.Test;
|
|
6 import org.msgpack.annotation.Message;
|
|
7 import org.msgpack.type.ArrayValue;
|
|
8 import org.msgpack.type.Value;
|
|
9
|
|
10 import java.io.IOException;
|
|
11 import java.util.ArrayList;
|
|
12 import java.util.List;
|
|
13
|
|
14 /**
|
|
15 * User: takeshita
|
|
16 * Create: 11/10/17 23:17
|
|
17 */
|
|
18 public class TestNestedList {
|
|
19
|
|
20 MessagePack messagePack;
|
|
21
|
|
22 @Before
|
|
23 public void before(){
|
|
24 messagePack = new MessagePack();
|
|
25 }
|
|
26
|
|
27 @Test
|
|
28 public void testTestNestedList() throws IOException {
|
|
29 NestedList obj = new NestedList();
|
|
30 obj.list.add(list("aaa", "bbb"));
|
|
31 obj.list.add(list(new MyClass("obj1"), new MyClass("obj2")));
|
|
32 obj.list2.add((List<MyClass>)list(new MyClass("obj3")));
|
|
33
|
|
34 byte[] bytes = messagePack.write(obj);
|
|
35
|
|
36 // Can't unpack as NestedList
|
|
37 Value unpacked = messagePack.read(bytes);
|
|
38 ArrayValue root = unpacked.asArrayValue().getElementArray()[0].asArrayValue();
|
|
39 ArrayValue list1 = root.getElementArray()[0].asArrayValue();
|
|
40 ArrayValue list2 = root.getElementArray()[1].asArrayValue();
|
|
41 ArrayValue list3 = unpacked.asArrayValue().getElementArray()[1].asArrayValue();
|
|
42 list3 = list3.getElementArray()[0].asArrayValue();
|
|
43
|
|
44 Assert.assertEquals("aaa",list1.getElementArray()[0].asRawValue().getString());
|
|
45 Assert.assertEquals("bbb",list1.getElementArray()[1].asRawValue().getString());
|
|
46 Assert.assertEquals("obj1",messagePack.convert(list2.getElementArray()[0],MyClass.class).name);
|
|
47 Assert.assertEquals("obj2",messagePack.convert(list2.getElementArray()[1],MyClass.class).name);
|
|
48 Assert.assertEquals("obj3",messagePack.convert(list3.getElementArray()[0],MyClass.class).name);
|
|
49
|
|
50 }
|
|
51
|
|
52 @Test
|
|
53 public void testNestedListToValue() throws IOException {
|
|
54
|
|
55 List values = list( list("hoge",4) , list(list(2,"aaa"),list("bbb")));
|
|
56
|
|
57 Value value = messagePack.unconvert(values);
|
|
58
|
|
59 Value[] rootArray = value.asArrayValue().getElementArray();
|
|
60 Value[] list1 = rootArray[0].asArrayValue().getElementArray();
|
|
61 Value[] list2 = rootArray[1].asArrayValue().getElementArray();
|
|
62 Value[] list3 = list2[0].asArrayValue().getElementArray();
|
|
63 Value[] list4 = list2[1].asArrayValue().getElementArray();
|
|
64 Assert.assertEquals("hoge",list1[0].asRawValue().getString());
|
|
65 Assert.assertEquals(4,list1[1].asIntegerValue().getInt());
|
|
66 Assert.assertEquals(2,list3[0].asIntegerValue().getInt());
|
|
67 Assert.assertEquals("aaa",list3[1].asRawValue().getString());
|
|
68 Assert.assertEquals("bbb",list4[0].asRawValue().getString());
|
|
69
|
|
70 }
|
|
71
|
|
72 private List<?> list( Object ... elements){
|
|
73 List<Object> list = new ArrayList();
|
|
74 for(Object o : elements){
|
|
75 list.add(o);
|
|
76 }
|
|
77 return list;
|
|
78 }
|
|
79
|
|
80 @Message
|
|
81 public static class NestedList{
|
|
82 public List<List> list = new ArrayList<List>();
|
|
83
|
|
84 public List<List<MyClass>> list2 = new ArrayList<List<MyClass>>();
|
|
85
|
|
86 }
|
|
87
|
|
88 @Message
|
|
89 public static class MyClass{
|
|
90 String name;
|
|
91
|
|
92 public MyClass(){}
|
|
93 public MyClass(String n ){ name = n;}
|
|
94 }
|
|
95
|
|
96 }
|