forked from msgpack/msgpack-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestNestedList.java
More file actions
96 lines (74 loc) · 3.14 KB
/
TestNestedList.java
File metadata and controls
96 lines (74 loc) · 3.14 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
90
91
92
93
94
95
96
package org.msgpack;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.msgpack.annotation.Message;
import org.msgpack.type.ArrayValue;
import org.msgpack.type.Value;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* User: takeshita
* Create: 11/10/17 23:17
*/
public class TestNestedList {
MessagePack messagePack;
@Before
public void before(){
messagePack = new MessagePack();
}
@Test
public void testTestNestedList() throws IOException {
NestedList obj = new NestedList();
obj.list.add(list("aaa", "bbb"));
obj.list.add(list(new MyClass("obj1"), new MyClass("obj2")));
obj.list2.add((List<MyClass>)list(new MyClass("obj3")));
byte[] bytes = messagePack.write(obj);
// Can't unpack as NestedList
Value unpacked = messagePack.read(bytes);
ArrayValue root = unpacked.asArrayValue().getElementArray()[0].asArrayValue();
ArrayValue list1 = root.getElementArray()[0].asArrayValue();
ArrayValue list2 = root.getElementArray()[1].asArrayValue();
ArrayValue list3 = unpacked.asArrayValue().getElementArray()[1].asArrayValue();
list3 = list3.getElementArray()[0].asArrayValue();
Assert.assertEquals("aaa",list1.getElementArray()[0].asRawValue().getString());
Assert.assertEquals("bbb",list1.getElementArray()[1].asRawValue().getString());
Assert.assertEquals("obj1",messagePack.convert(list2.getElementArray()[0],MyClass.class).name);
Assert.assertEquals("obj2",messagePack.convert(list2.getElementArray()[1],MyClass.class).name);
Assert.assertEquals("obj3",messagePack.convert(list3.getElementArray()[0],MyClass.class).name);
}
@Test
public void testNestedListToValue() throws IOException {
List values = list( list("hoge",4) , list(list(2,"aaa"),list("bbb")));
Value value = messagePack.unconvert(values);
Value[] rootArray = value.asArrayValue().getElementArray();
Value[] list1 = rootArray[0].asArrayValue().getElementArray();
Value[] list2 = rootArray[1].asArrayValue().getElementArray();
Value[] list3 = list2[0].asArrayValue().getElementArray();
Value[] list4 = list2[1].asArrayValue().getElementArray();
Assert.assertEquals("hoge",list1[0].asRawValue().getString());
Assert.assertEquals(4,list1[1].asIntegerValue().getInt());
Assert.assertEquals(2,list3[0].asIntegerValue().getInt());
Assert.assertEquals("aaa",list3[1].asRawValue().getString());
Assert.assertEquals("bbb",list4[0].asRawValue().getString());
}
private List<?> list( Object ... elements){
List<Object> list = new ArrayList();
for(Object o : elements){
list.add(o);
}
return list;
}
@Message
public static class NestedList{
public List<List> list = new ArrayList<List>();
public List<List<MyClass>> list2 = new ArrayList<List<MyClass>>();
}
@Message
public static class MyClass{
String name;
public MyClass(){}
public MyClass(String n ){ name = n;}
}
}