• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
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 public field ListBean->List<Developer> developers <br/>
28  * Developer class must be properly recognised
29  */
30 public class ListFileldBeanTest extends TestCase {
testDumpList()31     public void testDumpList() {
32         ListFieldBean bean = new ListFieldBean();
33         List<String> list = new ArrayList<String>();
34         list.add("aaa");
35         list.add("bbb");
36         bean.setChildren(list);
37         List<Developer> developers = new ArrayList<Developer>();
38         developers.add(new Developer("Fred", "creator"));
39         developers.add(new Developer("John", "committer"));
40         bean.developers = developers;
41         bean.setName("Bean123");
42         Yaml yaml = new Yaml();
43         String output = yaml.dumpAsMap(bean);
44         // System.out.println(output);
45         String etalon = Util.getLocalResource("examples/list-bean-1.yaml");
46         assertEquals(etalon, output);
47     }
48 
testLoadList()49     public void testLoadList() {
50         String output = Util.getLocalResource("examples/list-bean-1.yaml");
51         // System.out.println(output);
52         Yaml beanLoader = new Yaml();
53         ListFieldBean parsed = beanLoader.loadAs(output, ListFieldBean.class);
54         assertNotNull(parsed);
55         List<String> list2 = parsed.getChildren();
56         assertEquals(2, list2.size());
57         assertEquals("aaa", list2.get(0));
58         assertEquals("bbb", list2.get(1));
59         List<Developer> developers = parsed.developers;
60         assertEquals(2, developers.size());
61         assertEquals("Developer must be recognised.", Developer.class, developers.get(0).getClass());
62         Developer fred = developers.get(0);
63         assertEquals("Fred", fred.getName());
64         assertEquals("creator", fred.getRole());
65     }
66 
67     public static class ListFieldBean {
68         private List<String> children;
69         private String name;
70         public List<Developer> developers;
71 
ListFieldBean()72         public ListFieldBean() {
73             name = "Bean456";
74         }
75 
getChildren()76         public List<String> getChildren() {
77             return children;
78         }
79 
setChildren(List<String> children)80         public void setChildren(List<String> children) {
81             this.children = children;
82         }
83 
getName()84         public String getName() {
85             return name;
86         }
87 
setName(String name)88         public void setName(String name) {
89             this.name = name;
90         }
91     }
92 
93     public static class Developer {
94         private String name;
95         private String role;
96 
Developer()97         public Developer() {
98         }
99 
Developer(String name, String role)100         public Developer(String name, String role) {
101             this.name = name;
102             this.role = role;
103         }
104 
getName()105         public String getName() {
106             return name;
107         }
108 
setName(String name)109         public void setName(String name) {
110             this.name = name;
111         }
112 
getRole()113         public String getRole() {
114             return role;
115         }
116 
setRole(String role)117         public void setRole(String role) {
118             this.role = role;
119         }
120     }
121 }
122