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 */
16package examples.collections;
17
18import java.util.Iterator;
19import java.util.LinkedHashMap;
20import java.util.Map;
21
22import junit.framework.TestCase;
23
24import org.yaml.snakeyaml.Util;
25import org.yaml.snakeyaml.Yaml;
26
27/**
28 * Test MapBean->Map<Enum, Developer> developers <br/>
29 * Developer class must be properly recognised
30 */
31public class TypeSafeMap2Test extends TestCase {
32    public void testDumpMap() {
33        MapBean2 bean = new MapBean2();
34        Map<Developer2, Color> data = new LinkedHashMap<Developer2, Color>();
35        data.put(new Developer2("Andy", "tester"), Color.BLACK);
36        data.put(new Developer2("Lisa", "owner"), Color.RED);
37        bean.setData(data);
38        Map<Color, Developer2> developers = new LinkedHashMap<Color, Developer2>();
39        developers.put(Color.WHITE, new Developer2("Fred", "creator"));
40        developers.put(Color.BLACK, new Developer2("John", "committer"));
41        bean.setDevelopers(developers);
42        Yaml yaml = new Yaml();
43        String output = yaml.dumpAsMap(bean);
44        // System.out.println(output);
45        String etalon = Util.getLocalResource("examples/map-bean-12.yaml");
46        assertEquals(etalon, output);
47    }
48
49    public void testMap2() {
50        MapBean2 bean = new MapBean2();
51        Map<Developer2, Color> data = new LinkedHashMap<Developer2, Color>();
52        data.put(new Developer2("Andy", "tester"), Color.BLACK);
53        data.put(new SuperMan("Bill", "cleaner", false), Color.BLACK);
54        data.put(new Developer2("Lisa", "owner"), Color.RED);
55        bean.setData(data);
56        Map<Color, Developer2> developers = new LinkedHashMap<Color, Developer2>();
57        developers.put(Color.WHITE, new Developer2("Fred", "creator"));
58        developers.put(Color.RED, new SuperMan("Jason", "contributor", true));
59        developers.put(Color.BLACK, new Developer2("John", "committer"));
60        bean.setDevelopers(developers);
61        Yaml yaml = new Yaml();
62        String output = yaml.dumpAsMap(bean);
63        // System.out.println(output);
64        String etalon = Util.getLocalResource("examples/map-bean-13.yaml");
65        assertEquals(etalon, output);
66        // load
67        Yaml beanLoader = new Yaml();
68        MapBean2 parsed = beanLoader.loadAs(etalon, MapBean2.class);
69        assertNotNull(parsed);
70        Map<Developer2, Color> parsedData = parsed.getData();
71        assertEquals(3, parsedData.size());
72        assertTrue(parsedData.containsKey(new SuperMan("Bill", "cleaner", false)));
73        assertEquals(Color.BLACK, parsedData.get(new SuperMan("Bill", "cleaner", false)));
74        //
75        Map<Color, Developer2> parsedDevelopers = parsed.getDevelopers();
76        assertEquals(3, parsedDevelopers.size());
77        assertEquals(new SuperMan("Jason", "contributor", true), parsedDevelopers.get(Color.RED));
78    }
79
80    public void testLoadMap() {
81        String output = Util.getLocalResource("examples/map-bean-12.yaml");
82        // System.out.println(output);
83        Yaml beanLoader = new Yaml();
84        MapBean2 parsed = beanLoader.loadAs(output, MapBean2.class);
85        assertNotNull(parsed);
86        Map<Developer2, Color> data = parsed.getData();
87        assertEquals(2, data.size());
88        Iterator<Developer2> iter = data.keySet().iterator();
89        Developer2 first = iter.next();
90        assertEquals("Andy", first.getName());
91        assertEquals("tester", first.getRole());
92        assertEquals(Color.BLACK, data.get(first));
93        Developer2 second = iter.next();
94        assertEquals("Lisa", second.getName());
95        assertEquals("owner", second.getRole());
96        assertEquals(Color.RED, data.get(second));
97        //
98        Map<Color, Developer2> developers = parsed.getDevelopers();
99        assertEquals(2, developers.size());
100        Iterator<Color> iter2 = developers.keySet().iterator();
101        Color firstColor = iter2.next();
102        assertEquals(Color.WHITE, firstColor);
103        Developer2 dev1 = developers.get(firstColor);
104        assertEquals("Fred", dev1.getName());
105        assertEquals("creator", dev1.getRole());
106        Color secondColor = iter2.next();
107        assertEquals(Color.BLACK, secondColor);
108        Developer2 dev2 = developers.get(secondColor);
109        assertEquals("John", dev2.getName());
110        assertEquals("committer", dev2.getRole());
111    }
112
113    public static enum Color {
114        WHITE, BLACK, RED;
115    }
116
117    public static class MapBean2 {
118        private Map<Developer2, Color> data;
119        private String name;
120        private Map<Color, Developer2> developers;
121
122        public MapBean2() {
123            name = "Bean123";
124        }
125
126        public String getName() {
127            return name;
128        }
129
130        public void setName(String name) {
131            this.name = name;
132        }
133
134        public Map<Color, Developer2> getDevelopers() {
135            return developers;
136        }
137
138        public void setDevelopers(Map<Color, Developer2> developers) {
139            this.developers = developers;
140        }
141
142        public Map<Developer2, Color> getData() {
143            return data;
144        }
145
146        public void setData(Map<Developer2, Color> data) {
147            this.data = data;
148        }
149
150    }
151
152    public static class Developer2 implements Comparable<Developer2> {
153        private String name;
154        private String role;
155
156        public Developer2() {
157        }
158
159        private Developer2(String name, String role) {
160            this.name = name;
161            this.role = role;
162        }
163
164        public String getName() {
165            return name;
166        }
167
168        public void setName(String name) {
169            this.name = name;
170        }
171
172        public String getRole() {
173            return role;
174        }
175
176        public void setRole(String role) {
177            this.role = role;
178        }
179
180        public int compareTo(Developer2 o) {
181            return name.compareTo(o.name);
182        }
183
184        @Override
185        public boolean equals(Object obj) {
186            if (obj instanceof Developer2) {
187                return toString().equals(obj.toString());
188            } else {
189                return false;
190            }
191        }
192
193        @Override
194        public int hashCode() {
195            return toString().hashCode();
196        }
197
198        @Override
199        public String toString() {
200            return "Developer " + name + " " + role;
201        }
202
203    }
204
205    public static class SuperMan extends Developer2 {
206        private boolean smart;
207
208        public SuperMan() {
209            super();
210        }
211
212        private SuperMan(String name, String role, boolean smart) {
213            super(name, role);
214            this.smart = smart;
215        }
216
217        public boolean isSmart() {
218            return smart;
219        }
220
221        public void setSmart(boolean smart) {
222            this.smart = smart;
223        }
224
225        @Override
226        public String toString() {
227            return "Super" + super.toString();
228        }
229    }
230}
231