View Javadoc

1   /**
2    * Copyright (c) 2008-2011, 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  
17  package org.yaml.snakeyaml;
18  
19  import java.util.ArrayList;
20  import java.util.LinkedHashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import junit.framework.TestCase;
25  
26  import org.yaml.snakeyaml.emitter.Emitter;
27  import org.yaml.snakeyaml.error.YAMLException;
28  import org.yaml.snakeyaml.nodes.Tag;
29  import org.yaml.snakeyaml.representer.Representer;
30  
31  public class DumperOptionsTest extends TestCase {
32  
33      public void testDefaultStyle() {
34          DumperOptions options = new DumperOptions();
35          Yaml yaml = new Yaml(options);
36          assertEquals("abc\n", yaml.dump("abc"));
37          // string which looks like integer
38          assertEquals("'123'\n", yaml.dump("123"));
39          //
40          options.setDefaultScalarStyle(DumperOptions.ScalarStyle.DOUBLE_QUOTED);
41          yaml = new Yaml(options);
42          assertEquals("\"123\"\n", yaml.dump("123"));
43          //
44          options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
45          yaml = new Yaml(options);
46          assertEquals("'123'\n", yaml.dump("123"));
47          //
48          options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
49          yaml = new Yaml(options);
50          assertEquals("'123'\n", yaml.dump("123"));
51          assertEquals("abc\n", yaml.dump("abc"));
52          // null check
53          try {
54              options.setDefaultScalarStyle(null);
55              fail("Null must not be accepted.");
56          } catch (NullPointerException e) {
57              assertEquals("Use ScalarStyle enum.", e.getMessage());
58          }
59      }
60  
61      public void testDefaultFlowStyle() {
62          Yaml yaml = new Yaml();
63          List<Integer> list = new ArrayList<Integer>();
64          list.add(1);
65          list.add(2);
66          list.add(3);
67          assertEquals("[1, 2, 3]\n", yaml.dump(list));
68          //
69          DumperOptions options = new DumperOptions();
70          options = new DumperOptions();
71          options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
72          yaml = new Yaml(options);
73          assertEquals("[1, 2, 3]\n", yaml.dump(list));
74          //
75          options = new DumperOptions();
76          options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
77          options.setPrettyFlow(true);
78          yaml = new Yaml(options);
79          assertEquals("[\n  1,\n  2,\n  3]\n", yaml.dump(list));
80          //
81          options = new DumperOptions();
82          options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
83          yaml = new Yaml(options);
84          assertEquals("- 1\n- 2\n- 3\n", yaml.dump(list));
85          // null check
86          try {
87              options.setDefaultFlowStyle(null);
88              fail("Null must not be accepted.");
89          } catch (NullPointerException e) {
90              assertEquals("Use FlowStyle enum.", e.getMessage());
91          }
92      }
93  
94      public void testDefaultFlowStyleNested() {
95          Yaml yaml = new Yaml();
96          List<Integer> list = new ArrayList<Integer>();
97          list.add(1);
98          list.add(2);
99          list.add(3);
100         Map<String, Object> map = new LinkedHashMap<String, Object>();
101         map.put("a", "b");
102         map.put("c", list);
103         String result = yaml.dump(map);
104         assertEquals("a: b\nc: [1, 2, 3]\n", result);
105         //
106         DumperOptions options = new DumperOptions();
107         options = new DumperOptions();
108         options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
109         yaml = new Yaml(options);
110         assertEquals("{a: b, c: [1, 2, 3]}\n", yaml.dump(map));
111         //
112         options = new DumperOptions();
113         options.setDefaultFlowStyle(DumperOptions.FlowStyle.FLOW);
114         options.setPrettyFlow(true);
115         yaml = new Yaml(options);
116         result = yaml.dump(map);
117         assertEquals("{\n  a: b,\n  c: [\n    1,\n    2,\n    3]\n  \n}\n", result);
118         //
119         options = new DumperOptions();
120         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
121         yaml = new Yaml(options);
122         assertEquals("a: b\nc:\n- 1\n- 2\n- 3\n", yaml.dump(map));
123     }
124 
125     public void testCanonical() {
126         Yaml yaml = new Yaml();
127         assertEquals("123\n", yaml.dump(123));
128         //
129         DumperOptions options = new DumperOptions();
130         options = new DumperOptions();
131         options.setCanonical(true);
132         yaml = new Yaml(options);
133         assertEquals("---\n!!int \"123\"\n", yaml.dump(123));
134         //
135         options = new DumperOptions();
136         options.setCanonical(false);
137         yaml = new Yaml(options);
138         assertEquals("123\n", yaml.dump(123));
139     }
140 
141     public void testIndent() {
142         Yaml yaml = new Yaml();
143         List<Integer> list = new ArrayList<Integer>();
144         list.add(1);
145         list.add(2);
146         DumperOptions options = new DumperOptions();
147         options.setCanonical(true);
148         yaml = new Yaml(options);
149         assertEquals("---\n!!seq [\n  !!int \"1\",\n  !!int \"2\",\n]\n", yaml.dump(list));
150         //
151         options.setIndent(4);
152         yaml = new Yaml(options);
153         assertEquals("---\n!!seq [\n    !!int \"1\",\n    !!int \"2\",\n]\n", yaml.dump(list));
154         //
155         try {
156             options.setIndent(0);
157             fail();
158         } catch (YAMLException e) {
159             assertTrue(true);
160         }
161         try {
162             options.setIndent(-2);
163             fail();
164         } catch (YAMLException e) {
165             assertTrue(true);
166         }
167         try {
168             options.setIndent(11);
169             fail();
170         } catch (YAMLException e) {
171             assertTrue(true);
172         }
173         //
174         assertTrue(Emitter.MIN_INDENT > 0);
175         assertTrue(Emitter.MIN_INDENT < Emitter.MAX_INDENT);
176         assertTrue(Emitter.MAX_INDENT < 20);
177     }
178 
179     public void testLineBreak() {
180         Yaml yaml = new Yaml();
181         List<Integer> list = new ArrayList<Integer>();
182         list.add(1);
183         list.add(2);
184         DumperOptions options = new DumperOptions();
185         options.setCanonical(true);
186         yaml = new Yaml(options);
187         assertEquals("---\n!!seq [\n  !!int \"1\",\n  !!int \"2\",\n]\n", yaml.dump(list));
188         //
189         options.setLineBreak(DumperOptions.LineBreak.WIN);
190         yaml = new Yaml(options);
191         String output = yaml.dump(list);
192         assertEquals("---\r\n!!seq [\r\n  !!int \"1\",\r\n  !!int \"2\",\r\n]\r\n", output);
193         // null check
194         try {
195             options.setLineBreak(null);
196             fail("Null must not be accepted.");
197         } catch (NullPointerException e) {
198             assertEquals("Specify line break.", e.getMessage());
199         }
200     }
201 
202     public void testLineBreakForPlatform() {
203         DumperOptions.LineBreak lineBreak = DumperOptions.LineBreak.getPlatformLineBreak();
204         assertEquals("Line break must match platform's default.",
205                 System.getProperty("line.separator"), lineBreak.getString());
206         //
207         Yaml yaml = new Yaml();
208         List<Integer> list = new ArrayList<Integer>();
209         list.add(1);
210         list.add(2);
211         DumperOptions options = new DumperOptions();
212         options.setLineBreak(DumperOptions.LineBreak.getPlatformLineBreak());
213         yaml = new Yaml(options);
214         assertEquals("[1, 2]", yaml.dump(list).trim());
215     }
216 
217     public void testLineBreakForPlatformUnix() {
218         System.setProperty("line.separator", "\n");
219         assertEquals("\n", System.getProperty("line.separator"));
220         DumperOptions.LineBreak lineBreak = DumperOptions.LineBreak.getPlatformLineBreak();
221         assertEquals("Line break must match platform's default.",
222                 System.getProperty("line.separator"), lineBreak.getString());
223         assertEquals("Unknown Line break must match UNIX line break.", "\n", lineBreak.getString());
224     }
225 
226     public void testLineBreakForPlatformMac() {
227         System.setProperty("line.separator", "\r");
228         assertEquals("\r", System.getProperty("line.separator"));
229         DumperOptions.LineBreak lineBreak = DumperOptions.LineBreak.getPlatformLineBreak();
230         assertEquals("Line break must match platform's default.",
231                 System.getProperty("line.separator"), lineBreak.getString());
232         assertEquals("Unknown Line break must match UNIX line break.", "\r", lineBreak.getString());
233     }
234 
235     public void testLineBreakForPlatformWin() {
236         System.setProperty("line.separator", "\r\n");
237         assertEquals("\r\n", System.getProperty("line.separator"));
238         DumperOptions.LineBreak lineBreak = DumperOptions.LineBreak.getPlatformLineBreak();
239         assertEquals("Line break must match platform's default.",
240                 System.getProperty("line.separator"), lineBreak.getString());
241         assertEquals("Unknown Line break must match UNIX line break.", "\r\n",
242                 lineBreak.getString());
243     }
244 
245     public void testLineBreakForPlatformUnknown() {
246         System.setProperty("line.separator", "\n\r");
247         assertEquals("\n\r", System.getProperty("line.separator"));
248         DumperOptions.LineBreak lineBreak = DumperOptions.LineBreak.getPlatformLineBreak();
249         assertEquals("Unknown Line break must match UNIX line break.", "\n", lineBreak.getString());
250     }
251 
252     public void testExplicitStart() {
253         Yaml yaml = new Yaml();
254         List<Integer> list = new ArrayList<Integer>();
255         list.add(1);
256         list.add(2);
257         list.add(3);
258         assertEquals("[1, 2, 3]\n", yaml.dump(list));
259         //
260         DumperOptions options = new DumperOptions();
261         options = new DumperOptions();
262         options.setExplicitStart(true);
263         yaml = new Yaml(options);
264         assertEquals("--- [1, 2, 3]\n", yaml.dump(list));
265         //
266         options.setExplicitEnd(true);
267         yaml = new Yaml(options);
268         assertEquals("--- [1, 2, 3]\n...\n", yaml.dump(list));
269     }
270 
271     public void testVersion() {
272         Yaml yaml = new Yaml();
273         List<Integer> list = new ArrayList<Integer>();
274         list.add(1);
275         list.add(2);
276         list.add(3);
277         assertEquals("[1, 2, 3]\n", yaml.dump(list));
278         //
279         DumperOptions options = new DumperOptions();
280         options = new DumperOptions();
281         options.setVersion(DumperOptions.Version.V1_1);
282         yaml = new Yaml(options);
283         assertEquals("%YAML 1.1\n--- [1, 2, 3]\n", yaml.dump(list));
284         //
285         options.setVersion(DumperOptions.Version.V1_0);
286         yaml = new Yaml(options);
287         assertEquals("%YAML 1.0\n--- [1, 2, 3]\n", yaml.dump(list));
288         //
289         assertEquals("Version: 1.1", DumperOptions.Version.V1_1.toString());
290     }
291 
292     public void testTags() {
293         Yaml yaml = new Yaml();
294         List<Integer> list = new ArrayList<Integer>();
295         list.add(1);
296         list.add(2);
297         list.add(3);
298         assertEquals("[1, 2, 3]\n", yaml.dump(list));
299         //
300         DumperOptions options = new DumperOptions();
301         options = new DumperOptions();
302         Map<String, String> tags = new LinkedHashMap<String, String>();
303         tags.put("!foo!", "bar");
304         options.setTags(tags);
305         yaml = new Yaml(options);
306         assertEquals("%TAG !foo! bar\n--- [1, 2, 3]\n", yaml.dump(list));
307         //
308         options = new DumperOptions();
309         tags.put("!yaml!", Tag.PREFIX);
310         yaml = new Yaml(options);
311         assertEquals("foo\n", yaml.dump("foo"));
312     }
313 
314     public void testAllowUnicode() {
315         Yaml yaml = new Yaml();
316         assertEquals("out: " + yaml.dump("\u00DCber"), "\u00DCber\n", yaml.dump("\u00DCber"));
317         //
318         DumperOptions options = new DumperOptions();
319         options = new DumperOptions();
320         options.setAllowUnicode(false);
321         yaml = new Yaml(options);
322         assertEquals("\"\\xdcber\"\n", yaml.dump("\u00DCber"));
323     }
324 
325     @SuppressWarnings("deprecation")
326     public void testSetRootTag() {
327         DumperOptions options = new DumperOptions();
328         try {
329             options.setExplicitRoot((Tag) null);
330             fail("Root tag is required.");
331         } catch (NullPointerException e) {
332             assertEquals("Root tag must be specified.", e.getMessage());
333         }
334     }
335 
336     public void testToString() {
337         DumperOptions.ScalarStyle scalarStyle = DumperOptions.ScalarStyle.LITERAL;
338         assertEquals("Scalar style: '|'", scalarStyle.toString());
339         //
340         DumperOptions.FlowStyle flowStyle = DumperOptions.FlowStyle.BLOCK;
341         assertEquals("Flow style: 'false'", flowStyle.toString());
342         //
343         DumperOptions.LineBreak lb = DumperOptions.LineBreak.UNIX;
344         assertEquals("Line break: UNIX", lb.toString());
345     }
346 
347     public void testWithRepresenter() {
348         Representer representer = new Representer();
349         DumperOptions options = new DumperOptions();
350         options.setIndent(4);
351         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
352         Yaml yaml = new Yaml(representer, options);
353         List<Integer> list = new ArrayList<Integer>();
354         list.add(1);
355         list.add(2);
356         list.add(3);
357         Map<String, Object> map = new LinkedHashMap<String, Object>();
358         map.put("a", "b");
359         map.put("c", list);
360         assertEquals("a: b\nc:\n- 1\n- 2\n- 3\n", yaml.dump(map));
361     }
362 }