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.StringWriter;
    20
    21  import junit.framework.TestCase;
    22
    23  import org.yaml.snakeyaml.DumperOptions.FlowStyle;
    24  import org.yaml.snakeyaml.representer.Representer;
    25
    26  @SuppressWarnings("deprecation")
    27  public class JavaBeanDumperTest extends TestCase {
    28
    29      public void testDumpObjectWriter() {
    30          DumpBean bean = new DumpBean();
    31          bean.setName("Name1");
    32          bean.setNumber(3);
    33          JavaBeanDumper dumper = new JavaBeanDumper();
    34          String output = dumper.dump(bean);
    35          assertEquals("name: Name1\nnumber: 3\n", output);
    36          assertEquals(FlowStyle.BLOCK, dumper.getFlowStyle());
    37          assertFalse(dumper.isUseGlobalTag());
    38          //
    39          dumper.setFlowStyle(FlowStyle.AUTO);
    40          output = dumper.dump(bean);
    41          assertEquals("{name: Name1, number: 3}\n", output);
    42          //
    43          dumper.setUseGlobalTag(true);
    44          output = dumper.dump(bean);
    45          assertEquals("!!org.yaml.snakeyaml.JavaBeanDumperTest$DumpBean {name: Name1, number: 3}\n",
    46                  output);
    47      }
    48
    49      public void testDumpObject() {
    50          DumpBean bean = new DumpBean();
    51          bean.setName("Name2");
    52          bean.setNumber(4);
    53          JavaBeanDumper dumper = new JavaBeanDumper();
    54          StringWriter buffer = new StringWriter();
    55          dumper.dump(bean, buffer);
    56          assertEquals("name: Name2\nnumber: 4\n", buffer.toString());
    57      }
    58
    59      public void testDumpObject2() {
    60          DumpBean bean = new DumpBean();
    61          bean.setName("Name2");
    62          bean.setNumber(4);
    63          JavaBeanDumper dumper = new JavaBeanDumper();
    64          StringWriter buffer = new StringWriter();
    65          dumper.dump(bean, buffer);
    66          assertEquals("name: Name2\nnumber: 4\n", buffer.toString());
    67          String output = dumper.dump(bean);
    68          assertEquals("name: Name2\nnumber: 4\n", output);
    69      }
    70
    71      public void testDumpObjectNullRepresenter() {
    72          try {
    73              new JavaBeanDumper(null, new DumperOptions());
    74              fail();
    75          } catch (NullPointerException e) {
    76              assertEquals("Representer must be provided.", e.getMessage());
    77          }
    78      }
    79
    80      public void testDumpObjectNullOptions() {
    81          try {
    82              new JavaBeanDumper(new Representer(), null);
    83              fail();
    84          } catch (NullPointerException e) {
    85              assertEquals("DumperOptions must be provided.", e.getMessage());
    86          }
    87      }
    88
    89      public static class DumpBean {
    90          private String name;
    91          private int number;
    92
    93          public String getName() {
    94              return name;
    95          }
    96
    97          public void setName(String name) {
    98              this.name = name;
    99          }
    100
    101         public int getNumber() {
    102             return number;
    103         }
    104
    105         public void setNumber(int number) {
    106             this.number = number;
    107         }
    108     }
    109 }