1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package org.yaml.snakeyaml; 15 16 import java.io.StringReader; 17 import junit.framework.TestCase; 18 import org.yaml.snakeyaml.nodes.MappingNode; 19 import org.yaml.snakeyaml.nodes.Node; 20 import org.yaml.snakeyaml.nodes.NodeId; 21 import org.yaml.snakeyaml.nodes.ScalarNode; 22 23 public class YamlComposeTest extends TestCase { 24 testComposeManyDocuments()25 public void testComposeManyDocuments() { 26 try { 27 Yaml yaml = new Yaml(); 28 yaml.compose(new StringReader("abc: 56\n---\n123\n---\n456")); 29 fail("YAML contans more then one document."); 30 } catch (Exception e) { 31 assertTrue("<<<" + e.getMessage() + ">>>", 32 e.getMessage().startsWith("expected a single document in the stream")); 33 } 34 } 35 testComposeFromReader()36 public void testComposeFromReader() { 37 Yaml yaml = new Yaml(); 38 MappingNode node = (MappingNode) yaml.compose(new StringReader("abc: 56")); 39 ScalarNode node1 = (ScalarNode) node.getValue().get(0).getKeyNode(); 40 assertEquals("abc", node1.getValue()); 41 ScalarNode node2 = (ScalarNode) node.getValue().get(0).getValueNode(); 42 assertEquals("56", node2.getValue()); 43 } 44 testComposeAllFromReader()45 public void testComposeAllFromReader() { 46 Yaml yaml = new Yaml(); 47 boolean first = true; 48 for (Node node : yaml.composeAll(new StringReader("abc: 56\n---\n123\n---\n456"))) { 49 if (first) { 50 assertEquals(NodeId.mapping, node.getNodeId()); 51 } else { 52 assertEquals(NodeId.scalar, node.getNodeId()); 53 } 54 first = false; 55 } 56 } 57 testComposeAllOneDocument()58 public void testComposeAllOneDocument() { 59 Yaml yaml = new Yaml(); 60 for (Node node : yaml.composeAll(new StringReader("6"))) { 61 assertEquals(NodeId.scalar, node.getNodeId()); 62 } 63 } 64 } 65