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