1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.yaml.snakeyaml;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.TreeMap;
24
25 import junit.framework.TestCase;
26
27 public class CollectionWithBeanYamlTest extends TestCase {
28
29 @SuppressWarnings("unchecked")
30 public void testYamlMap() throws IOException {
31 Map<String, Bean> data = new TreeMap<String, Bean>();
32 data.put("gold1", new Bean());
33 data.put("gold2", new Bean());
34
35 Yaml yaml = new Yaml();
36 String output = yaml.dump(data);
37 assertEquals(
38 "gold1: !!org.yaml.snakeyaml.CollectionWithBeanYamlTest$Bean {a: ''}\ngold2: !!org.yaml.snakeyaml.CollectionWithBeanYamlTest$Bean {a: ''}\n",
39 output);
40 Object o = yaml.load(output);
41
42 assertTrue(o instanceof Map);
43 Map<String, Bean> m = (Map<String, Bean>) o;
44 assertTrue(m.get("gold1") instanceof Bean);
45 assertTrue("" + m.get("gold2").getClass(), m.get("gold2") instanceof Bean);
46 }
47
48 @SuppressWarnings("unchecked")
49 public void testYamlList() throws IOException {
50 List<Bean> data = new ArrayList<Bean>();
51 data.add(new Bean("1"));
52 data.add(new Bean("2"));
53
54 Yaml yaml = new Yaml();
55 String output = yaml.dump(data);
56 assertEquals(
57 "- !!org.yaml.snakeyaml.CollectionWithBeanYamlTest$Bean {a: '1'}\n- !!org.yaml.snakeyaml.CollectionWithBeanYamlTest$Bean {a: '2'}\n",
58 output);
59 Object o = yaml.load(output);
60
61 assertTrue(o instanceof List);
62 List<Bean> m = (List<Bean>) o;
63 assertEquals(2, m.size());
64 assertTrue(m.get(0) instanceof Bean);
65 assertTrue(m.get(1) instanceof Bean);
66 }
67
68 public static class Bean {
69 private String a;
70
71 public Bean() {
72 a = "";
73 }
74
75 public Bean(String value) {
76 a = value;
77 }
78
79 public String getA() {
80 return a;
81 }
82
83 public void setA(String s) {
84 a = s;
85 }
86 }
87 }