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