View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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  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   * Test ListBean->List<Human> developers <br/>
28   * Human is an interface and the global tags are required
29   */
30  public class TypeSafeListWithInterfaceTest extends TestCase {
31      public void testDumpList() {
32          ListBean bean = new ListBean();
33          List<String> list = new ArrayList<String>();
34          list.add("aaa");
35          list.add("bbb");
36          bean.setChildren(list);
37          List<Human> developers = new ArrayList<Human>();
38          developers.add(new Developer("Fred", "creator"));
39          developers.add(new Committer("John", "committer", 34));
40          bean.setDevelopers(developers);
41          Yaml yaml = new Yaml();
42          String output = yaml.dumpAsMap(bean);
43          // System.out.println(output);
44          String etalon = Util.getLocalResource("examples/list-bean-2.yaml");
45          assertEquals(etalon, output);
46      }
47  
48      public void testLoadWrongList() {
49          String output = Util.getLocalResource("examples/list-bean-1.yaml");
50          // System.out.println(output);
51          Yaml beanLoader = new Yaml();
52          try {
53              beanLoader.loadAs(output, ListBean.class);
54              fail("Global tags are required since Human is an interface.");
55          } catch (Exception e) {
56              assertTrue(e.getMessage(), e.getMessage().contains("Cannot create property=developers"));
57          }
58      }
59  
60      public void testLoadList() {
61          String output = Util.getLocalResource("examples/list-bean-2.yaml");
62          // System.out.println(output);
63          Yaml beanLoader = new Yaml();
64          ListBean parsed = beanLoader.loadAs(output, ListBean.class);
65          assertNotNull(parsed);
66          List<String> list2 = parsed.getChildren();
67          assertEquals(2, list2.size());
68          assertEquals("aaa", list2.get(0));
69          assertEquals("bbb", list2.get(1));
70          List<Human> developers = parsed.getDevelopers();
71          assertEquals(2, developers.size());
72          assertEquals("Developer must be recognised.", Developer.class, developers.get(0).getClass());
73          Developer fred = (Developer) developers.get(0);
74          assertEquals("Fred", fred.getName());
75          assertEquals("creator", fred.getRole());
76          Committer john = (Committer) developers.get(1);
77          assertEquals("John", john.getName());
78          assertEquals("committer", john.getRole());
79          assertEquals(34, john.getKey());
80      }
81  
82      public static class ListBean {
83          private List<String> children;
84          private String name;
85          private List<Human> developers;
86  
87          public ListBean() {
88              name = "Bean123";
89          }
90  
91          public List<String> getChildren() {
92              return children;
93          }
94  
95          public void setChildren(List<String> children) {
96              this.children = children;
97          }
98  
99          public String getName() {
100             return name;
101         }
102 
103         public void setName(String name) {
104             this.name = name;
105         }
106 
107         public List<Human> getDevelopers() {
108             return developers;
109         }
110 
111         public void setDevelopers(List<Human> developers) {
112             this.developers = developers;
113         }
114     }
115 
116     public static interface Human {
117 
118         public String getName();
119 
120         public void setName(String name);
121 
122     }
123 
124     public static class Developer implements Human {
125         private String name;
126         private String role;
127 
128         public Developer() {
129         }
130 
131         public Developer(String name, String role) {
132             this.name = name;
133             this.role = role;
134         }
135 
136         public String getName() {
137             return name;
138         }
139 
140         public void setName(String name) {
141             this.name = name;
142         }
143 
144         public String getRole() {
145             return role;
146         }
147 
148         public void setRole(String role) {
149             this.role = role;
150         }
151     }
152 
153     public static class Committer extends Developer {
154         private int key;
155 
156         public Committer() {
157         }
158 
159         public Committer(String string, String string2, int i) {
160             super(string, string2);
161             this.key = i;
162         }
163 
164         public int getKey() {
165             return key;
166         }
167 
168         public void setKey(int key) {
169             this.key = key;
170         }
171     }
172 }