|
| 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 | + |
| 33 | + byte[] bytes = messagePack.write(obj); |
| 34 | + |
| 35 | + // Can't unpack as NestedList |
| 36 | + Value unpacked = messagePack.read(bytes); |
| 37 | + ArrayValue root = unpacked.asArrayValue(); |
| 38 | + ArrayValue list1 = root.getElementArray()[0].asArrayValue(); |
| 39 | + ArrayValue list2 = root.getElementArray()[1].asArrayValue(); |
| 40 | + |
| 41 | + Assert.assertEquals("aaa",list1.getElementArray()[0].asIntegerValue().getInt()); |
| 42 | + Assert.assertEquals("bbb",list1.getElementArray()[1].asIntegerValue().getInt()); |
| 43 | + Assert.assertEquals("obj1",messagePack.convert(list2.getElementArray()[0],MyClass.class).name); |
| 44 | + Assert.assertEquals("obj2",messagePack.convert(list2.getElementArray()[1],MyClass.class).name); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + private List<?> list( Object ... elements){ |
| 49 | + List<Object> list = new ArrayList(); |
| 50 | + for(Object o : elements){ |
| 51 | + list.add(o); |
| 52 | + } |
| 53 | + return list; |
| 54 | + } |
| 55 | + |
| 56 | + @Message |
| 57 | + public static class NestedList{ |
| 58 | + public List<List> list = new ArrayList<List>(); |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + @Message |
| 63 | + public static class MyClass{ |
| 64 | + String name; |
| 65 | + |
| 66 | + public MyClass(){} |
| 67 | + public MyClass(String n ){ name = n;} |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments