1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.yaml.snakeyaml;
18
19 import java.io.ByteArrayOutputStream;
20 import java.io.InputStream;
21 import java.io.OutputStreamWriter;
22 import java.io.UnsupportedEncodingException;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27
28 import junit.framework.AssertionFailedError;
29
30 public class YamlStream {
31 private List<Object> nativeData = new ArrayList<Object>();
32
33 public YamlStream(String sourceName) {
34 InputStream input = YamlDocument.class.getClassLoader().getResourceAsStream(
35 YamlDocument.ROOT + sourceName);
36 Yaml yaml = new Yaml();
37 for (Object document : yaml.loadAll(input)) {
38 nativeData.add(document);
39 }
40 ByteArrayOutputStream output = new ByteArrayOutputStream();
41 yaml.dumpAll(nativeData.iterator(), new OutputStreamWriter(output));
42 String presentation;
43 try {
44 presentation = output.toString("UTF-8");
45 } catch (UnsupportedEncodingException e) {
46 throw new RuntimeException(e);
47 }
48
49
50 List<Object> parsedNativeData = new ArrayList<Object>();
51 for (Object document : yaml.loadAll(presentation)) {
52 parsedNativeData.add(document);
53 }
54 if (nativeData.getClass() != parsedNativeData.getClass()) {
55 throw new AssertionFailedError("Different class: " + parsedNativeData.getClass());
56 }
57 if (nativeData.size() != parsedNativeData.size()) {
58 throw new AssertionFailedError("Different size.");
59 }
60 Iterator<Object> piterator = parsedNativeData.iterator();
61 Iterator<Object> niterator = nativeData.iterator();
62 while (piterator.hasNext()) {
63 Object obj1 = niterator.next();
64 Object obj2 = piterator.next();
65 if (obj1 instanceof Map) {
66 @SuppressWarnings("unchecked")
67 Map<Object, Object> map1 = (Map<Object, Object>) obj1;
68 @SuppressWarnings("unchecked")
69 Map<Object, Object> map2 = (Map<Object, Object>) obj2;
70 if (!map1.keySet().equals(map2.keySet())) {
71 throw new AssertionFailedError("Keyset: " + map1.keySet() + "; but was: "
72 + map2.keySet());
73 }
74 for (Iterator<Object> iterator = map1.keySet().iterator(); iterator.hasNext();) {
75 Object key = iterator.next();
76 Object o1 = map1.get(key);
77 Object o2 = map2.get(key);
78 if (!o1.equals(o2)) {
79 throw new AssertionFailedError("Values: " + o1 + "; but was: " + o2);
80 }
81 }
82 }
83 if (!obj1.equals(obj2)) {
84 throw new AssertionFailedError("Expected: " + obj1 + "; but was: " + obj2);
85 }
86 }
87 if (!parsedNativeData.equals(nativeData)) {
88 throw new AssertionFailedError("Generated presentation is not the same: "
89 + presentation);
90 }
91 }
92
93 public List<Object> getNativeData() {
94 return nativeData;
95 }
96 }