1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package examples;
18
19 import java.io.StringWriter;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import junit.framework.TestCase;
26
27 import org.yaml.snakeyaml.DumperOptions;
28 import org.yaml.snakeyaml.Yaml;
29
30 public class DumpExampleTest extends TestCase {
31 public void testDump() {
32 Map<String, Object> data = new HashMap<String, Object>();
33 data.put("name", "Silenthand Olleander");
34 data.put("race", "Human");
35 data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
36 Yaml yaml = new Yaml();
37 String output = yaml.dump(data);
38 assertTrue(output.contains("name: Silenthand Olleander"));
39 assertTrue(output.contains("race: Human"));
40 assertTrue(output.contains("traits: [ONE_HAND, ONE_EYE]"));
41 }
42
43 public void testDumpWriter() {
44 Map<String, Object> data = new HashMap<String, Object>();
45 data.put("name", "Silenthand Olleander");
46 data.put("race", "Human");
47 data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
48 Yaml yaml = new Yaml();
49 StringWriter writer = new StringWriter();
50 yaml.dump(data, writer);
51 assertTrue(writer.toString().contains("name: Silenthand Olleander"));
52 assertTrue(writer.toString().contains("race: Human"));
53 assertTrue(writer.toString().contains("traits: [ONE_HAND, ONE_EYE]"));
54 }
55
56 public void testDumpMany() {
57 List<Integer> docs = new ArrayList<Integer>();
58 for (int i = 1; i < 4; i++) {
59 docs.add(i);
60 }
61 DumperOptions options = new DumperOptions();
62 options.setExplicitStart(true);
63 Yaml yaml = new Yaml(options);
64 String result = yaml.dumpAll(docs.iterator());
65 assertNotNull(result);
66 assertTrue(result.contains("--- 2"));
67 }
68
69 public void testDumpCustomJavaClass() {
70 Hero hero = new Hero("Galain Ysseleg", -3, 2);
71 DumperOptions options = new DumperOptions();
72 options.setAllowReadOnlyProperties(true);
73 Yaml yaml = new Yaml(options);
74 String output = yaml.dump(hero);
75 assertEquals("!!examples.Hero {hp: -3, name: Galain Ysseleg, sp: 2}\n", output);
76 }
77
78 public void testDumperOptions() {
79 List<Integer> data = new ArrayList<Integer>();
80 for (int i = 0; i < 50; i++) {
81 data.add(i);
82 }
83 Yaml yaml = new Yaml();
84 String output = yaml.dump(data);
85 assertTrue(output.contains("[0, 1, 2, 3, 4, 5, 6, 7, 8"));
86
87 DumperOptions options = new DumperOptions();
88 options.setWidth(50);
89 options.setIndent(4);
90 yaml = new Yaml(options);
91 output = yaml.dump(data);
92 assertTrue(output.contains("1, 2"));
93 }
94
95 public void testDumperOptionsCanonical() {
96 List<Integer> data = new ArrayList<Integer>();
97 for (int i = 0; i < 5; i++) {
98 data.add(i);
99 }
100 DumperOptions options = new DumperOptions();
101 options.setCanonical(true);
102 Yaml yaml = new Yaml(options);
103 String output = yaml.dump(data);
104 assertTrue(output.contains("---"));
105 assertTrue(output.contains("!!seq ["));
106 assertTrue(output.contains("!!int \"3\","));
107 }
108
109 public void testDumperOptionsFlowStyle() {
110 List<Integer> data = new ArrayList<Integer>();
111 for (int i = 0; i < 5; i++) {
112 data.add(i);
113 }
114 DumperOptions options = new DumperOptions();
115 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
116 Yaml yaml = new Yaml(options);
117 String output = yaml.dump(data);
118 assertTrue(output.contains("- 0\n"));
119 assertTrue(output.contains("- 1\n"));
120 assertTrue(output.contains("- 4\n"));
121 }
122
123 public void testDumperOptionsStyle() {
124 List<Integer> data = new ArrayList<Integer>();
125 for (int i = 0; i < 5; i++) {
126 data.add(i);
127 }
128 DumperOptions options = new DumperOptions();
129 options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
130 Yaml yaml = new Yaml(options);
131 String output = yaml.dump(data);
132 assertTrue(output.contains("- !!int \"0\""));
133 assertTrue(output.contains("- !!int \"1\""));
134 assertTrue(output.contains("- !!int \"4\""));
135 }
136 }