Home | History | Annotate | Download | only in snakeyaml
      1 /**
      2  * Copyright (c) 2008, 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("while parsing a flow node\n" + " in 'reader', line 2, column 6:\n"
     54                     + "    --- [:]\n" + "         ^\n"
     55                     + "expected the node content, but found Value\n"
     56                     + " in 'reader', line 2, column 6:\n" + "    --- [:]\n" + "         ^\n",
     57                     e.getMessage());
     58         }
     59     }
     60 
     61     public void testOnlyOneDocument() {
     62         Yaml yaml = new Yaml();
     63         String doc = "--- a\n--- b";
     64         try {
     65             yaml.load(doc);
     66             fail("It must be only one document.");
     67         } catch (YAMLException e) {
     68             assertEquals("expected a single document in the stream\n"
     69                     + " in 'string', line 1, column 5:\n" + "    --- a\n" + "        ^\n"
     70                     + "but found another document\n" + " in 'string', line 2, column 1:\n"
     71                     + "    --- b\n" + "    ^\n", e.getMessage());
     72         }
     73     }
     74 }
     75