1b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov/**
211a89b445f3bde56bf07e6a0d04f0b0256dcb215Andrey Somov * Copyright (c) 2008, http://www.snakeyaml.org
3b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov *
4b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * Licensed under the Apache License, Version 2.0 (the "License");
5b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * you may not use this file except in compliance with the License.
6b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * You may obtain a copy of the License at
7b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov *
8b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov *     http://www.apache.org/licenses/LICENSE-2.0
9b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov *
10b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * Unless required by applicable law or agreed to in writing, software
11b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * distributed under the License is distributed on an "AS IS" BASIS,
12b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * See the License for the specific language governing permissions and
14b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * limitations under the License.
15b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov */
16b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovpackage org.yaml.snakeyaml.constructor;
17b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
18b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.util.HashMap;
197be8e3a40e03237e7c8c7d862d8ef511ae2d7810maslovaleximport java.util.LinkedHashMap;
20b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.util.List;
21b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.util.Map;
22b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
23b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport junit.framework.TestCase;
24b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
25b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport org.yaml.snakeyaml.DumperOptions.FlowStyle;
26b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport org.yaml.snakeyaml.TypeDescription;
27b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport org.yaml.snakeyaml.Util;
28b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport org.yaml.snakeyaml.Yaml;
29b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport org.yaml.snakeyaml.nodes.Tag;
30b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport org.yaml.snakeyaml.representer.Representer;
31b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
32b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovpublic class ImplicitTagsTest extends TestCase {
33b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
34b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public void testDefaultRepresenter() {
35b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        CarWithWheel car1 = new CarWithWheel();
36b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        car1.setPlate("12-XP-F4");
37b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Wheel wheel = new Wheel();
38b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        wheel.setId(2);
39b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        car1.setWheel(wheel);
40b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Map<String, Integer> map = new HashMap<String, Integer>();
41b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        map.put("id", 3);
42b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        car1.setMap(map);
43b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        car1.setPart(new Wheel(4));
44b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        car1.setYear("2008");
45b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        String carYaml1 = new Yaml().dump(car1);
46b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertEquals(Util.getLocalResource("constructor/carwheel-without-tags.yaml"), carYaml1);
47b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        CarWithWheel car2 = (CarWithWheel) new Yaml().load(carYaml1);
48b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        String carYaml2 = new Yaml().dump(car2);
49b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertEquals(carYaml1, carYaml2);
50b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    }
51b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
52b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public void testNoRootTag() {
53b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        CarWithWheel car1 = new CarWithWheel();
54b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        car1.setPlate("12-XP-F4");
55b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Wheel wheel = new Wheel();
56b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        wheel.setId(2);
57b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        car1.setWheel(wheel);
58b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Map<String, Integer> map = new HashMap<String, Integer>();
59b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        map.put("id", 3);
60b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        car1.setMap(map);
61b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        car1.setYear("2008");
62b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        String carYaml1 = new Yaml().dumpAs(car1, Tag.MAP, FlowStyle.AUTO);
63b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertEquals(Util.getLocalResource("constructor/car-without-root-tag.yaml"), carYaml1);
64b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        //
65b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Constructor contructor = new Constructor(CarWithWheel.class);
66b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        CarWithWheel car2 = (CarWithWheel) new Yaml(contructor).load(carYaml1);
67b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        String carYaml2 = new Yaml().dumpAs(car2, Tag.MAP, FlowStyle.AUTO);
68b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertEquals(carYaml1, carYaml2);
69b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    }
70b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
71b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    @SuppressWarnings("unchecked")
72b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public void testRootMap() {
737be8e3a40e03237e7c8c7d862d8ef511ae2d7810maslovalex        Map<Object, Object> car1 = new LinkedHashMap<Object, Object>();
74b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Wheel wheel = new Wheel();
75b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        wheel.setId(2);
76b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Map<String, Integer> map = new HashMap<String, Integer>();
77b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        map.put("id", 3);
787be8e3a40e03237e7c8c7d862d8ef511ae2d7810maslovalex
797be8e3a40e03237e7c8c7d862d8ef511ae2d7810maslovalex        car1.put("wheel", wheel);
80b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        car1.put("map", map);
817be8e3a40e03237e7c8c7d862d8ef511ae2d7810maslovalex        car1.put("plate", "12-XP-F4");
827be8e3a40e03237e7c8c7d862d8ef511ae2d7810maslovalex
83b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        String carYaml1 = new Yaml().dump(car1);
84b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertEquals(Util.getLocalResource("constructor/carwheel-root-map.yaml"), carYaml1);
85b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Map<Object, Object> car2 = (Map<Object, Object>) new Yaml().load(carYaml1);
86b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertEquals(car1, car2);
87b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertEquals(carYaml1, new Yaml().dump(car2));
88b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    }
89b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
90b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public void testLoadClassTag() {
91b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Constructor constructor = new Constructor();
92b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        constructor.addTypeDescription(new TypeDescription(Car.class, "!car"));
93b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Yaml yaml = new Yaml(constructor);
94b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Car car = (Car) yaml.load(Util.getLocalResource("constructor/car-without-tags.yaml"));
95b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertEquals("12-XP-F4", car.getPlate());
96b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        List<Wheel> wheels = car.getWheels();
97b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertNotNull(wheels);
98b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertEquals(5, wheels.size());
99b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Wheel w1 = wheels.get(0);
100b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertEquals(1, w1.getId());
101b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        //
102b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        String carYaml1 = new Yaml().dump(car);
103b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertTrue(carYaml1.startsWith("!!org.yaml.snakeyaml.constructor.Car"));
104b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        //
105b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        Representer representer = new Representer();
106b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        representer.addClassTag(Car.class, new Tag("!car"));
107b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        yaml = new Yaml(representer);
108b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        String carYaml2 = yaml.dump(car);
109b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        assertEquals(Util.getLocalResource("constructor/car-without-tags.yaml"), carYaml2);
110b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    }
111b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
112b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    public static class CarWithWheel {
113b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        private String plate;
114b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        private String year;
115b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        private Wheel wheel;
116b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        private Object part;
117b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        private Map<String, Integer> map;
118b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
119b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        public String getPlate() {
120b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            return plate;
121b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
122b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
123b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        public void setPlate(String plate) {
124b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            this.plate = plate;
125b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
126b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
127b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        public Wheel getWheel() {
128b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            return wheel;
129b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
130b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
131b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        public void setWheel(Wheel wheel) {
132b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            this.wheel = wheel;
133b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
134b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
135b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        public Map<String, Integer> getMap() {
136b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            return map;
137b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
138b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
139b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        public void setMap(Map<String, Integer> map) {
140b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            this.map = map;
141b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
142b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
143b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        public Object getPart() {
144b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            return part;
145b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
146b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
147b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        public void setPart(Object part) {
148b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            this.part = part;
149b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
150b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
151b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        public String getYear() {
152b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            return year;
153b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
154b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov
155b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        public void setYear(String year) {
156b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov            this.year = year;
157b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov        }
158b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov    }
159b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov}
160