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
    22  import junit.framework.TestCase;
    23
    24  import org.yaml.snakeyaml.Util;
    25  import org.yaml.snakeyaml.Yaml;
    26
    27  /**
    28   * Test ListBean->List<Developer> developers <br/>
    29   * Developer class must be properly recognised
    30   */
    31  public class TypeSafeListTest extends TestCase {
    32      public void testDumpList() {
    33          ListBean1 bean = new ListBean1();
    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-1.yaml");
    46          assertEquals(etalon, output);
    47      }
    48
    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          ListBean1 parsed = beanLoader.loadAs(output, ListBean1.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.getDevelopers();
    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 ListBean1 {
    68          private List<String> children;
    69          private String name;
    70          private List<Developer> developers;
    71
    72          public ListBean1() {
    73              name = "Bean123";
    74          }
    75
    76          public List<String> getChildren() {
    77              return children;
    78          }
    79
    80          public void setChildren(List<String> children) {
    81              this.children = children;
    82          }
    83
    84          public String getName() {
    85              return name;
    86          }
    87
    88          public void setName(String name) {
    89              this.name = name;
    90          }
    91
    92          public List<Developer> getDevelopers() {
    93              return developers;
    94          }
    95
    96          public void setDevelopers(List<Developer> developers) {
    97              this.developers = developers;
    98          }
    99      }
    100
    101     public static class Developer {
    102         private String name;
    103         private String role;
    104
    105         public Developer() {
    106         }
    107
    108         public Developer(String name, String role) {
    109             this.name = name;
    110             this.role = role;
    111         }
    112
    113         public String getName() {
    114             return name;
    115         }
    116
    117         public void setName(String name) {
    118             this.name = name;
    119         }
    120
    121         public String getRole() {
    122             return role;
    123         }
    124
    125         public void setRole(String role) {
    126             this.role = role;
    127         }
    128     }
    129 }