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.javabeans;
    18
    19  import java.util.ArrayList;
    20  import java.util.List;
    21  import java.util.Map;
    22  import java.util.TreeMap;
    23
    24  import junit.framework.TestCase;
    25
    26  import org.yaml.snakeyaml.DumperOptions;
    27  import org.yaml.snakeyaml.DumperOptions.FlowStyle;
    28  import org.yaml.snakeyaml.TypeDescription;
    29  import org.yaml.snakeyaml.Util;
    30  import org.yaml.snakeyaml.Yaml;
    31  import org.yaml.snakeyaml.constructor.Constructor;
    32
    33  public class HouseTest extends TestCase {
    34      /**
    35       * no root global tag
    36       */
    37      public void testDump1() {
    38          House house = new House();
    39          FrontDoor frontDoor = new FrontDoor("qaz1", 5);
    40          frontDoor.setKeytype("qwerty123");
    41          house.setFrontDoor(frontDoor);
    42          List<Room> rooms = new ArrayList<Room>();
    43          rooms.add(new Room("Hall"));
    44          rooms.add(new Room("Kitchen"));
    45          house.setRooms(rooms);
    46          Map<String, String> reminders = new TreeMap<String, String>();
    47          reminders.put("today", "do nothig");
    48          reminders.put("tomorrow", "go shoping");
    49          house.setReminders(reminders);
    50          house.setNumber(1);
    51          house.setStreet("Wall Street");
    52          Yaml beanDumper = new Yaml();
    53          String yaml = beanDumper.dumpAsMap(house);
    54          String etalon = Util.getLocalResource("javabeans/house-dump1.yaml");
    55          assertEquals(etalon, yaml);
    56          // load
    57          Yaml beanLoader = new Yaml();
    58          House loadedHouse = beanLoader.loadAs(yaml, House.class);
    59          assertNotNull(loadedHouse);
    60          assertEquals("Wall Street", loadedHouse.getStreet());
    61          // dump again
    62          String yaml3 = beanDumper.dumpAsMap(loadedHouse);
    63          assertEquals(yaml, yaml3);
    64      }
    65
    66      /**
    67       * with global root class tag (global tag should be avoided)
    68       */
    69      public void testDump3() {
    70          House house = new House();
    71          FrontDoor frontDoor = new FrontDoor("qaz1", 5);
    72          frontDoor.setKeytype("qwerty123");
    73          house.setFrontDoor(frontDoor);
    74          List<Room> rooms = new ArrayList<Room>();
    75          rooms.add(new Room("Hall"));
    76          rooms.add(new Room("Kitchen"));
    77          house.setRooms(rooms);
    78          Map<String, String> reminders = new TreeMap<String, String>();
    79          reminders.put("today", "do nothig");
    80          reminders.put("tomorrow", "go shoping");
    81          house.setReminders(reminders);
    82          house.setNumber(1);
    83          house.setStreet("Wall Street");
    84          Yaml beanDumper = new Yaml();
    85          String yaml = beanDumper.dumpAsMap(house);
    86          String etalon = Util.getLocalResource("javabeans/house-dump3.yaml");
    87          assertEquals(etalon, yaml);
    88          // load
    89          TypeDescription description = new TypeDescription(House.class);
    90          description.putListPropertyType("rooms", Room.class);
    91          Yaml beanLoader = new Yaml(new Constructor(description));
    92          House loadedHouse = (House) beanLoader.load(yaml);
    93          House loadedHouse2 = (House) beanLoader.loadAs(yaml, House.class);
    94          assertNotNull(loadedHouse);
    95          assertFalse(loadedHouse == loadedHouse2);
    96          assertEquals("Wall Street", loadedHouse.getStreet());
    97          assertEquals(1, loadedHouse.getNumber());
    98          assertEquals(1, loadedHouse2.getNumber());
    99          FrontDoor fdoor = loadedHouse.getFrontDoor();
    100         assertEquals(frontDoor.getId(), fdoor.getId());
    101         assertEquals(frontDoor.getHeight(), fdoor.getHeight());
    102         assertEquals(frontDoor.getKeytype(), fdoor.getKeytype());
    103         assertEquals(frontDoor, fdoor);
    104         assertEquals(reminders, loadedHouse.getReminders());
    105         List<Room> loadedRooms = loadedHouse.getRooms();
    106         assertEquals(rooms, loadedRooms);
    107         // dump again
    108         String yaml3 = beanDumper.dumpAsMap(loadedHouse);
    109         assertEquals(yaml, yaml3);
    110     }
    111
    112     /**
    113      * with global root class tag (global tag should be avoided)
    114      */
    115     public void testDump2() {
    116         House house = new House();
    117         FrontDoor frontDoor = new FrontDoor("qaz1", 5);
    118         frontDoor.setKeytype("qwerty123");
    119         house.setFrontDoor(frontDoor);
    120         List<Room> rooms = new ArrayList<Room>();
    121         rooms.add(new Room("Hall"));
    122         rooms.add(new Room("Kitchen"));
    123         house.setRooms(rooms);
    124         Map<String, String> reminders = new TreeMap<String, String>();
    125         reminders.put("today", "do nothig");
    126         reminders.put("tomorrow", "go shoping");
    127         house.setReminders(reminders);
    128         house.setNumber(1);
    129         house.setStreet("Wall Street");
    130         DumperOptions options = new DumperOptions();
    131         options.setDefaultFlowStyle(FlowStyle.BLOCK);
    132         Yaml beanDumper = new Yaml(options);
    133         String yaml = beanDumper.dump(house);
    134         String etalon = Util.getLocalResource("javabeans/house-dump2.yaml");
    135         assertEquals(etalon, yaml);
    136         // load
    137         Yaml beanLoader = new Yaml();
    138         House loadedHouse = beanLoader.loadAs(yaml, House.class);
    139         assertNotNull(loadedHouse);
    140         assertEquals("Wall Street", loadedHouse.getStreet());
    141         // dump again
    142         String yaml3 = beanDumper.dump(loadedHouse);
    143         assertEquals(yaml, yaml3);
    144     }
    145 }