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