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<Human> developers <br/>
    29   * Human is an interface and the global tags are required
    30   */
    31  public class TypeSafeListWithInterfaceTest 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<Human> developers = new ArrayList<Human>();
    39          developers.add(new Developer("Fred", "creator"));
    40          developers.add(new Committer("John", "committer", 34));
    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-2.yaml");
    46          assertEquals(etalon, output);
    47      }
    48
    49      public void testLoadWrongList() {
    50          String output = Util.getLocalResource("examples/list-bean-1.yaml");
    51          // System.out.println(output);
    52          Yaml beanLoader = new Yaml();
    53          try {
    54              beanLoader.loadAs(output, ListBean.class);
    55              fail("Global tags are required since Human is an interface.");
    56          } catch (Exception e) {
    57              assertTrue(e.getMessage(), e.getMessage().contains("Cannot create property=developers"));
    58          }
    59      }
    60
    61      public void testLoadList() {
    62          String output = Util.getLocalResource("examples/list-bean-2.yaml");
    63          // System.out.println(output);
    64          Yaml beanLoader = new Yaml();
    65          ListBean parsed = beanLoader.loadAs(output, ListBean.class);
    66          assertNotNull(parsed);
    67          List<String> list2 = parsed.getChildren();
    68          assertEquals(2, list2.size());
    69          assertEquals("aaa", list2.get(0));
    70          assertEquals("bbb", list2.get(1));
    71          List<Human> developers = parsed.getDevelopers();
    72          assertEquals(2, developers.size());
    73          assertEquals("Developer must be recognised.", Developer.class, developers.get(0).getClass());
    74          Developer fred = (Developer) developers.get(0);
    75          assertEquals("Fred", fred.getName());
    76          assertEquals("creator", fred.getRole());
    77          Committer john = (Committer) developers.get(1);
    78          assertEquals("John", john.getName());
    79          assertEquals("committer", john.getRole());
    80          assertEquals(34, john.getKey());
    81      }
    82
    83      public static class ListBean {
    84          private List<String> children;
    85          private String name;
    86          private List<Human> developers;
    87
    88          public ListBean() {
    89              name = "Bean123";
    90          }
    91
    92          public List<String> getChildren() {
    93              return children;
    94          }
    95
    96          public void setChildren(List<String> children) {
    97              this.children = children;
    98          }
    99
    100         public String getName() {
    101             return name;
    102         }
    103
    104         public void setName(String name) {
    105             this.name = name;
    106         }
    107
    108         public List<Human> getDevelopers() {
    109             return developers;
    110         }
    111
    112         public void setDevelopers(List<Human> developers) {
    113             this.developers = developers;
    114         }
    115     }
    116
    117     public static interface Human {
    118
    119         public String getName();
    120
    121         public void setName(String name);
    122
    123     }
    124
    125     public static class Developer implements Human {
    126         private String name;
    127         private String role;
    128
    129         public Developer() {
    130         }
    131
    132         public Developer(String name, String role) {
    133             this.name = name;
    134             this.role = role;
    135         }
    136
    137         public String getName() {
    138             return name;
    139         }
    140
    141         public void setName(String name) {
    142             this.name = name;
    143         }
    144
    145         public String getRole() {
    146             return role;
    147         }
    148
    149         public void setRole(String role) {
    150             this.role = role;
    151         }
    152     }
    153
    154     public static class Committer extends Developer {
    155         private int key;
    156
    157         public Committer() {
    158         }
    159
    160         public Committer(String string, String string2, int i) {
    161             super(string, string2);
    162             this.key = i;
    163         }
    164
    165         public int getKey() {
    166             return key;
    167         }
    168
    169         public void setKey(int key) {
    170             this.key = key;
    171         }
    172     }
    173 }