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.ArrayList;
20  import java.util.List;
21  import java.util.Map;
22  
23  import junit.framework.TestCase;
24  
25  import org.yaml.snakeyaml.Util;
26  import org.yaml.snakeyaml.Yaml;
27  
28  /**
29   * Test ListBean->List developers <br/>
30   * Developer class cannot be properly recognised
31   */
32  public class TypeSafeListNoGerericsTest extends TestCase {
33      public void testDumpList() {
34          ListBean bean = new ListBean();
35          List<String> list = new ArrayList<String>();
36          list.add("aaa");
37          list.add("bbb");
38          bean.setChildren(list);
39          List<Developer> developers = new ArrayList<Developer>();
40          developers.add(new Developer("Fred", "creator"));
41          developers.add(new Developer("John", "committer"));
42          bean.setDevelopers(developers);
43          Yaml yaml = new Yaml();
44          String output = yaml.dumpAsMap(bean);
45          // System.out.println(output);
46          String etalon = Util.getLocalResource("examples/list-bean-4.yaml");
47          assertEquals(etalon, output);
48      }
49  
50      @SuppressWarnings("unchecked")
51      public void testLoadList() {
52          String output = Util.getLocalResource("examples/list-bean-1.yaml");
53          // System.out.println(output);
54          Yaml beanLoader = new Yaml();
55          ListBean parsed = beanLoader.loadAs(output, ListBean.class);
56          assertNotNull(parsed);
57          List<String> list2 = parsed.getChildren();
58          assertEquals(2, list2.size());
59          assertEquals("aaa", list2.get(0));
60          assertEquals("bbb", list2.get(1));
61          List<Map<String, String>> developers = parsed.getDevelopers();
62          assertEquals(2, developers.size());
63          Map<String, String> fred = developers.get(0);
64          assertEquals("Fred", fred.get("name"));
65          assertEquals("creator", fred.get("role"));
66      }
67  
68      @SuppressWarnings("rawtypes")
69      public static class ListBean {
70          private List<String> children;
71          private String name;
72          private List developers;
73  
74          public ListBean() {
75              name = "Bean123";
76          }
77  
78          public List<String> getChildren() {
79              return children;
80          }
81  
82          public void setChildren(List<String> children) {
83              this.children = children;
84          }
85  
86          public String getName() {
87              return name;
88          }
89  
90          public void setName(String name) {
91              this.name = name;
92          }
93  
94          public List getDevelopers() {
95              return developers;
96          }
97  
98          public void setDevelopers(List developers) {
99              this.developers = developers;
100         }
101     }
102 
103     public static class Developer {
104         private String name;
105         private String role;
106 
107         public Developer() {
108         }
109 
110         public Developer(String name, String role) {
111             this.name = name;
112             this.role = role;
113         }
114 
115         public String getName() {
116             return name;
117         }
118 
119         public void setName(String name) {
120             this.name = name;
121         }
122 
123         public String getRole() {
124             return role;
125         }
126 
127         public void setRole(String role) {
128             this.role = role;
129         }
130     }
131 }