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