View Javadoc

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