View Javadoc

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