1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.yaml.snakeyaml.representer;
18
19 import junit.framework.TestCase;
20
21 import org.yaml.snakeyaml.DumperOptions;
22 import org.yaml.snakeyaml.Yaml;
23 import org.yaml.snakeyaml.nodes.Tag;
24
25 public class RepresenterTest extends TestCase {
26
27 public void testRepresenter() {
28 MyBean bean = new MyBean();
29 bean.setName("Gnome");
30 bean.setValid(true);
31 bean.setPrimitive(true);
32 Yaml yaml = new Yaml();
33 assertEquals(
34 "!!org.yaml.snakeyaml.representer.RepresenterTest$MyBean {name: Gnome, primitive: true}\n",
35 yaml.dump(bean));
36 }
37
38 public static class MyBean {
39 private String name;
40 private Boolean valid;
41 private boolean primitive;
42
43 public String getName() {
44 return name;
45 }
46
47 public void setName(String name) {
48 this.name = name;
49 }
50
51 public Boolean isValid() {
52 return valid;
53 }
54
55 public void setValid(Boolean valid) {
56 this.valid = valid;
57 }
58
59 public boolean isPrimitive() {
60 return primitive;
61 }
62
63 public void setPrimitive(boolean primitive) {
64 this.primitive = primitive;
65 }
66 }
67
68 public void testRepresenterNoConstructorAvailable() {
69 MyBean2 bean = new MyBean2("Gnome", true);
70 DumperOptions options = new DumperOptions();
71 options.setAllowReadOnlyProperties(true);
72 Yaml yaml = new Yaml(options);
73 assertEquals("!!org.yaml.snakeyaml.representer.RepresenterTest$MyBean2 {valid: true}\n",
74 yaml.dump(bean));
75 }
76
77 public static class MyBean2 {
78 private String name;
79 private Boolean valid;
80
81 public MyBean2(String name, Boolean valid) {
82 this();
83 this.name = name;
84 this.valid = valid;
85 }
86
87 private MyBean2() {
88 super();
89 }
90
91 private String getName() {
92 return name;
93 }
94
95 public Boolean getValid() {
96 return valid;
97 }
98
99 @Override
100 public String toString() {
101 return getName() + " " + getValid();
102 }
103 }
104
105 public void testRepresenterGetterWithException() {
106 MyBean3 bean = new MyBean3("Gnome", false);
107 DumperOptions options = new DumperOptions();
108 options.setAllowReadOnlyProperties(true);
109 Yaml yaml = new Yaml(options);
110 try {
111 String str = yaml.dump(bean);
112 fail("Exception must be reported: " + str);
113 } catch (Exception e) {
114 assertTrue(true);
115 }
116
117 MyBean3 bean2 = new MyBean3("Gnome", true);
118 String str = yaml.dump(bean2);
119
120 assertEquals(
121 "isValid property must not be dumped.",
122 "!!org.yaml.snakeyaml.representer.RepresenterTest$MyBean3 {boolProperty: true, name: Gnome}\n",
123 str);
124 }
125
126 public static class MyBean3 {
127 private String name;
128 private Boolean valid;
129 private boolean boolProperty;
130
131 public MyBean3(String name, Boolean valid) {
132 this.name = name;
133 this.valid = valid;
134 boolProperty = true;
135 }
136
137 public String getName() {
138 if (valid) {
139 return name;
140 } else {
141 throw new UnsupportedOperationException("Test.");
142 }
143 }
144
145 public Boolean isValid() {
146 return valid;
147 }
148
149 public boolean isBoolProperty() {
150 return boolProperty;
151 }
152
153 @Override
154 public String toString() {
155 return "MyBean3<" + name + ", " + isValid() + ">";
156 }
157 }
158
159 public void testRepresenterAddNull() {
160 Representer representer = new Representer();
161 try {
162 representer.addClassTag(EmptyBean.class, (Tag) null);
163 fail("Tag must be provided.");
164 } catch (Exception e) {
165 assertEquals("Tag must be provided.", e.getMessage());
166 }
167 }
168
169 public void testRepresenterEmptyBean() {
170 EmptyBean bean = new EmptyBean();
171 Yaml yaml = new Yaml();
172 try {
173 yaml.dump(bean);
174 fail("EmptyBean has empty representation.");
175 } catch (Exception e) {
176 assertEquals(
177 "No JavaBean properties found in org.yaml.snakeyaml.representer.RepresenterTest$EmptyBean",
178 e.getMessage());
179 }
180 }
181
182 public static class EmptyBean {
183 private int number;
184
185 public void process() {
186 number += 1;
187 }
188
189 public int obtain() {
190 return number;
191 }
192 }
193 }