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  package org.yaml.snakeyaml.issues.issue124;
    17
    18  import junit.framework.TestCase;
    19
    20  import org.yaml.snakeyaml.DumperOptions;
    21  import org.yaml.snakeyaml.DumperOptions.FlowStyle;
    22  import org.yaml.snakeyaml.Yaml;
    23  import org.yaml.snakeyaml.nodes.Tag;
    24
    25  public class DumpTest extends TestCase {
    26
    27      public void testDumperOptionsSideEffect() {
    28          Yaml yaml = new Yaml();
    29          Bean124 bean = new Bean124();
    30          String output0 = yaml.dump(bean);
    31          // System.out.println(output0);
    32          assertEquals("!!org.yaml.snakeyaml.issues.issue124.Bean124\na: aaa\nnumbers: [1, 2, 3]\n",
    33                  output0);
    34          String output1 = yaml.dumpAsMap(bean);
    35          // System.out.println(output1);
    36          assertEquals("a: aaa\nnumbers:\n- 1\n- 2\n- 3\n", output1);
    37          String output2 = yaml.dump(bean);
    38          // System.out.println(output2);
    39          assertEquals("Yaml.dumpAs() should not have any side effects.", output0, output2);
    40      }
    41
    42      public void testDumperDifferentTag() {
    43          Yaml yaml = new Yaml();
    44          Bean124 bean = new Bean124();
    45          String output1 = yaml.dumpAs(bean, new Tag("!!foo.bar"), FlowStyle.BLOCK);
    46          assertEquals("!!foo.bar\na: aaa\nnumbers:\n- 1\n- 2\n- 3\n", output1);
    47      }
    48
    49      public void testDumperFlowStyle() {
    50          Yaml yaml = new Yaml();
    51          Bean124 bean = new Bean124();
    52          String output1 = yaml.dumpAs(bean, new Tag("!!foo.bar"), FlowStyle.FLOW);
    53          assertEquals("!!foo.bar {a: aaa, numbers: [1, 2, 3]}\n", output1);
    54      }
    55
    56      public void testDumperAutoStyle() {
    57          Yaml yaml = new Yaml();
    58          Bean124 bean = new Bean124();
    59          String output1 = yaml.dumpAs(bean, new Tag("!!foo.bar"), FlowStyle.AUTO);
    60          assertEquals("!!foo.bar\na: aaa\nnumbers: [1, 2, 3]\n", output1);
    61      }
    62
    63      public void testDumperNullStyle() {
    64          Yaml yaml = new Yaml();
    65          Bean124 bean = new Bean124();
    66          String output1 = yaml.dumpAs(bean, new Tag("!!foo.bar"), null);
    67          assertEquals("!!foo.bar\na: aaa\nnumbers: [1, 2, 3]\n", output1);
    68      }
    69
    70      public void testDumperNullStyle2() {
    71          DumperOptions options = new DumperOptions();
    72          options.setDefaultFlowStyle(FlowStyle.BLOCK);
    73          Yaml yaml = new Yaml(options);
    74          Bean124 bean = new Bean124();
    75          String output1 = yaml.dumpAs(bean, new Tag("!!foo2.bar2"), null);
    76          assertEquals("!!foo2.bar2\na: aaa\nnumbers:\n- 1\n- 2\n- 3\n", output1);
    77      }
    78
    79      public void testDumperNullTag() {
    80          Yaml yaml = new Yaml();
    81          Bean124 bean = new Bean124();
    82          String output1 = yaml.dumpAs(bean, null, FlowStyle.BLOCK);
    83          assertEquals(
    84                  "!!org.yaml.snakeyaml.issues.issue124.Bean124\na: aaa\nnumbers:\n- 1\n- 2\n- 3\n",
    85                  output1);
    86      }
    87  }