TypeSafePriorityTest.java revision 11a89b445f3bde56bf07e6a0d04f0b0256dcb215
1/**
2 * Copyright (c) 2008, http://www.snakeyaml.org
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package examples.collections;
17
18import java.util.List;
19
20import junit.framework.TestCase;
21
22import org.yaml.snakeyaml.TypeDescription;
23import org.yaml.snakeyaml.Util;
24import org.yaml.snakeyaml.Yaml;
25import org.yaml.snakeyaml.constructor.Constructor;
26
27/**
28 * Test ListBean->List<Human> developers <br/>
29 * Human is an interface and the global tags are required
30 */
31public class TypeSafePriorityTest extends TestCase {
32
33    /**
34     * explicit TypeDescription is more important then runtime class (which may
35     * be an interface)
36     */
37    public void testLoadList2() {
38        String output = Util.getLocalResource("examples/list-bean-3.yaml");
39        // System.out.println(output);
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}
119