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