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.constructor;
    18
    19  import java.io.IOException;
    20  import java.util.HashMap;
    21  import java.util.List;
    22  import java.util.Map;
    23
    24  import junit.framework.TestCase;
    25
    26  import org.yaml.snakeyaml.DumperOptions.FlowStyle;
    27  import org.yaml.snakeyaml.TypeDescription;
    28  import org.yaml.snakeyaml.Util;
    29  import org.yaml.snakeyaml.Yaml;
    30  import org.yaml.snakeyaml.nodes.Tag;
    31  import org.yaml.snakeyaml.representer.Representer;
    32
    33  public class ImplicitTagsTest extends TestCase {
    34
    35      public void testDefaultRepresenter() throws IOException {
    36          CarWithWheel car1 = new CarWithWheel();
    37          car1.setPlate("12-XP-F4");
    38          Wheel wheel = new Wheel();
    39          wheel.setId(2);
    40          car1.setWheel(wheel);
    41          Map<String, Integer> map = new HashMap<String, Integer>();
    42          map.put("id", 3);
    43          car1.setMap(map);
    44          car1.setPart(new Wheel(4));
    45          car1.setYear("2008");
    46          String carYaml1 = new Yaml().dump(car1);
    47          assertEquals(Util.getLocalResource("constructor/carwheel-without-tags.yaml"), carYaml1);
    48          CarWithWheel car2 = (CarWithWheel) new Yaml().load(carYaml1);
    49          String carYaml2 = new Yaml().dump(car2);
    50          assertEquals(carYaml1, carYaml2);
    51      }
    52
    53      public void testNoRootTag() throws IOException {
    54          CarWithWheel car1 = new CarWithWheel();
    55          car1.setPlate("12-XP-F4");
    56          Wheel wheel = new Wheel();
    57          wheel.setId(2);
    58          car1.setWheel(wheel);
    59          Map<String, Integer> map = new HashMap<String, Integer>();
    60          map.put("id", 3);
    61          car1.setMap(map);
    62          car1.setYear("2008");
    63          String carYaml1 = new Yaml().dumpAs(car1, Tag.MAP, FlowStyle.AUTO);
    64          assertEquals(Util.getLocalResource("constructor/car-without-root-tag.yaml"), carYaml1);
    65          //
    66          Constructor contructor = new Constructor(CarWithWheel.class);
    67          CarWithWheel car2 = (CarWithWheel) new Yaml(contructor).load(carYaml1);
    68          String carYaml2 = new Yaml().dumpAs(car2, Tag.MAP, FlowStyle.AUTO);
    69          assertEquals(carYaml1, carYaml2);
    70      }
    71
    72      @SuppressWarnings("unchecked")
    73      public void testRootMap() throws IOException {
    74          Map<Object, Object> car1 = new HashMap<Object, Object>();
    75          car1.put("plate", "12-XP-F4");
    76          Wheel wheel = new Wheel();
    77          wheel.setId(2);
    78          car1.put("wheel", wheel);
    79          Map<String, Integer> map = new HashMap<String, Integer>();
    80          map.put("id", 3);
    81          car1.put("map", map);
    82          String carYaml1 = new Yaml().dump(car1);
    83          assertEquals(Util.getLocalResource("constructor/carwheel-root-map.yaml"), carYaml1);
    84          Map<Object, Object> car2 = (Map<Object, Object>) new Yaml().load(carYaml1);
    85          assertEquals(car1, car2);
    86          assertEquals(carYaml1, new Yaml().dump(car2));
    87      }
    88
    89      public void testLoadClassTag() throws IOException {
    90          Constructor constructor = new Constructor();
    91          constructor.addTypeDescription(new TypeDescription(Car.class, "!car"));
    92          Yaml yaml = new Yaml(constructor);
    93          Car car = (Car) yaml.load(Util.getLocalResource("constructor/car-without-tags.yaml"));
    94          assertEquals("12-XP-F4", car.getPlate());
    95          List<Wheel> wheels = car.getWheels();
    96          assertNotNull(wheels);
    97          assertEquals(5, wheels.size());
    98          Wheel w1 = wheels.get(0);
    99          assertEquals(1, w1.getId());
    100         //
    101         String carYaml1 = new Yaml().dump(car);
    102         assertTrue(carYaml1.startsWith("!!org.yaml.snakeyaml.constructor.Car"));
    103         //
    104         Representer representer = new Representer();
    105         representer.addClassTag(Car.class, new Tag("!car"));
    106         yaml = new Yaml(representer);
    107         String carYaml2 = yaml.dump(car);
    108         assertEquals(Util.getLocalResource("constructor/car-without-tags.yaml"), carYaml2);
    109     }
    110
    111     public static class CarWithWheel {
    112         private String plate;
    113         private String year;
    114         private Wheel wheel;
    115         private Object part;
    116         private Map<String, Integer> map;
    117
    118         public String getPlate() {
    119             return plate;
    120         }
    121
    122         public void setPlate(String plate) {
    123             this.plate = plate;
    124         }
    125
    126         public Wheel getWheel() {
    127             return wheel;
    128         }
    129
    130         public void setWheel(Wheel wheel) {
    131             this.wheel = wheel;
    132         }
    133
    134         public Map<String, Integer> getMap() {
    135             return map;
    136         }
    137
    138         public void setMap(Map<String, Integer> map) {
    139             this.map = map;
    140         }
    141
    142         public Object getPart() {
    143             return part;
    144         }
    145
    146         public void setPart(Object part) {
    147             this.part = part;
    148         }
    149
    150         public String getYear() {
    151             return year;
    152         }
    153
    154         public void setYear(String year) {
    155             this.year = year;
    156         }
    157     }
    158 }