/marathon-support/snakeyaml/src/test/java/org/yaml/snakeyaml/YamlStream.java

https://github.com/bakerbaker/Marathon · Java · 95 lines · 72 code · 6 blank · 17 comment · 13 complexity · 605551ea6ef13b7c0a363a1c23e60d9e MD5 · raw file

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