1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.yaml.snakeyaml.issues.issue154;
17
18 import junit.framework.TestCase;
19
20 import org.yaml.snakeyaml.Yaml;
21 import org.yaml.snakeyaml.constructor.Constructor;
22 import org.yaml.snakeyaml.error.YAMLException;
23 import org.yaml.snakeyaml.representer.Representer;
24
25 public class MissingPropertyTest extends TestCase {
26
27 private Yaml yaml;
28
29 public void setUp() {
30 yaml = new Yaml();
31 }
32
33
34
35
36 public void testPublicField() throws Exception {
37 String doc = "hello: 5";
38 TestBean bean = yaml.loadAs(doc, TestBean.class);
39 assertNotNull(bean);
40 assertEquals(5, bean.hello);
41 }
42
43
44
45
46 public void testUnknownField() throws Exception {
47 try {
48 String doc = "goodbye: 10";
49 yaml.loadAs(doc, TestBean.class);
50 } catch (YAMLException e) {
51 assertTrue(e.getMessage().contains("Cannot create property=goodbye"));
52 }
53 }
54
55
56
57
58
59
60 public void testSkipMissingProperties() throws Exception {
61 Representer representer = new Representer();
62 representer.getPropertyUtils().setSkipMissingProperties(true);
63 yaml = new Yaml(new Constructor(), representer);
64 String doc = "goodbye: 10\nhello: 5\nfizz: [1]";
65 TestBean bean = yaml.loadAs(doc, TestBean.class);
66 assertNotNull(bean);
67 assertEquals(5, bean.hello);
68 }
69
70
71
72
73
74 public void testNoSkipMissingProperties() throws Exception {
75 try {
76 Representer representer = new Representer();
77 representer.getPropertyUtils().setSkipMissingProperties(false);
78 yaml = new Yaml(new Constructor(), representer);
79 String doc = "goodbye: 10";
80 yaml.loadAs(doc, TestBean.class);
81 } catch (YAMLException e) {
82 assertTrue(e.getMessage().contains("Cannot create property=goodbye"));
83 }
84 }
85 }