Home | History | Annotate | Download | only in examples
      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 examples;
     17 
     18 import java.util.List;
     19 
     20 import junit.framework.TestCase;
     21 
     22 import org.yaml.snakeyaml.Yaml;
     23 import org.yaml.snakeyaml.constructor.SafeConstructor;
     24 
     25 public class SafeConstructorExampleTest extends TestCase {
     26     @SuppressWarnings("unchecked")
     27     public void testConstruct() {
     28         String doc = "- 5\n- Person\n- true";
     29         Yaml yaml = new Yaml(new SafeConstructor());
     30         List<Object> list = (List<Object>) yaml.load(doc);
     31         assertEquals(3, list.size());
     32         assertEquals(new Integer(5), list.get(0));
     33         assertEquals("Person", list.get(1));
     34         assertEquals(Boolean.TRUE, list.get(2));
     35     }
     36 
     37     public void testSafeConstruct() {
     38         String doc = "- 5\n- !org.yaml.snakeyaml.constructor.Person\n  firstName: Andrey\n  age: 99\n- true";
     39         Yaml yaml = new Yaml(new SafeConstructor());
     40         try {
     41             yaml.load(doc);
     42             fail("Custom Java classes should not be created.");
     43         } catch (Exception e) {
     44             assertEquals(
     45                     "could not determine a constructor for the tag !org.yaml.snakeyaml.constructor.Person\n"
     46                             + " in 'string', line 2, column 3:\n"
     47                             + "    - !org.yaml.snakeyaml.constructor. ... \n" + "      ^\n",
     48                     e.getMessage());
     49         }
     50     }
     51 }
     52