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.recursive;
    18
    19  import java.io.IOException;
    20  import java.util.Date;
    21
    22  import junit.framework.TestCase;
    23
    24  import org.yaml.snakeyaml.TypeDescription;
    25  import org.yaml.snakeyaml.Util;
    26  import org.yaml.snakeyaml.Yaml;
    27  import org.yaml.snakeyaml.constructor.Constructor;
    28
    29  public class Human_WithArrayOfChildrenTest extends TestCase {
    30
    31      public static class Human_WithArrayOfChildren extends AbstractHuman {
    32
    33          private Human_WithArrayOfChildren father;
    34          private Human_WithArrayOfChildren mother;
    35          private Human_WithArrayOfChildren partner;
    36          private Human_WithArrayOfChildren bankAccountOwner;
    37          protected Human_WithArrayOfChildren[] children;
    38
    39          public Human_WithArrayOfChildren() {
    40              children = new Human_WithArrayOfChildren[0];
    41          }
    42
    43          public Human_WithArrayOfChildren getFather() {
    44              return father;
    45          }
    46
    47          public void setFather(Human_WithArrayOfChildren father) {
    48              this.father = father;
    49          }
    50
    51          public Human_WithArrayOfChildren getMother() {
    52              return mother;
    53          }
    54
    55          public void setMother(Human_WithArrayOfChildren mother) {
    56              this.mother = mother;
    57          }
    58
    59          public Human_WithArrayOfChildren getPartner() {
    60              return partner;
    61          }
    62
    63          public void setPartner(Human_WithArrayOfChildren partner) {
    64              this.partner = partner;
    65          }
    66
    67          public Human_WithArrayOfChildren getBankAccountOwner() {
    68              return bankAccountOwner;
    69          }
    70
    71          public void setBankAccountOwner(Human_WithArrayOfChildren bankAccountOwner) {
    72              this.bankAccountOwner = bankAccountOwner;
    73          }
    74
    75          public Human_WithArrayOfChildren[] getChildren() {
    76              return children;
    77          }
    78
    79          public void setChildren(Human_WithArrayOfChildren[] children) {
    80              this.children = children;
    81          }
    82
    83      }
    84
    85      private Human_WithArrayOfChildren createSon() {
    86          Human_WithArrayOfChildren father = new Human_WithArrayOfChildren();
    87          father.setName("Father");
    88          father.setBirthday(new Date(1000000000));
    89          father.setBirthPlace("Leningrad");
    90          father.setBankAccountOwner(father);
    91          //
    92          Human_WithArrayOfChildren mother = new Human_WithArrayOfChildren();
    93          mother.setName("Mother");
    94          mother.setBirthday(new Date(100000000000L));
    95          mother.setBirthPlace("Saint-Petersburg");
    96          father.setPartner(mother);
    97          mother.setPartner(father);
    98          mother.setBankAccountOwner(father);
    99          //
    100         Human_WithArrayOfChildren son = new Human_WithArrayOfChildren();
    101         son.setName("Son");
    102         son.setBirthday(new Date(310000000000L));
    103         son.setBirthPlace("Munich");
    104         son.setBankAccountOwner(father);
    105         son.setFather(father);
    106         son.setMother(mother);
    107         //
    108         Human_WithArrayOfChildren daughter = new Human_WithArrayOfChildren();
    109         daughter.setName("Daughter");
    110         daughter.setBirthday(new Date(420000000000L));
    111         daughter.setBirthPlace("New York");
    112         daughter.setBankAccountOwner(father);
    113         daughter.setFather(father);
    114         daughter.setMother(mother);
    115         //
    116         Human_WithArrayOfChildren[] children = new Human_WithArrayOfChildren[] { son, daughter };
    117         father.setChildren(children);
    118         mother.setChildren(children);
    119         //
    120         return son;
    121     }
    122
    123     private void checkSon(Human_WithArrayOfChildren son) {
    124         assertNotNull(son);
    125         assertEquals("Son", son.getName());
    126
    127         Human_WithArrayOfChildren father2 = son.getFather();
    128         assertEquals("Father", father2.getName());
    129         assertEquals("Mother", son.getMother().getName());
    130         assertSame(father2, father2.getBankAccountOwner());
    131         assertSame(father2.getPartner(), son.getMother());
    132         assertSame(father2, son.getMother().getPartner());
    133
    134         Human_WithArrayOfChildren[] fathersChildren = father2.getChildren();
    135         assertEquals(2, fathersChildren.length);
    136         Human_WithArrayOfChildren[] mothersChildren = father2.getPartner().getChildren();
    137         assertEquals(2, mothersChildren.length);
    138         assertSame(mothersChildren, fathersChildren);
    139
    140         for (Object child : fathersChildren) {
    141             // check if type descriptor was correct
    142             assertSame(Human_WithArrayOfChildren.class, child.getClass());
    143         }
    144     }
    145
    146     public void testChildrenArray() throws IOException {
    147         Constructor constructor = new Constructor(Human_WithArrayOfChildren.class);
    148         TypeDescription HumanWithChildrenArrayDescription = new TypeDescription(
    149                 Human_WithArrayOfChildren.class);
    150         HumanWithChildrenArrayDescription.putListPropertyType("children",
    151                 Human_WithArrayOfChildren.class);
    152         constructor.addTypeDescription(HumanWithChildrenArrayDescription);
    153         Human_WithArrayOfChildren son = createSon();
    154         Yaml yaml = new Yaml(constructor);
    155         String output = yaml.dump(son);
    156         // System.out.println(output);
    157         String etalon = Util.getLocalResource("recursive/with-childrenArray.yaml");
    158         assertEquals(etalon, output);
    159         //
    160         Human_WithArrayOfChildren son2 = (Human_WithArrayOfChildren) yaml.load(output);
    161         checkSon(son2);
    162     }
    163
    164     public void testDumpChildrenArrayWithoutRootTag() throws IOException {
    165         Yaml yaml = new Yaml();
    166         Human_WithArrayOfChildren son = createSon();
    167         String output = yaml.dumpAsMap(son);
    168         // System.out.println(output);
    169         String etalon = Util.getLocalResource("recursive/with-childrenArray-no-root-tag.yaml");
    170         assertEquals(etalon, output);
    171     }
    172
    173     public void testParseChildrenArrayWithoutRootTag() throws IOException {
    174         Constructor constructor = new Constructor(Human_WithArrayOfChildren.class);
    175         TypeDescription HumanWithChildrenArrayDescription = new TypeDescription(
    176                 Human_WithArrayOfChildren.class);
    177         HumanWithChildrenArrayDescription.putListPropertyType("children",
    178                 Human_WithArrayOfChildren.class);
    179         constructor.addTypeDescription(HumanWithChildrenArrayDescription);
    180         Yaml yaml = new Yaml(constructor);
    181         String doc = Util.getLocalResource("recursive/with-childrenArray-no-root-tag.yaml");
    182         Human_WithArrayOfChildren son2 = (Human_WithArrayOfChildren) yaml.load(doc);
    183         checkSon(son2);
    184     }
    185 }