1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package examples.collections;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import junit.framework.TestCase;
22
23 import org.yaml.snakeyaml.Util;
24 import org.yaml.snakeyaml.Yaml;
25
26
27
28
29
30 public class ListFileldBeanTest extends TestCase {
31 public void testDumpList() {
32 ListFieldBean bean = new ListFieldBean();
33 List<String> list = new ArrayList<String>();
34 list.add("aaa");
35 list.add("bbb");
36 bean.setChildren(list);
37 List<Developer> developers = new ArrayList<Developer>();
38 developers.add(new Developer("Fred", "creator"));
39 developers.add(new Developer("John", "committer"));
40 bean.developers = developers;
41 bean.setName("Bean123");
42 Yaml yaml = new Yaml();
43 String output = yaml.dumpAsMap(bean);
44
45 String etalon = Util.getLocalResource("examples/list-bean-1.yaml");
46 assertEquals(etalon, output);
47 }
48
49 public void testLoadList() {
50 String output = Util.getLocalResource("examples/list-bean-1.yaml");
51
52 Yaml beanLoader = new Yaml();
53 ListFieldBean parsed = beanLoader.loadAs(output, ListFieldBean.class);
54 assertNotNull(parsed);
55 List<String> list2 = parsed.getChildren();
56 assertEquals(2, list2.size());
57 assertEquals("aaa", list2.get(0));
58 assertEquals("bbb", list2.get(1));
59 List<Developer> developers = parsed.developers;
60 assertEquals(2, developers.size());
61 assertEquals("Developer must be recognised.", Developer.class, developers.get(0).getClass());
62 Developer fred = developers.get(0);
63 assertEquals("Fred", fred.getName());
64 assertEquals("creator", fred.getRole());
65 }
66
67 public static class ListFieldBean {
68 private List<String> children;
69 private String name;
70 public List<Developer> developers;
71
72 public ListFieldBean() {
73 name = "Bean456";
74 }
75
76 public List<String> getChildren() {
77 return children;
78 }
79
80 public void setChildren(List<String> children) {
81 this.children = children;
82 }
83
84 public String getName() {
85 return name;
86 }
87
88 public void setName(String name) {
89 this.name = name;
90 }
91 }
92
93 public static class Developer {
94 private String name;
95 private String role;
96
97 public Developer() {
98 }
99
100 public Developer(String name, String role) {
101 this.name = name;
102 this.role = role;
103 }
104
105 public String getName() {
106 return name;
107 }
108
109 public void setName(String name) {
110 this.name = name;
111 }
112
113 public String getRole() {
114 return role;
115 }
116
117 public void setRole(String role) {
118 this.role = role;
119 }
120 }
121 }