1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.yaml.snakeyaml.constructor;
18
19 import java.io.IOException;
20 import java.math.BigInteger;
21
22 import junit.framework.TestCase;
23
24 import org.yaml.snakeyaml.DumperOptions;
25 import org.yaml.snakeyaml.Util;
26 import org.yaml.snakeyaml.Yaml;
27 import org.yaml.snakeyaml.error.YAMLException;
28
29 public class BeanConstructorTest extends TestCase {
30
31 public void testPrimitivesConstructor() throws IOException {
32 Yaml yaml = new Yaml(new Constructor(TestBean1.class));
33 String document = Util.getLocalResource("constructor/test-primitives1.yaml");
34 TestBean1 result = (TestBean1) yaml.load(document);
35 assertNotNull(result);
36 assertEquals(new Byte((byte) 1), result.getByteClass());
37 assertEquals((byte) -3, result.getBytePrimitive());
38 assertEquals(new Short((short) 0), result.getShortClass());
39 assertEquals((short) -13, result.getShortPrimitive());
40 assertEquals(new Integer(5), result.getInteger());
41 assertEquals(17, result.getIntPrimitive());
42 assertEquals("the text", result.getText());
43 assertEquals("13", result.getId());
44 assertEquals(new Long(11111111111L), result.getLongClass());
45 assertEquals(9999999999L, result.getLongPrimitive());
46 assertEquals(Boolean.TRUE, result.getBooleanClass());
47 assertTrue(result.isBooleanPrimitive());
48 assertEquals(new Character('2'), result.getCharClass());
49 assertEquals('#', result.getCharPrimitive());
50 assertEquals(new BigInteger("1234567890123456789012345678901234567890"),
51 result.getBigInteger());
52 assertEquals(new Float(2), result.getFloatClass());
53 assertEquals(new Float(3.1416), result.getFloatPrimitive());
54 assertEquals(new Double(4), result.getDoubleClass());
55 assertEquals(new Double(11200), result.getDoublePrimitive());
56 assertEquals(1199836800000L, result.getDate().getTime());
57 assertEquals("public", result.publicField);
58
59 DumperOptions options = new DumperOptions();
60 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
61 Yaml yamlToDump = new Yaml(options);
62 String output = yamlToDump.dump(result);
63 TestBean1 result2 = (TestBean1) yaml.load(output);
64 assertNotNull(result2);
65 TestBean1 result3 = (TestBean1) new Yaml().load(output);
66 assertNotNull(result3);
67 }
68
69 public void testNoClassConstructor() {
70 try {
71 new Yaml(new Constructor((Class<? extends Object>) null));
72 fail("Class must be provided.");
73 } catch (NullPointerException e) {
74 assertEquals("Root class must be provided.", e.getMessage());
75 }
76 }
77
78 public void testNoClassConstructorString() throws ClassNotFoundException {
79 try {
80 new Yaml(new Constructor((String) null));
81 fail("Class must be provided.");
82 } catch (NullPointerException e) {
83 assertEquals("Root type must be provided.", e.getMessage());
84 }
85 }
86
87 public void testNoClassConstructorEmptyString() throws ClassNotFoundException {
88 try {
89 new Yaml(new Constructor(" "));
90 fail("Class must be provided.");
91 } catch (YAMLException e) {
92 assertEquals("Root type must be provided.", e.getMessage());
93 }
94 }
95
96 public void testCharacter() throws IOException {
97 Yaml yaml = new Yaml(new Constructor(TestBean1.class));
98 String document = "charClass: id";
99 try {
100 yaml.load(document);
101 fail("Only one char must be allowed.");
102 } catch (Exception e) {
103 assertTrue(e.getMessage(),
104 e.getMessage().contains("Invalid node Character: 'id'; length: 2"));
105 }
106 document = "charClass: #";
107 TestBean1 bean = (TestBean1) yaml.load(document);
108 assertNull("Null must be accepted.", bean.getCharClass());
109 document = "charClass: ''";
110 bean = (TestBean1) yaml.load(document);
111 assertNull("Null must be accepted.", bean.getCharClass());
112 document = "charClass:\n";
113 bean = (TestBean1) yaml.load(document);
114 assertNull("Null must be accepted.", bean.getCharClass());
115 document = "charClass: 1\n";
116 bean = (TestBean1) yaml.load(document);
117 assertEquals(new Character('1'), bean.getCharClass());
118 }
119
120 public void testNoEmptyConstructor() throws IOException {
121 Yaml yaml = new Yaml(new Constructor(TestBean2.class));
122 String document = "text: qwerty";
123 try {
124 yaml.load(document);
125 fail("No empty constructor available");
126 } catch (Exception e) {
127 assertTrue(e.getMessage(), e.getMessage().contains("NoSuchMethodException"));
128 }
129 TestBean2 bean = new TestBean2();
130 assertEquals("", bean.getText());
131 }
132
133 private class TestBean2 {
134 private String text;
135
136 public TestBean2() {
137 setText("");
138 }
139
140 public String getText() {
141 return text;
142 }
143
144 public void setText(String text) {
145 this.text = text;
146 }
147 }
148
149 public void testPrivateMethod() throws IOException {
150
151 Yaml yaml = new Yaml(new Constructor(TestBean2.class));
152 String document = "text: qwerty";
153 try {
154 yaml.load(document);
155 fail("Private method cannot be called.");
156 } catch (Exception e) {
157 assertTrue(e.getMessage(), e.getMessage().contains("NoSuchMethodException"));
158 }
159 }
160
161 public void testKeyNotScalar() throws IOException {
162 Yaml yaml = new Yaml(new Constructor(TestBean1.class));
163 String document = "[1, 2]: qwerty";
164 try {
165 yaml.load(document);
166 fail("Keys must be scalars.");
167 } catch (Exception e) {
168 assertTrue(e.getMessage(), e.getMessage().contains("Keys must be scalars but found"));
169 }
170 }
171
172 public void testInvalidKey() throws IOException {
173 Yaml yaml = new Yaml(new Constructor(TestBean1.class));
174 String document = "something: qwerty";
175 try {
176 yaml.load(document);
177 fail("Non-existing property must fail.");
178 } catch (Exception e) {
179 assertTrue(e.getMessage(),
180 e.getMessage().contains("Unable to find property 'something'"));
181 }
182 }
183
184 public void testStaticField() throws IOException {
185 Yaml yaml = new Yaml(new Constructor(TestBean1.class));
186 String document = "staticInteger: 123";
187 try {
188 yaml.load(document);
189 fail("Staic variables must not be used.");
190 } catch (Exception e) {
191 assertTrue(e.getMessage(),
192 e.getMessage().contains("Unable to find property 'staticInteger'"));
193 }
194 }
195
196 public void testScalarContructor() throws IOException {
197 Yaml yaml = new Yaml(new Constructor(Parent1.class));
198 String document = "id: 123\nchild: 25";
199 Parent1 parent = (Parent1) yaml.load(document);
200 assertEquals("123", parent.getId());
201 Child1 child = parent.getChild();
202 assertEquals(new Integer(25), child.getCode());
203 }
204
205 public void testScalarContructorException() throws IOException {
206 Yaml yaml = new Yaml(new Constructor(ExceptionParent.class));
207 String document = "id: 123\nchild: 25";
208 try {
209 yaml.load(document);
210 fail("ExceptionParent should not be created.");
211 } catch (Exception e) {
212 assertTrue(
213 e.getMessage(),
214 e.getMessage().contains(
215 "Can't construct a java object for scalar tag:yaml.org,2002:int"));
216 }
217 }
218
219 static public class ExceptionParent {
220 private String id;
221 private ExceptionChild child;
222
223 public String getId() {
224 return id;
225 }
226
227 public void setId(String id) {
228 this.id = id;
229 }
230
231 public ExceptionChild getChild() {
232 return child;
233 }
234
235 public void setChild(ExceptionChild child) {
236 this.child = child;
237 }
238
239 }
240
241 public static class ExceptionChild {
242 private Integer code;
243
244 public ExceptionChild(Integer code) {
245 throw new RuntimeException("ExceptionChild cannot be created.");
246 }
247
248 public Integer getCode() {
249 return code;
250 }
251 }
252 }