1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.yaml.snakeyaml;
17
18 import java.io.IOException;
19 import java.io.Writer;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import junit.framework.TestCase;
24
25 import org.yaml.snakeyaml.resolver.Resolver;
26
27 public class DumperTest extends TestCase {
28
29 public void testDump1() {
30 DumperOptions options = new DumperOptions();
31 options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
32 options.setExplicitStart(true);
33 options.setExplicitEnd(true);
34 List<Integer> list = new ArrayList<Integer>();
35 for (int i = 0; i < 3; i++) {
36 list.add(i);
37 }
38 Yaml yaml = new Yaml(options);
39 String output = yaml.dump(list);
40 assertEquals("---\n- !!int \"0\"\n- !!int \"1\"\n- !!int \"2\"\n...\n", output);
41 }
42
43 public void testDump2() {
44 DumperOptions options = new DumperOptions();
45 options.setExplicitStart(true);
46 List<Integer> list = new ArrayList<Integer>();
47 for (int i = 0; i < 3; i++) {
48 list.add(i);
49 }
50 Yaml yaml = new Yaml(options);
51 String output = yaml.dump(list);
52 assertEquals("--- [0, 1, 2]\n", output);
53 }
54
55 public void testDump3() {
56 DumperOptions options = new DumperOptions();
57 options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
58 List<Integer> list = new ArrayList<Integer>();
59 for (int i = 0; i < 3; i++) {
60 list.add(i);
61 }
62 Yaml yaml = new Yaml(options);
63 String output = yaml.dump(list);
64 assertEquals("- !!int '0'\n- !!int '1'\n- !!int '2'\n", output);
65 }
66
67 public void testDumpException() {
68 Yaml yaml = new Yaml();
69 Writer writer = new ExceptionWriter1();
70 try {
71 yaml.dump("aaa1234567890", writer);
72 fail("Exception must be thrown.");
73 } catch (Exception e) {
74 assertEquals("java.io.IOException: write test failure.", e.getMessage());
75 }
76 }
77
78 private class ExceptionWriter1 extends Writer {
79 @Override
80 public void write(String str) throws IOException {
81 throw new IOException("write test failure.");
82 }
83
84 @Override
85 public void close() throws IOException {
86 }
87
88 @Override
89 public void flush() throws IOException {
90 }
91
92 @Override
93 public void write(char[] cbuf, int off, int len) throws IOException {
94 throw new IOException("write test failure.");
95 }
96 }
97
98 @SuppressWarnings("deprecation")
99 public void testDeprecated1() {
100 Yaml yaml = new Yaml(new Dumper());
101 yaml.dump("aaa1234567890");
102 }
103
104 @SuppressWarnings("deprecation")
105 public void testDeprecated2() {
106 DumperOptions options = new DumperOptions();
107 options.setCanonical(true);
108 Yaml yaml = new Yaml(new Loader(), new Dumper(options), new Resolver());
109 String doc = yaml.dump("aaa1234567890");
110 assertEquals("---\n!!str \"aaa1234567890\"\n", doc);
111 }
112 }