1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package examples.collections;
17
18 import java.util.HashSet;
19 import java.util.Iterator;
20 import java.util.Set;
21 import java.util.SortedSet;
22 import java.util.TreeSet;
23
24 import junit.framework.TestCase;
25
26 import org.yaml.snakeyaml.Util;
27 import org.yaml.snakeyaml.Yaml;
28
29
30
31
32 public class TypeSafeSetImplementationsTest extends TestCase {
33 public void testDumpSet() {
34 SetBean bean = new SetBean();
35 SortedSet<String> sortedSet = new TreeSet<String>();
36 sortedSet.add("two");
37 sortedSet.add("one");
38 sortedSet.add("three");
39 bean.setSorted(sortedSet);
40 SortedSet<Developer> developers = new TreeSet<Developer>();
41 developers.add(new Developer("John", "founder"));
42 developers.add(new Developer("Karl", "user"));
43 bean.setDevelopers(developers);
44 Yaml yaml = new Yaml();
45 String output = yaml.dumpAsMap(bean);
46
47 String etalon = Util.getLocalResource("examples/set-bean-1.yaml");
48 assertEquals(etalon, output);
49 }
50
51 public void testDumpSet2() {
52 SetBean bean = new SetBean();
53 SortedSet<String> sortedSet = new TreeSet<String>();
54 sortedSet.add("two");
55 sortedSet.add("one");
56 sortedSet.add("three");
57 bean.setSorted(sortedSet);
58 SortedSet<Developer> developers = new TreeSet<Developer>();
59 developers.add(new Developer("John", "founder"));
60 developers.add(new Developer("Karl", "user"));
61 developers.add(new SuperDeveloper("Bill", "super"));
62 bean.setDevelopers(developers);
63 Yaml yaml = new Yaml();
64 String output = yaml.dumpAsMap(bean);
65
66 String etalon = Util.getLocalResource("examples/set-bean-6.yaml");
67 assertEquals(etalon, output);
68 }
69
70 public void testLoadSet() {
71 String output = Util.getLocalResource("examples/set-bean-1.yaml");
72
73 Yaml beanLoader = new Yaml();
74 SetBean parsed = beanLoader.loadAs(output, SetBean.class);
75 assertNotNull(parsed);
76 SortedSet<String> sortedMap = parsed.getSorted();
77 assertEquals(3, sortedMap.size());
78 assertTrue(sortedMap.contains("one"));
79 assertTrue(sortedMap.contains("two"));
80 assertTrue(sortedMap.contains("three"));
81 String first = sortedMap.iterator().next();
82 assertEquals("one", first);
83
84 SortedSet<Developer> developers = parsed.getDevelopers();
85 assertEquals(2, developers.size());
86 assertEquals("John", developers.first().getName());
87 assertEquals("Karl", developers.last().getName());
88 }
89
90 public void testLoadSetReversed() {
91 String output = Util.getLocalResource("examples/set-bean-2.yaml");
92
93 Yaml beanLoader = new Yaml();
94 SetBean parsed = beanLoader.loadAs(output, SetBean.class);
95 assertNotNull(parsed);
96 SortedSet<String> sortedMap = parsed.getSorted();
97 assertEquals(3, sortedMap.size());
98 assertTrue(sortedMap.contains("one"));
99 assertTrue(sortedMap.contains("two"));
100 assertTrue(sortedMap.contains("three"));
101
102 assertEquals("one", sortedMap.first());
103 assertEquals("two", sortedMap.last());
104
105 SortedSet<Developer> developers = parsed.getDevelopers();
106 assertEquals(2, developers.size());
107 assertEquals("John", developers.first().getName());
108 assertEquals("Karl", developers.last().getName());
109 }
110
111 public static class SetBean {
112 private SortedSet<String> sorted;
113 private SortedSet<Developer> developers;
114 private String name;
115
116 public SetBean() {
117 name = "Bean123";
118 }
119
120 public SortedSet<String> getSorted() {
121 return sorted;
122 }
123
124 public void setSorted(SortedSet<String> sorted) {
125 this.sorted = sorted;
126 }
127
128 public String getName() {
129 return name;
130 }
131
132 public void setName(String name) {
133 this.name = name;
134 }
135
136 public SortedSet<Developer> getDevelopers() {
137 return developers;
138 }
139
140 public void setDevelopers(SortedSet<Developer> developers) {
141 this.developers = developers;
142 }
143 }
144
145 public static class Developer implements Comparable<Developer> {
146 private String name;
147 private String role;
148
149 public Developer() {
150 }
151
152 public Developer(String name, String role) {
153 this.name = name;
154 this.role = role;
155 }
156
157 public String getName() {
158 return name;
159 }
160
161 public void setName(String name) {
162 this.name = name;
163 }
164
165 public String getRole() {
166 return role;
167 }
168
169 public void setRole(String role) {
170 this.role = role;
171 }
172
173 public int compareTo(Developer o) {
174 return name.compareTo(o.name);
175 }
176 }
177
178 public static class SuperDeveloper extends Developer {
179
180 public SuperDeveloper() {
181 super();
182 }
183
184 public SuperDeveloper(String string, String string2) {
185 super(string, string2);
186 }
187
188 }
189
190 @SuppressWarnings("unchecked")
191 public void testNoJavaBeanSetRecursive() {
192 Set<Object> set = new HashSet<Object>(3);
193 set.add("aaa");
194 set.add(111);
195 Box box = new Box();
196 box.setId("id123");
197 box.setSet(set);
198 set.add(box);
199 Yaml yaml = new Yaml();
200 String output = yaml.dump(set);
201
202
203
204
205 assertTrue(output.contains("&id001 !!set"));
206 assertTrue(output.contains("? !!examples.collections.TypeSafeSetImplementationsTest$Box"));
207 assertTrue(output.contains("set: *id001"));
208 assertTrue(output.contains("111: null"));
209
210 Set<Object> list2 = (Set<Object>) yaml.load(output);
211 assertEquals(3, list2.size());
212 assertTrue(list2.contains("aaa"));
213 assertTrue(list2.contains(111));
214 }
215
216 public static class Box {
217 private String id;
218 private Set<Object> set;
219
220 public String getId() {
221 return id;
222 }
223
224 public void setId(String id) {
225 this.id = id;
226 }
227
228 public Set<Object> getSet() {
229 return set;
230 }
231
232 public void setSet(Set<Object> set) {
233 this.set = set;
234 }
235 }
236
237 @SuppressWarnings("unchecked")
238 public void testNoJavaBeanSet() {
239 Yaml yaml = new Yaml();
240 String output = Util.getLocalResource("examples/set-bean-4.yaml");
241
242
243 Set<String> set = (Set<String>) yaml.load(output);
244 assertEquals(3, set.size());
245 assertTrue(set.contains("aaa"));
246 assertTrue(set.contains("bbb"));
247 assertTrue(set.contains("zzz"));
248 Iterator<String> iter = set.iterator();
249 assertEquals("bbb", iter.next());
250 assertEquals("aaa", iter.next());
251 assertEquals("zzz", iter.next());
252 }
253
254 @SuppressWarnings("unchecked")
255 public void testNoJavaBeanSet2() {
256 Yaml yaml = new Yaml();
257 String output = Util.getLocalResource("examples/set-bean-5.yaml");
258
259
260 Set<String> set = (Set<String>) yaml.load(output);
261 assertEquals(3, set.size());
262 assertTrue(set.contains("aaa"));
263 assertTrue(set.contains("bbb"));
264 assertTrue(set.contains("zzz"));
265 Iterator<String> iter = set.iterator();
266 assertEquals("aaa", iter.next());
267 assertEquals("bbb", iter.next());
268 assertEquals("zzz", iter.next());
269 }
270 }