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.List;
20
21 import junit.framework.TestCase;
22
23 import org.yaml.snakeyaml.TypeDescription;
24 import org.yaml.snakeyaml.Util;
25 import org.yaml.snakeyaml.Yaml;
26 import org.yaml.snakeyaml.constructor.Constructor;
27
28
29
30
31
32 public class TypeSafePriorityTest extends TestCase {
33
34
35
36
37
38 public void testLoadList2() {
39 String output = Util.getLocalResource("examples/list-bean-3.yaml");
40
41 TypeDescription descr = new TypeDescription(ListBean.class);
42 descr.putListPropertyType("developers", Developer.class);
43 Yaml beanLoader = new Yaml(new Constructor(descr));
44 ListBean parsed = beanLoader.loadAs(output, ListBean.class);
45 assertNotNull(parsed);
46 List<Human> developers = parsed.getDevelopers();
47 assertEquals(2, developers.size());
48 assertEquals("Committer must be recognised.", Developer.class, developers.get(0).getClass());
49 Developer fred = (Developer) developers.get(0);
50 assertEquals("Fred", fred.getName());
51 assertEquals("creator", fred.getRole());
52 Developer john = (Developer) developers.get(1);
53 assertEquals("John", john.getName());
54 assertEquals("committer", john.getRole());
55 }
56
57 public static class ListBean {
58 private String name;
59 private List<Human> developers;
60
61 public ListBean() {
62 name = "Bean123";
63 }
64
65 public String getName() {
66 return name;
67 }
68
69 public void setName(String name) {
70 this.name = name;
71 }
72
73 public List<Human> getDevelopers() {
74 return developers;
75 }
76
77 public void setDevelopers(List<Human> developers) {
78 this.developers = developers;
79 }
80 }
81
82 public static interface Human {
83
84 public String getName();
85
86 public void setName(String name);
87
88 }
89
90 public static class Developer implements Human {
91 private String name;
92 private String role;
93
94 public Developer() {
95 }
96
97 public Developer(String name, String role) {
98 this.name = name;
99 this.role = role;
100 }
101
102 public String getName() {
103 return name;
104 }
105
106 public void setName(String name) {
107 this.name = name;
108 }
109
110 public String getRole() {
111 return role;
112 }
113
114 public void setRole(String role) {
115 this.role = role;
116 }
117 }
118
119 }