• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2008, http://www.snakeyaml.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.yaml.snakeyaml;
17 
18 import java.util.ArrayList;
19 import java.util.LinkedHashMap;
20 import java.util.List;
21 import java.util.Map;
22 
23 import junit.framework.TestCase;
24 
25 import org.yaml.snakeyaml.emitter.Emitter;
26 import org.yaml.snakeyaml.error.YAMLException;
27 import org.yaml.snakeyaml.nodes.Tag;
28 import org.yaml.snakeyaml.representer.Representer;
29 
30 public class DumperOptionsTest extends TestCase {
31 
testDefaultStyle()32     public void testDefaultStyle() {
33         DumperOptions options = new DumperOptions();
34         Yaml yaml = new Yaml(options);
35         assertEquals("abc\n", yaml.dump("abc"));
36         // string which looks like integer
37         assertEquals("'123'\n", yaml.dump("123"));
38         //
39         options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
40         yaml = new Yaml(options);
41         assertEquals("\"123\"\n", yaml.dump("123"));
42         //
43         options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
44         yaml = new Yaml(options);
45         assertEquals("'123'\n", yaml.dump("123"));
46         //
47         options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
48         yaml = new Yaml(options);
49         assertEquals("'123'\n", yaml.dump("123"));
50         assertEquals("abc\n", yaml.dump("abc"));
51         // null check
52         try {
53             options.setDefaultScalarStyle(null);
54             fail("Null must not be accepted.");
55         } catch (NullPointerException e) {
56             assertEquals("Use ScalarStyle enum.", e.getMessage());
57         }
58     }
59 
testDefaultFlowStyle()60     public void testDefaultFlowStyle() {
61         Yaml yaml = new Yaml();
62         List<Integer> list = new ArrayList<Integer>();
63         list.add(1);
64         list.add(2);
65         list.add(3);
66         assertEquals("[1, 2, 3]\n", yaml.dump(list));
67         //
68         DumperOptions options = new DumperOptions();
69         options = new DumperOptions();
70         options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
71         yaml = new Yaml(options);
72         assertEquals("[1, 2, 3]\n", yaml.dump(list));
73         //
74         options = new DumperOptions();
75         options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
76         options.setPrettyFlow(true);
77         yaml = new Yaml(options);
78         assertEquals("[\n  1,\n  2,\n  3]\n", yaml.dump(list));
79         //
80         options = new DumperOptions();
81         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
82         yaml = new Yaml(options);
83         assertEquals("- 1\n- 2\n- 3\n", yaml.dump(list));
84         // null check
85         try {
86             options.setDefaultFlowStyle(null);
87             fail("Null must not be accepted.");
88         } catch (NullPointerException e) {
89             assertEquals("Use FlowStyle enum.", e.getMessage());
90         }
91     }
92 
testDefaultFlowStyleNested()93     public void testDefaultFlowStyleNested() {
94         Yaml yaml = new Yaml();
95         List<Integer> list = new ArrayList<Integer>();
96         list.add(1);
97         list.add(2);
98         list.add(3);
99         Map<String, Object> map = new LinkedHashMap<String, Object>();
100         map.put("a", "b");
101         map.put("c", list);
102         String result = yaml.dump(map);
103         assertEquals("a: b\nc: [1, 2, 3]\n", result);
104         //
105         DumperOptions options = new DumperOptions();
106         options = new DumperOptions();
107         options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
108         yaml = new Yaml(options);
109         assertEquals("{a: b, c: [1, 2, 3]}\n", yaml.dump(map));
110         //
111         options = new DumperOptions();
112         options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
113         options.setPrettyFlow(true);
114         yaml = new Yaml(options);
115         result = yaml.dump(map);
116         assertEquals("{\n  a: b,\n  c: [\n    1,\n    2,\n    3]\n  \n}\n", result);
117         //
118         options = new DumperOptions();
119         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
120         yaml = new Yaml(options);
121         assertEquals("a: b\nc:\n- 1\n- 2\n- 3\n", yaml.dump(map));
122     }
123 
testCanonical()124     public void testCanonical() {
125         Yaml yaml = new Yaml();
126         assertEquals("123\n", yaml.dump(123));
127         //
128         DumperOptions options = new DumperOptions();
129         options = new DumperOptions();
130         options.setCanonical(true);
131         yaml = new Yaml(options);
132         assertEquals("---\n!!int \"123\"\n", yaml.dump(123));
133         //
134         options = new DumperOptions();
135         options.setCanonical(false);
136         yaml = new Yaml(options);
137         assertEquals("123\n", yaml.dump(123));
138     }
139 
testIndent()140     public void testIndent() {
141         Yaml yaml = new Yaml();
142         List<Integer> list = new ArrayList<Integer>();
143         list.add(1);
144         list.add(2);
145         DumperOptions options = new DumperOptions();
146         options.setCanonical(true);
147         yaml = new Yaml(options);
148         assertEquals("---\n!!seq [\n  !!int \"1\",\n  !!int \"2\",\n]\n", yaml.dump(list));
149         //
150         options.setIndent(4);
151         yaml = new Yaml(options);
152         assertEquals("---\n!!seq [\n    !!int \"1\",\n    !!int \"2\",\n]\n", yaml.dump(list));
153         //
154         try {
155             options.setIndent(0);
156             fail();
157         } catch (YAMLException e) {
158             assertTrue(true);
159         }
160         try {
161             options.setIndent(-2);
162             fail();
163         } catch (YAMLException e) {
164             assertTrue(true);
165         }
166         try {
167             options.setIndent(11);
168             fail();
169         } catch (YAMLException e) {
170             assertTrue(true);
171         }
172         //
173         assertTrue(Emitter.MIN_INDENT > 0);
174         assertTrue(Emitter.MIN_INDENT < Emitter.MAX_INDENT);
175         assertTrue(Emitter.MAX_INDENT < 20);
176     }
177 
178     public void testLineBreak() {
179         Yaml yaml = new Yaml();
180         List<Integer> list = new ArrayList<Integer>();
181         list.add(1);
182         list.add(2);
183         DumperOptions options = new DumperOptions();
184         options.setCanonical(true);
185         yaml = new Yaml(options);
186         assertEquals("---\n!!seq [\n  !!int \"1\",\n  !!int \"2\",\n]\n", yaml.dump(list));
187         //
188         options.setLineBreak(DumperOptions.LineBreak.WIN);
189         yaml = new Yaml(options);
190         String output = yaml.dump(list);
191         assertEquals("---\r\n!!seq [\r\n  !!int \"1\",\r\n  !!int \"2\",\r\n]\r\n", output);
192         // null check
193         try {
194             options.setLineBreak(null);
195             fail("Null must not be accepted.");
196         } catch (NullPointerException e) {
197             assertEquals("Specify line break.", e.getMessage());
198         }
199     }
200 
201     public void testLineBreakForPlatform() {
202         DumperOptions.LineBreak lineBreak = DumperOptions.LineBreak.getPlatformLineBreak();
203         assertEquals("Line break must match platform's default.",
204                 System.getProperty("line.separator"), lineBreak.getString());
205         //
206         Yaml yaml = new Yaml();
207         List<Integer> list = new ArrayList<Integer>();
208         list.add(1);
209         list.add(2);
210         DumperOptions options = new DumperOptions();
211         options.setLineBreak(DumperOptions.LineBreak.getPlatformLineBreak());
212         yaml = new Yaml(options);
213         assertEquals("[1, 2]", yaml.dump(list).trim());
214     }
215 
216     public void testLineBreakForPlatformUnix() {
217         System.setProperty("line.separator", "\n");
218         assertEquals("\n", System.getProperty("line.separator"));
219         DumperOptions.LineBreak lineBreak = DumperOptions.LineBreak.getPlatformLineBreak();
220         assertEquals("Line break must match platform's default.",
221                 System.getProperty("line.separator"), lineBreak.getString());
222         assertEquals("Unknown Line break must match UNIX line break.", "\n", lineBreak.getString());
223     }
224 
225     public void testLineBreakForPlatformMac() {
226         System.setProperty("line.separator", "\r");
227         assertEquals("\r", System.getProperty("line.separator"));
228         DumperOptions.LineBreak lineBreak = DumperOptions.LineBreak.getPlatformLineBreak();
229         assertEquals("Line break must match platform's default.",
230                 System.getProperty("line.separator"), lineBreak.getString());
231         assertEquals("Unknown Line break must match UNIX line break.", "\r", lineBreak.getString());
232     }
233 
234     public void testLineBreakForPlatformWin() {
235         System.setProperty("line.separator", "\r\n");
236         assertEquals("\r\n", System.getProperty("line.separator"));
237         DumperOptions.LineBreak lineBreak = DumperOptions.LineBreak.getPlatformLineBreak();
238         assertEquals("Line break must match platform's default.",
239                 System.getProperty("line.separator"), lineBreak.getString());
240         assertEquals("Unknown Line break must match UNIX line break.", "\r\n",
241                 lineBreak.getString());
242     }
243 
244     public void testLineBreakForPlatformUnknown() {
245         System.setProperty("line.separator", "\n\r");
246         assertEquals("\n\r", System.getProperty("line.separator"));
247         DumperOptions.LineBreak lineBreak = DumperOptions.LineBreak.getPlatformLineBreak();
248         assertEquals("Unknown Line break must match UNIX line break.", "\n", lineBreak.getString());
249     }
250 
251     public void testExplicitStart() {
252         Yaml yaml = new Yaml();
253         List<Integer> list = new ArrayList<Integer>();
254         list.add(1);
255         list.add(2);
256         list.add(3);
257         assertEquals("[1, 2, 3]\n", yaml.dump(list));
258         //
259         DumperOptions options = new DumperOptions();
260         options = new DumperOptions();
261         options.setExplicitStart(true);
262         yaml = new Yaml(options);
263         assertEquals("--- [1, 2, 3]\n", yaml.dump(list));
264         //
265         options.setExplicitEnd(true);
266         yaml = new Yaml(options);
267         assertEquals("--- [1, 2, 3]\n...\n", yaml.dump(list));
268     }
269 
270     public void testVersion() {
271         Yaml yaml = new Yaml();
272         List<Integer> list = new ArrayList<Integer>();
273         list.add(1);
274         list.add(2);
275         list.add(3);
276         assertEquals("[1, 2, 3]\n", yaml.dump(list));
277         //
278         DumperOptions options = new DumperOptions();
279         options = new DumperOptions();
280         options.setVersion(DumperOptions.Version.V1_1);
281         yaml = new Yaml(options);
282         assertEquals("%YAML 1.1\n--- [1, 2, 3]\n", yaml.dump(list));
283         //
284         options.setVersion(DumperOptions.Version.V1_0);
285         yaml = new Yaml(options);
286         assertEquals("%YAML 1.0\n--- [1, 2, 3]\n", yaml.dump(list));
287         //
288         assertEquals("Version: 1.1", DumperOptions.Version.V1_1.toString());
289     }
290 
291     public void testTags() {
292         Yaml yaml = new Yaml();
293         List<Integer> list = new ArrayList<Integer>();
294         list.add(1);
295         list.add(2);
296         list.add(3);
297         assertEquals("[1, 2, 3]\n", yaml.dump(list));
298         //
299         DumperOptions options = new DumperOptions();
300         options = new DumperOptions();
301         Map<String, String> tags = new LinkedHashMap<String, String>();
302         tags.put("!foo!", "bar");
303         options.setTags(tags);
304         yaml = new Yaml(options);
305         assertEquals("%TAG !foo! bar\n--- [1, 2, 3]\n", yaml.dump(list));
306         //
307         options = new DumperOptions();
308         tags.put("!yaml!", Tag.PREFIX);
309         yaml = new Yaml(options);
310         assertEquals("foo\n", yaml.dump("foo"));
311     }
312 
313     public void testAllowUnicode() {
314         Yaml yaml = new Yaml();
315         assertEquals("out: " + yaml.dump("\u00DCber"), "\u00DCber\n", yaml.dump("\u00DCber"));
316         //
317         DumperOptions options = new DumperOptions();
318         options = new DumperOptions();
319         options.setAllowUnicode(false);
320         yaml = new Yaml(options);
321         assertEquals("\"\\xdcber\"\n", yaml.dump("\u00DCber"));
322     }
323 
324     public void testToString() {
325         DumperOptions.ScalarStyle scalarStyle = DumperOptions.ScalarStyle.LITERAL;
326         assertEquals("Scalar style: '|'", scalarStyle.toString());
327         //
328         DumperOptions.FlowStyle flowStyle = DumperOptions.FlowStyle.BLOCK;
329         assertEquals("Flow style: 'false'", flowStyle.toString());
330         //
331         DumperOptions.LineBreak lb = DumperOptions.LineBreak.UNIX;
332         assertEquals("Line break: UNIX", lb.toString());
333     }
334 
335     public void testWithRepresenter() {
336         Representer representer = new Representer();
337         DumperOptions options = new DumperOptions();
338         options.setIndent(4);
339         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
340         Yaml yaml = new Yaml(representer, options);
341         List<Integer> list = new ArrayList<Integer>();
342         list.add(1);
343         list.add(2);
344         list.add(3);
345         Map<String, Object> map = new LinkedHashMap<String, Object>();
346         map.put("a", "b");
347         map.put("c", list);
348         assertEquals("a: b\nc:\n- 1\n- 2\n- 3\n", yaml.dump(map));
349     }
350 
351     public void testSplitLinesDoubleQuoted() {
352         DumperOptions options = new DumperOptions();
353         options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
354         Yaml yaml;
355         String output;
356 
357         // Split lines enabled (default)
358         assertTrue(options.getSplitLines());
359         yaml = new Yaml(options);
360         output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
361         assertEquals("\"1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888\\\n  \\ 9999999999 0000000000\"\n", output);
362 
363         // Split lines disabled
364         options.setSplitLines(false);
365         assertFalse(options.getSplitLines());
366         yaml = new Yaml(options);
367         output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
368         assertEquals("\"1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000\"\n", output);
369     }
370 
371     public void testSplitLinesSingleQuoted() {
372         DumperOptions options = new DumperOptions();
373         options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
374         Yaml yaml;
375         String output;
376 
377         // Split lines enabled (default)
378         assertTrue(options.getSplitLines());
379         yaml = new Yaml(options);
380         output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
381         assertEquals("'1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888\n  9999999999 0000000000'\n", output);
382 
383         // Split lines disabled
384         options.setSplitLines(false);
385         assertFalse(options.getSplitLines());
386         yaml = new Yaml(options);
387         output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
388         assertEquals("'1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000'\n", output);
389     }
390 
391     public void testSplitLinesFolded() {
392         DumperOptions options = new DumperOptions();
393         options.setDefaultScalarStyle(DumperOptions.ScalarStyle.FOLDED);
394         Yaml yaml;
395         String output;
396 
397         // Split lines enabled (default)
398         assertTrue(options.getSplitLines());
399         yaml = new Yaml(options);
400         output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
401         assertEquals(">-\n  1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888\n  9999999999 0000000000\n", output);
402 
403         // Split lines disabled
404         options.setSplitLines(false);
405         assertFalse(options.getSplitLines());
406         yaml = new Yaml(options);
407         output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
408         assertEquals(">-\n  1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000\n", output);
409     }
410 
411     public void testSplitLinesLiteral() {
412         DumperOptions options = new DumperOptions();
413         options.setDefaultScalarStyle(DumperOptions.ScalarStyle.LITERAL);
414         Yaml yaml;
415         String output;
416 
417         // Split lines enabled (default) -- split lines does not apply to literal style
418         assertTrue(options.getSplitLines());
419         yaml = new Yaml(options);
420         output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
421         assertEquals("|-\n  1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000\n", output);
422     }
423 
424     public void testSplitLinesPlain() {
425         DumperOptions options = new DumperOptions();
426         options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
427         Yaml yaml;
428         String output;
429 
430         // Split lines enabled (default) -- split lines does not apply to plain style
431         assertTrue(options.getSplitLines());
432         yaml = new Yaml(options);
433         output = yaml.dump("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000");
434         assertEquals("1111111111 2222222222 3333333333 4444444444 5555555555 6666666666 7777777777 8888888888 9999999999 0000000000\n", output);
435     }
436 
437     public void testSetIndicatorIndentNegative() {
438         DumperOptions options = new DumperOptions();
439         try {
440         options.setIndicatorIndent(-1);
441             fail("Negative indent must not be accepted.");
442         } catch (YAMLException e) {
443             assertEquals("Indicator indent must be non-negative.", e.getMessage());
444         }
445     }
446 
447     public void testSetIndicatorIndentTooBig() {
448         DumperOptions options = new DumperOptions();
449         try {
450             options.setIndicatorIndent(100);
451             fail("Negative indent must not be accepted.");
452         } catch (YAMLException e) {
453             assertEquals("Indicator indent must be at most Emitter.MAX_INDENT-1: 9", e.getMessage());
454         }
455     }
456 
457     public void testCreateUnknownStyle() {
458         try {
459             DumperOptions.ScalarStyle.createStyle(' ');
460             fail("Negative indent must not be accepted.");
461         } catch (YAMLException e) {
462             assertEquals("Unknown scalar style character:  ", e.getMessage());
463         }
464     }
465 
466     public void testCreateStyle() {
467         assertEquals(DumperOptions.ScalarStyle.DOUBLE_QUOTED, DumperOptions.ScalarStyle.createStyle('"'));
468         assertEquals(DumperOptions.ScalarStyle.SINGLE_QUOTED, DumperOptions.ScalarStyle.createStyle('\''));
469         assertEquals(DumperOptions.ScalarStyle.LITERAL, DumperOptions.ScalarStyle.createStyle('|'));
470         assertEquals(DumperOptions.ScalarStyle.FOLDED, DumperOptions.ScalarStyle.createStyle('>'));
471         assertEquals(DumperOptions.ScalarStyle.PLAIN, DumperOptions.ScalarStyle.createStyle(null));
472     }
473 }
474