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