1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 "null; could not determine a constructor for the tag !org.yaml.snakeyaml.constructor.Person; in 'string', line 2, column 3:\n - !org.yaml.snakeyaml.constructor. ... \n ^",
46 e.getMessage());
47 }
48 }
49 }