1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package examples; 15 16 import java.util.List; 17 import junit.framework.TestCase; 18 import org.yaml.snakeyaml.Yaml; 19 import org.yaml.snakeyaml.constructor.SafeConstructor; 20 21 public class SafeConstructorExampleTest extends TestCase { 22 23 @SuppressWarnings("unchecked") testConstruct()24 public void testConstruct() { 25 String doc = "- 5\n- Person\n- true"; 26 Yaml yaml = new Yaml(new SafeConstructor()); 27 List<Object> list = yaml.load(doc); 28 assertEquals(3, list.size()); 29 assertEquals(Integer.valueOf(5), list.get(0)); 30 assertEquals("Person", list.get(1)); 31 assertEquals(Boolean.TRUE, list.get(2)); 32 } 33 testSafeConstruct()34 public void testSafeConstruct() { 35 String doc = 36 "- 5\n- !org.yaml.snakeyaml.constructor.Person\n firstName: Andrey\n age: 99\n- true"; 37 Yaml yaml = new Yaml(new SafeConstructor()); 38 try { 39 yaml.load(doc); 40 fail("Custom Java classes should not be created."); 41 } catch (Exception e) { 42 assertEquals( 43 "could not determine a constructor for the tag !org.yaml.snakeyaml.constructor.Person\n" 44 + " in 'string', line 2, column 3:\n" 45 + " - !org.yaml.snakeyaml.constructor. ... \n" + " ^\n", 46 e.getMessage()); 47 } 48 } 49 } 50