1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package examples.collections; 15 16 import java.util.ArrayList; 17 import java.util.List; 18 import junit.framework.TestCase; 19 import org.yaml.snakeyaml.Util; 20 import org.yaml.snakeyaml.Yaml; 21 22 /** 23 * Test public field ListBean->List<Developer> developers <br/> 24 * Developer class must be properly recognised 25 */ 26 public class ListFileldBeanTest extends TestCase { 27 testDumpList()28 public void testDumpList() { 29 ListFieldBean bean = new ListFieldBean(); 30 List<String> list = new ArrayList<String>(); 31 list.add("aaa"); 32 list.add("bbb"); 33 bean.setChildren(list); 34 List<Developer> developers = new ArrayList<Developer>(); 35 developers.add(new Developer("Fred", "creator")); 36 developers.add(new Developer("John", "committer")); 37 bean.developers = developers; 38 bean.setName("Bean123"); 39 Yaml yaml = new Yaml(); 40 String output = yaml.dumpAsMap(bean); 41 // System.out.println(output); 42 String etalon = Util.getLocalResource("examples/list-bean-1.yaml"); 43 assertEquals(etalon, output); 44 } 45 testLoadList()46 public void testLoadList() { 47 String output = Util.getLocalResource("examples/list-bean-1.yaml"); 48 // System.out.println(output); 49 Yaml beanLoader = new Yaml(); 50 ListFieldBean parsed = beanLoader.loadAs(output, ListFieldBean.class); 51 assertNotNull(parsed); 52 List<String> list2 = parsed.getChildren(); 53 assertEquals(2, list2.size()); 54 assertEquals("aaa", list2.get(0)); 55 assertEquals("bbb", list2.get(1)); 56 List<Developer> developers = parsed.developers; 57 assertEquals(2, developers.size()); 58 assertEquals("Developer must be recognised.", Developer.class, developers.get(0).getClass()); 59 Developer fred = developers.get(0); 60 assertEquals("Fred", fred.getName()); 61 assertEquals("creator", fred.getRole()); 62 } 63 64 public static class ListFieldBean { 65 66 private List<String> children; 67 private String name; 68 public List<Developer> developers; 69 ListFieldBean()70 public ListFieldBean() { 71 name = "Bean456"; 72 } 73 getChildren()74 public List<String> getChildren() { 75 return children; 76 } 77 setChildren(List<String> children)78 public void setChildren(List<String> children) { 79 this.children = children; 80 } 81 getName()82 public String getName() { 83 return name; 84 } 85 setName(String name)86 public void setName(String name) { 87 this.name = name; 88 } 89 } 90 91 public static class Developer { 92 93 private String name; 94 private String role; 95 Developer()96 public Developer() {} 97 Developer(String name, String role)98 public Developer(String name, String role) { 99 this.name = name; 100 this.role = role; 101 } 102 getName()103 public String getName() { 104 return name; 105 } 106 setName(String name)107 public void setName(String name) { 108 this.name = name; 109 } 110 getRole()111 public String getRole() { 112 return role; 113 } 114 setRole(String role)115 public void setRole(String role) { 116 this.role = role; 117 } 118 } 119 } 120