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