View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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.StringReader;
19  
20  import junit.framework.TestCase;
21  
22  import org.yaml.snakeyaml.nodes.MappingNode;
23  import org.yaml.snakeyaml.nodes.Node;
24  import org.yaml.snakeyaml.nodes.NodeId;
25  import org.yaml.snakeyaml.nodes.ScalarNode;
26  
27  public class YamlComposeTest extends TestCase {
28  
29      public void testComposeManyDocuments() {
30          try {
31              Yaml yaml = new Yaml();
32              yaml.compose(new StringReader("abc: 56\n---\n123\n---\n456"));
33              fail("YAML contans more then one document.");
34          } catch (Exception e) {
35              assertTrue(e.getMessage().startsWith(
36                      "expected a single document in the stream; but found another document"));
37          }
38      }
39  
40      public void testComposeFromReader() {
41          Yaml yaml = new Yaml();
42          MappingNode node = (MappingNode) yaml.compose(new StringReader("abc: 56"));
43          ScalarNode node1 = (ScalarNode) node.getValue().get(0).getKeyNode();
44          assertEquals("abc", node1.getValue());
45          ScalarNode node2 = (ScalarNode) node.getValue().get(0).getValueNode();
46          assertEquals("56", node2.getValue());
47      }
48  
49      public void testComposeAllFromReader() {
50          Yaml yaml = new Yaml();
51          boolean first = true;
52          for (Node node : yaml.composeAll(new StringReader("abc: 56\n---\n123\n---\n456"))) {
53              if (first) {
54                  assertEquals(NodeId.mapping, node.getNodeId());
55              } else {
56                  assertEquals(NodeId.scalar, node.getNodeId());
57              }
58              first = false;
59          }
60      }
61  
62      public void testComposeAllOneDocument() {
63          Yaml yaml = new Yaml();
64          for (Node node : yaml.composeAll(new StringReader("6"))) {
65              assertEquals(NodeId.scalar, node.getNodeId());
66          }
67      }
68  }