View Javadoc

1   /**
2    * Copyright (c) 2008-2011, 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   */
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   * Test ListBean->List<Human> developers <br/>
30   * Human is an interface and the global tags are required
31   */
32  public class TypeSafePriorityTest extends TestCase {
33  
34      /**
35       * explicit TypeDescription is more important then runtime class (which may
36       * be an interface)
37       */
38      public void testLoadList2() {
39          String output = Util.getLocalResource("examples/list-bean-3.yaml");
40          // System.out.println(output);
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 }