1 /** 2 * Copyright (c) 2008, 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 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 YamlStream(String sourceName)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 // try to read generated presentation to prove that the presentation 48 // is identical to the source 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 getNativeData()92 public List<Object> getNativeData() { 93 return nativeData; 94 } 95 } 96