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 TypeSafeListTest extends TestCase {
31 public void testDumpList() {
32 ListBean1 bean = new ListBean1();
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.setDevelopers(developers);
41 Yaml yaml = new Yaml();
42 String output = yaml.dumpAsMap(bean);
43
44 String etalon = Util.getLocalResource("examples/list-bean-1.yaml");
45 assertEquals(etalon, output);
46 }
47
48 public void testLoadList() {
49 String output = Util.getLocalResource("examples/list-bean-1.yaml");
50
51 Yaml beanLoader = new Yaml();
52 ListBean1 parsed = beanLoader.loadAs(output, ListBean1.class);
53 assertNotNull(parsed);
54 List<String> list2 = parsed.getChildren();
55 assertEquals(2, list2.size());
56 assertEquals("aaa", list2.get(0));
57 assertEquals("bbb", list2.get(1));
58 List<Developer> developers = parsed.getDevelopers();
59 assertEquals(2, developers.size());
60 assertEquals("Developer must be recognised.", Developer.class, developers.get(0).getClass());
61 Developer fred = developers.get(0);
62 assertEquals("Fred", fred.getName());
63 assertEquals("creator", fred.getRole());
64 }
65
66 public static class ListBean1 {
67 private List<String> children;
68 private String name;
69 private List<Developer> developers;
70
71 public ListBean1() {
72 name = "Bean123";
73 }
74
75 public List<String> getChildren() {
76 return children;
77 }
78
79 public void setChildren(List<String> children) {
80 this.children = children;
81 }
82
83 public String getName() {
84 return name;
85 }
86
87 public void setName(String name) {
88 this.name = name;
89 }
90
91 public List<Developer> getDevelopers() {
92 return developers;
93 }
94
95 public void setDevelopers(List<Developer> developers) {
96 this.developers = developers;
97 }
98 }
99
100 public static class Developer {
101 private String name;
102 private String role;
103
104 public Developer() {
105 }
106
107 public Developer(String name, String role) {
108 this.name = name;
109 this.role = role;
110 }
111
112 public String getName() {
113 return name;
114 }
115
116 public void setName(String name) {
117 this.name = name;
118 }
119
120 public String getRole() {
121 return role;
122 }
123
124 public void setRole(String role) {
125 this.role = role;
126 }
127 }
128 }