1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.yaml.snakeyaml.issues.issue82;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Map;
23 import java.util.Set;
24
25 import junit.framework.TestCase;
26
27 import org.yaml.snakeyaml.TypeDescription;
28 import org.yaml.snakeyaml.Yaml;
29 import org.yaml.snakeyaml.constructor.Constructor;
30 import org.yaml.snakeyaml.nodes.Tag;
31 import org.yaml.snakeyaml.representer.Representer;
32
33
34
35
36 public class PropOrderInfluenceWhenAliasedInGenericCollectionTest extends TestCase {
37
38 public static interface Account {
39 }
40
41 public static class GeneralAccount implements Account {
42 public String name = "General";
43 }
44
45 public static class SuperSaverAccount extends GeneralAccount {
46
47 public SuperSaverAccount() {
48 name = "SuperSaver";
49 }
50 }
51
52 public static class CustomerAB {
53 public Collection<Account> aAll;
54 public Collection<GeneralAccount> bGeneral;
55
56 @Override
57 public String toString() {
58 return "CustomerAB";
59 }
60 }
61
62 public static class CustomerBA {
63 public Collection<GeneralAccount> aGeneral;
64 public Collection<Account> bAll;
65 }
66
67 public static class CustomerAB_MapValue {
68 public Collection<Account> aAll;
69 public Map<String, GeneralAccount> bGeneralMap;
70
71 @Override
72 public String toString() {
73 return "CustomerAB_MapValue";
74 }
75 }
76
77 public static class CustomerAB_MapKey {
78 public Collection<Account> aAll;
79 public Map<GeneralAccount, String> bGeneralMap;
80
81 @Override
82 public String toString() {
83 return "CustomerAB_MapKey";
84 }
85 }
86
87 public static class CustomerAB_Property {
88 public Account acc;
89 public Collection<GeneralAccount> bGeneral;
90
91 @Override
92 public String toString() {
93 return "CustomerAB_Property";
94 }
95 }
96
97 public void testAB() {
98 SuperSaverAccount supersaver = new SuperSaverAccount();
99 GeneralAccount generalAccount = new GeneralAccount();
100
101 CustomerAB customerAB = new CustomerAB();
102 ArrayList<Account> all = new ArrayList<Account>();
103 all.add(supersaver);
104 all.add(generalAccount);
105 ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
106 general.add(generalAccount);
107 general.add(supersaver);
108
109 customerAB.aAll = all;
110 customerAB.bGeneral = general;
111
112 Yaml yaml = new Yaml();
113 String dump = yaml.dump(customerAB);
114
115 CustomerAB parsed = (CustomerAB) yaml.load(dump);
116 assertNotNull(parsed);
117 }
118
119 public void testAB_Set() {
120 SuperSaverAccount supersaver = new SuperSaverAccount();
121 GeneralAccount generalAccount = new GeneralAccount();
122
123 CustomerAB customerAB = new CustomerAB();
124 ArrayList<Account> all = new ArrayList<Account>();
125 all.add(supersaver);
126 all.add(generalAccount);
127 Set<GeneralAccount> general = new HashSet<GeneralAccount>();
128 general.add(generalAccount);
129 general.add(supersaver);
130
131 customerAB.aAll = all;
132 customerAB.bGeneral = general;
133
134 Yaml yaml = new Yaml();
135 String dump = yaml.dump(customerAB);
136
137 CustomerAB parsed = (CustomerAB) yaml.load(dump);
138 assertNotNull(parsed);
139 }
140
141 public void testABWithCustomTag() {
142 SuperSaverAccount supersaver = new SuperSaverAccount();
143 GeneralAccount generalAccount = new GeneralAccount();
144
145 CustomerAB customerAB = new CustomerAB();
146 ArrayList<Account> all = new ArrayList<Account>();
147 all.add(supersaver);
148 all.add(generalAccount);
149 ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
150 general.add(generalAccount);
151 general.add(supersaver);
152
153 customerAB.aAll = all;
154 customerAB.bGeneral = general;
155
156 Constructor constructor = new Constructor();
157 Representer representer = new Representer();
158 Tag generalAccountTag = new Tag("!GA");
159 constructor
160 .addTypeDescription(new TypeDescription(GeneralAccount.class, generalAccountTag));
161 representer.addClassTag(GeneralAccount.class, generalAccountTag);
162
163 Yaml yaml = new Yaml(constructor, representer);
164 String dump = yaml.dump(customerAB);
165
166 CustomerAB parsed = (CustomerAB) yaml.load(dump);
167 assertNotNull(parsed);
168 }
169
170 public void testABProperty() {
171 SuperSaverAccount supersaver = new SuperSaverAccount();
172 GeneralAccount generalAccount = new GeneralAccount();
173
174 CustomerAB_Property customerAB_property = new CustomerAB_Property();
175 ArrayList<Account> all = new ArrayList<Account>();
176 all.add(supersaver);
177 all.add(generalAccount);
178 ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
179 general.add(generalAccount);
180 general.add(supersaver);
181
182 customerAB_property.acc = generalAccount;
183 customerAB_property.bGeneral = general;
184
185 Constructor constructor = new Constructor();
186 Representer representer = new Representer();
187
188 Yaml yaml = new Yaml(constructor, representer);
189 String dump = yaml.dump(customerAB_property);
190
191 CustomerAB_Property parsed = (CustomerAB_Property) yaml.load(dump);
192 assertNotNull(parsed);
193 }
194
195 public void testABPropertyWithCustomTag() {
196 SuperSaverAccount supersaver = new SuperSaverAccount();
197 GeneralAccount generalAccount = new GeneralAccount();
198
199 CustomerAB_Property customerAB_property = new CustomerAB_Property();
200 ArrayList<Account> all = new ArrayList<Account>();
201 all.add(supersaver);
202 all.add(generalAccount);
203 ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
204 general.add(generalAccount);
205 general.add(supersaver);
206
207 customerAB_property.acc = generalAccount;
208 customerAB_property.bGeneral = general;
209
210 Constructor constructor = new Constructor();
211 Representer representer = new Representer();
212
213 Tag generalAccountTag = new Tag("!GA");
214 constructor
215 .addTypeDescription(new TypeDescription(GeneralAccount.class, generalAccountTag));
216 representer.addClassTag(GeneralAccount.class, generalAccountTag);
217
218 Yaml yaml = new Yaml(constructor, representer);
219 String dump = yaml.dump(customerAB_property);
220
221 CustomerAB_Property parsed = (CustomerAB_Property) yaml.load(dump);
222 assertNotNull(parsed);
223 }
224
225 public void testABwithJavaBeanHelpers() {
226 SuperSaverAccount supersaver = new SuperSaverAccount();
227 GeneralAccount generalAccount = new GeneralAccount();
228
229 CustomerAB customerAB = new CustomerAB();
230 ArrayList<Account> all = new ArrayList<Account>();
231 all.add(supersaver);
232 all.add(generalAccount);
233 ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
234 general.add(generalAccount);
235 general.add(supersaver);
236
237 customerAB.aAll = all;
238 customerAB.bGeneral = general;
239
240 Yaml yaml = new Yaml();
241 String dump2 = yaml.dumpAsMap(customerAB);
242
243 Yaml loader = new Yaml();
244 CustomerAB parsed = loader.loadAs(dump2, CustomerAB.class);
245 assertNotNull(parsed);
246 }
247
248 public void testAB_asMapValue() {
249 SuperSaverAccount supersaver = new SuperSaverAccount();
250 GeneralAccount generalAccount = new GeneralAccount();
251
252 CustomerAB_MapValue customerAB_mapValue = new CustomerAB_MapValue();
253 ArrayList<Account> all = new ArrayList<Account>();
254 all.add(supersaver);
255 all.add(generalAccount);
256 Map<String, GeneralAccount> generalMap = new HashMap<String, GeneralAccount>();
257 generalMap.put(generalAccount.name, generalAccount);
258 generalMap.put(supersaver.name, supersaver);
259
260 customerAB_mapValue.aAll = all;
261 customerAB_mapValue.bGeneralMap = generalMap;
262
263 Yaml yaml = new Yaml();
264 String dump = yaml.dump(customerAB_mapValue);
265
266 CustomerAB_MapValue parsed = (CustomerAB_MapValue) yaml.load(dump);
267 assertNotNull(parsed);
268 }
269
270 public void testAB_asMapKey() {
271 SuperSaverAccount supersaver = new SuperSaverAccount();
272 GeneralAccount generalAccount = new GeneralAccount();
273
274 CustomerAB_MapKey customerAB_mapKey = new CustomerAB_MapKey();
275 ArrayList<Account> all = new ArrayList<Account>();
276 all.add(supersaver);
277 all.add(generalAccount);
278 Map<GeneralAccount, String> generalMap = new HashMap<GeneralAccount, String>();
279 generalMap.put(generalAccount, generalAccount.name);
280 generalMap.put(supersaver, supersaver.name);
281
282 customerAB_mapKey.aAll = all;
283 customerAB_mapKey.bGeneralMap = generalMap;
284
285 Yaml yaml = new Yaml();
286 String dump = yaml.dump(customerAB_mapKey);
287
288 CustomerAB_MapKey parsed = (CustomerAB_MapKey) yaml.load(dump);
289 assertNotNull(parsed);
290 }
291
292 public void testBA() {
293 SuperSaverAccount supersaver = new SuperSaverAccount();
294 GeneralAccount generalAccount = new GeneralAccount();
295
296 CustomerBA customerBA = new CustomerBA();
297 ArrayList<Account> all = new ArrayList<Account>();
298 all.add(supersaver);
299 all.add(generalAccount);
300 ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
301 general.add(generalAccount);
302 general.add(supersaver);
303
304 customerBA.aGeneral = general;
305 customerBA.bAll = all;
306
307 Yaml yaml = new Yaml();
308 String dump = yaml.dump(customerBA);
309
310
311 CustomerBA parsed = (CustomerBA) yaml.load(dump);
312 assertEquals(2, parsed.bAll.size());
313 assertEquals(2, parsed.aGeneral.size());
314 assertFalse(parsed.bAll.equals(parsed.aGeneral));
315 GeneralAccount[] array = parsed.aGeneral.toArray(new GeneralAccount[2]);
316 assertEquals(GeneralAccount.class, array[0].getClass());
317 assertEquals(SuperSaverAccount.class, array[1].getClass());
318 assertEquals("SuperSaver", array[1].name);
319 }
320 }