1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package examples;
18
19 import java.io.IOException;
20 import java.util.List;
21
22 import junit.framework.TestCase;
23
24 import org.yaml.snakeyaml.Yaml;
25 import org.yaml.snakeyaml.constructor.SafeConstructor;
26
27 public class SafeConstructorExampleTest extends TestCase {
28 @SuppressWarnings("unchecked")
29 public void testConstruct() throws IOException {
30 String doc = "- 5\n- Person\n- true";
31 Yaml yaml = new Yaml(new SafeConstructor());
32 List<Object> list = (List<Object>) yaml.load(doc);
33 assertEquals(3, list.size());
34 assertEquals(new Integer(5), list.get(0));
35 assertEquals("Person", list.get(1));
36 assertEquals(Boolean.TRUE, list.get(2));
37 }
38
39 public void testSafeConstruct() throws IOException {
40 String doc = "- 5\n- !org.yaml.snakeyaml.constructor.Person\n firstName: Andrey\n age: 99\n- true";
41 Yaml yaml = new Yaml(new SafeConstructor());
42 try {
43 yaml.load(doc);
44 fail("Custom Java classes should not be created.");
45 } catch (Exception e) {
46 assertEquals(
47 "null; could not determine a constructor for the tag !org.yaml.snakeyaml.constructor.Person",
48 e.getMessage());
49 }
50 }
51 }