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