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.issues.issue8;
17  
18  import junit.framework.TestCase;
19  
20  import org.yaml.snakeyaml.DumperOptions;
21  import org.yaml.snakeyaml.Yaml;
22  
23  /**
24   * to test http://code.google.com/p/snakeyaml/issues/detail?id=8
25   */
26  public class PrattleRepresenterTest extends TestCase {
27      public void test() {
28          Yaml yaml = new Yaml();
29          Person person = new Person("Alan", "Gutierrez", 9);
30          String etalon = "!!org.yaml.snakeyaml.issues.issue8.Person {firstName: Alan, hatSize: 9, lastName: Gutierrez}\n";
31          assertEquals(etalon, yaml.dump(person));
32          assertEquals(etalon, yaml.dump(person));
33      }
34  
35      public void test2beans() {
36          DumperOptions options = new DumperOptions();
37          options.setAllowReadOnlyProperties(true);
38          Yaml yaml = new Yaml(options);
39          Person person = new Person("Alan", "Gutierrez", 9);
40          String etalon = "!!org.yaml.snakeyaml.issues.issue8.Person {firstName: Alan, hatSize: 9, lastName: Gutierrez}\n";
41          assertEquals(etalon, yaml.dump(person));
42          Horse horse = new Horse("Tom", person);
43          String etalon2 = "!!org.yaml.snakeyaml.issues.issue8.PrattleRepresenterTest$Horse\nname: Tom\nowner: {firstName: Alan, hatSize: 9, lastName: Gutierrez}\n";
44          assertEquals(etalon2, yaml.dump(horse));
45      }
46  
47      public static class Horse {
48          private String name;
49          private Person owner;
50  
51          public Horse(String name, Person owner) {
52              super();
53              this.name = name;
54              this.owner = owner;
55          }
56  
57          public String getName() {
58              return name;
59          }
60  
61          public Person getOwner() {
62              return owner;
63          }
64  
65      }
66  }