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