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