1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.yaml.snakeyaml.constructor;
17
18 import junit.framework.TestCase;
19
20 import org.yaml.snakeyaml.DumperOptions;
21 import org.yaml.snakeyaml.Yaml;
22 import org.yaml.snakeyaml.error.YAMLException;
23
24 public class IncompleteBeanConstructorTest extends TestCase {
25
26 public void testRepresentor() {
27 IncompleteJavaBean bean = new IncompleteJavaBean();
28 DumperOptions options = new DumperOptions();
29 options.setAllowReadOnlyProperties(true);
30 Yaml yaml = new Yaml(options);
31 String output = yaml.dump(bean);
32 String className = this.getClass().getPackage().getName();
33 assertEquals("!!" + className + ".IncompleteJavaBean {name: No name}\n", output);
34 }
35
36 public void testConstructor() {
37 String className = "!!" + this.getClass().getPackage().getName()
38 + ".IncompleteJavaBean {number: 2}";
39 Yaml yaml = new Yaml();
40 IncompleteJavaBean bean = (IncompleteJavaBean) yaml.load(className);
41 assertNotNull(bean);
42 assertEquals("No name", bean.getName());
43 assertEquals(2, bean.obtainNumber());
44 }
45
46 public void testConstructor2() {
47 String className = "!!" + this.getClass().getPackage().getName()
48 + ".IncompleteJavaBean {number: 2, name: Bill}";
49 Yaml yaml = new Yaml();
50 try {
51 yaml.load(className);
52 fail("'name' property does not have setter.");
53 } catch (YAMLException e) {
54 assertEquals(
55 "Cannot create property=name for JavaBean=<IncompleteJavaBean name=No name>; Unable to find property 'name' on class: org.yaml.snakeyaml.constructor.IncompleteJavaBean",
56 e.getCause().getMessage());
57 }
58 }
59 }