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