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