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.util.Iterator;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.error.YAMLException;
24  
25  public class YamlTest extends TestCase {
26  
27      public void testSetNoName() {
28          Yaml yaml = new Yaml();
29          assertTrue(yaml.toString().matches("Yaml:\\d+"));
30      }
31  
32      public void testSetName() {
33          Yaml yaml = new Yaml();
34          yaml.setName("REST");
35          assertEquals("REST", yaml.getName());
36          assertEquals("REST", yaml.toString());
37      }
38  
39      /**
40       * Check that documents are parsed only when they are asked to be loaded.
41       */
42      public void testOneDocument() {
43          Yaml yaml = new Yaml();
44          String doc = "--- a\n--- [:]";
45          Iterator<Object> loaded = yaml.loadAll(doc).iterator();
46          assertTrue(loaded.hasNext());
47          Object obj1 = loaded.next();
48          assertEquals("a", obj1);
49          assertTrue(loaded.hasNext());
50          try {
51              loaded.next();
52              fail("Second document is invalid");
53          } catch (Exception e) {
54              assertEquals("while parsing a flow node; expected the node content, but found Value",
55                      e.getMessage());
56          }
57      }
58  
59      public void testOnlyOneDocument() {
60          Yaml yaml = new Yaml();
61          String doc = "--- a\n--- b";
62          try {
63              yaml.load(doc);
64              fail("It must be only one document.");
65          } catch (YAMLException e) {
66              assertEquals("expected a single document in the stream; but found another document",
67                      e.getMessage());
68          }
69      }
70  }