1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.yaml.snakeyaml.issues.issue112;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import org.junit.Test;
26 import org.yaml.snakeyaml.Util;
27 import org.yaml.snakeyaml.Yaml;
28 import org.yaml.snakeyaml.constructor.Constructor;
29
30 public class ParameterisedTypeLoadingTestCase {
31
32 @Test
33 public void testParameterisedTypeLoading() throws IOException {
34 Yaml yamlParser = new Yaml(new Constructor(MyCompositeObject.class));
35 MyCompositeObject obj = (MyCompositeObject) yamlParser.load(getInput());
36 check(obj);
37
38
39 Yaml yaml = new Yaml();
40 String output = yaml.dumpAsMap(obj);
41 assertEquals(Util.getLocalResource("issues/issue112-2.yaml"), output);
42 }
43
44 @Test
45 public void testJavaBeanLoader() throws IOException {
46 Yaml yamlParser = new Yaml();
47 MyCompositeObject obj = yamlParser.loadAs(getInput(), MyCompositeObject.class);
48 check(obj);
49 }
50
51 private void check(MyCompositeObject obj) {
52 Object[] values = { 1, "two", 3, "four", "!!!" };
53 assertNotNull(obj);
54 assertEquals(5, obj.getThings().size());
55 int i = 0;
56 for (MyClass<? extends Object> thing : obj.getThings()) {
57 assertEquals(MyClass.class, thing.getClass());
58 assertNotNull("The 'name' property must be set.", thing.getName());
59 assertEquals(values[i++], thing.getName());
60 }
61 }
62
63 private InputStream getInput() throws IOException {
64 return this.getClass().getClassLoader().getResource("issues/issue112-1.yaml").openStream();
65 }
66 }