View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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  
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  
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  
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 
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 
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     @SuppressWarnings("deprecation")
325     public void testSetRootTag() {
326         DumperOptions options = new DumperOptions();
327         try {
328             options.setExplicitRoot((Tag) null);
329             fail("Root tag is required.");
330         } catch (NullPointerException e) {
331             assertEquals("Root tag must be specified.", e.getMessage());
332         }
333     }
334 
335     public void testToString() {
336         DumperOptions.ScalarStyle scalarStyle = DumperOptions.ScalarStyle.LITERAL;
337         assertEquals("Scalar style: '|'", scalarStyle.toString());
338         //
339         DumperOptions.FlowStyle flowStyle = DumperOptions.FlowStyle.BLOCK;
340         assertEquals("Flow style: 'false'", flowStyle.toString());
341         //
342         DumperOptions.LineBreak lb = DumperOptions.LineBreak.UNIX;
343         assertEquals("Line break: UNIX", lb.toString());
344     }
345 
346     public void testWithRepresenter() {
347         Representer representer = new Representer();
348         DumperOptions options = new DumperOptions();
349         options.setIndent(4);
350         options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
351         Yaml yaml = new Yaml(representer, options);
352         List<Integer> list = new ArrayList<Integer>();
353         list.add(1);
354         list.add(2);
355         list.add(3);
356         Map<String, Object> map = new LinkedHashMap<String, Object>();
357         map.put("a", "b");
358         map.put("c", list);
359         assertEquals("a: b\nc:\n- 1\n- 2\n- 3\n", yaml.dump(map));
360     }
361 }