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.issues.issue40;
    18
    19  import java.math.BigDecimal;
    20
    21  import junit.framework.TestCase;
    22
    23  import org.yaml.snakeyaml.Yaml;
    24
    25  public class DogFoodBeanTest extends TestCase {
    26
    27      public void testOwnBigDecimal() {
    28          DogFoodBean input = new DogFoodBean();
    29          input.setDecimal(new BigDecimal("5"));
    30          Yaml yaml = new Yaml();
    31          String text = yaml.dump(input);
    32          // System.out.println(text);
    33          assertEquals("!!org.yaml.snakeyaml.issues.issue40.DogFoodBean {decimal: !!float '5'}\n",
    34                  text);
    35          DogFoodBean output = (DogFoodBean) yaml.load(text);
    36          assertEquals(output.getDecimal(), input.getDecimal());
    37      }
    38
    39      public void testBigDecimalPrecision() {
    40          DogFoodBean input = new DogFoodBean();
    41          input.setDecimal(new BigDecimal("5.123"));
    42          Yaml yaml = new Yaml();
    43          String text = yaml.dump(input);
    44          // System.out.println(text);
    45          assertEquals("!!org.yaml.snakeyaml.issues.issue40.DogFoodBean {decimal: 5.123}\n", text);
    46          DogFoodBean output = (DogFoodBean) yaml.load(text);
    47          assertEquals(input.getDecimal(), output.getDecimal());
    48      }
    49
    50      public void testBigDecimalNoRootTag() {
    51          DogFoodBean input = new DogFoodBean();
    52          input.setDecimal(new BigDecimal("5.123"));
    53          Yaml yaml = new Yaml();
    54          String text = yaml.dumpAsMap(input);
    55          // System.out.println(text);
    56          assertEquals("decimal: 5.123\n", text);
    57          Yaml loader = new Yaml();
    58          DogFoodBean output = loader.loadAs(text, DogFoodBean.class);
    59          assertEquals(input.getDecimal(), output.getDecimal());
    60      }
    61
    62      public void testBigDecimal1() {
    63          Yaml yaml = new Yaml();
    64          String text = yaml.dump(new BigDecimal("5"));
    65          assertEquals("!!float '5'\n", text);
    66      }
    67
    68      public void testBigDecimal2() {
    69          Yaml yaml = new Yaml();
    70          String text = yaml.dump(new BigDecimal("5.123"));
    71          assertEquals("5.123\n", text);
    72      }
    73  }