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 org.yaml.snakeyaml.issues.issue74;
    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  public class ArrayBeanTest extends TestCase {
    28
    29      public void testArrayProperty() {
    30          ArrayMember[] members = new ArrayMember[3];
    31          members[0] = new ArrayMember("Foo", 21);
    32          members[1] = new ArrayMember("Bar", 23);
    33          members[2] = new ArrayMember("Hue Long Hair", 25);
    34          ArrayBean bean = new ArrayBean();
    35          bean.setId("ID123");
    36          bean.setNumber(7);
    37          bean.setMembers(members);
    38          bean.openMembers = new ArrayMember[] { new ArrayMember("OpenFoo", 1000),
    39                  new ArrayMember("OpenBar", 2000) };
    40          List<ArrayMember> list = new ArrayList<ArrayMember>(2);
    41          list.add(new ArrayMember("John", 111));
    42          list.add(new ArrayMember("Tony", 222));
    43          bean.setList(list);
    44          Yaml yaml = new Yaml();
    45          String output = yaml.dumpAsMap(bean);
    46          // System.out.println(output);
    47          assertEquals(Util.getLocalResource("issues/issue74-array1.txt"), output);
    48          Yaml beanLoader = new Yaml();
    49          ArrayBean parsed = beanLoader.loadAs(output, ArrayBean.class);
    50          // System.out.println(parsed);
    51          assertEquals(3, parsed.getMembers().length);
    52          assertEquals(2, parsed.openMembers.length);
    53          assertEquals(2, parsed.getList().size());
    54          assertEquals("ID123", parsed.getId());
    55          assertEquals(7, parsed.getNumber());
    56          for (ArrayMember member : parsed.getMembers()) {
    57              assertTrue((member.getAge() >= 21) && (member.getAge() <= 25));
    58          }
    59      }
    60
    61      public static class ArrayBean {
    62          private String id;
    63          private int number;
    64          private ArrayMember[] members;
    65          public ArrayMember[] openMembers;
    66          private List<ArrayMember> list;
    67
    68          public String getId() {
    69              return id;
    70          }
    71
    72          public void setId(String id) {
    73              this.id = id;
    74          }
    75
    76          public int getNumber() {
    77              return number;
    78          }
    79
    80          public void setNumber(int number) {
    81              this.number = number;
    82          }
    83
    84          public ArrayMember[] getMembers() {
    85              return members;
    86          }
    87
    88          public void setMembers(ArrayMember[] members) {
    89              this.members = members;
    90          }
    91
    92          public List<ArrayMember> getList() {
    93              return list;
    94          }
    95
    96          public void setList(List<ArrayMember> list) {
    97              this.list = list;
    98          }
    99      }
    100
    101     public static class ArrayMember {
    102         private String name;
    103         private int age;
    104
    105         public ArrayMember(String name, int age) {
    106             this.name = name;
    107             this.age = age;
    108         }
    109
    110         public ArrayMember() {
    111             this.name = "NoName";
    112             this.age = 0;
    113         }
    114
    115         public String getName() {
    116             return name;
    117         }
    118
    119         public void setName(String name) {
    120             this.name = name;
    121         }
    122
    123         public int getAge() {
    124             return age;
    125         }
    126
    127         public void setAge(int age) {
    128             this.age = age;
    129         }
    130
    131         @Override
    132         public boolean equals(Object obj) {
    133             if (obj instanceof ArrayMember) {
    134                 ArrayMember m = (ArrayMember) obj;
    135                 return age == m.age;
    136             } else {
    137                 return false;
    138             }
    139         }
    140
    141         @Override
    142         public int hashCode() {
    143             return age;
    144         }
    145
    146         @Override
    147         public String toString() {
    148             return "ArrayMember age=" + age;
    149         }
    150     }
    151 }