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