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.constructor;
17  
18  import java.math.BigDecimal;
19  
20  import junit.framework.TestCase;
21  
22  import org.yaml.snakeyaml.Yaml;
23  
24  public class BigDecimalBeanConstructorTest extends TestCase {
25  
26      public void testRepresentor() {
27          BigDecimalJavaBean bean = new BigDecimalJavaBean();
28          bean.setAmount(1.5f);
29          bean.setNumber(new BigDecimal("3.1416"));
30          Yaml yaml = new Yaml();
31          String output = yaml.dump(bean);
32          String className = this.getClass().getPackage().getName();
33          assertEquals("!!" + className + ".BigDecimalJavaBean {amount: 1.5, number: 3.1416}\n",
34                  output);
35      }
36  
37      public void testConstructor() {
38          String className = "!!" + this.getClass().getPackage().getName()
39                  + ".BigDecimalJavaBean {amount: 1.5, number: 3.1416}";
40          Yaml yaml = new Yaml();
41          BigDecimalJavaBean bean = (BigDecimalJavaBean) yaml.load(className);
42          assertNotNull(bean);
43          assertTrue(1.5 - bean.getAmount() < 0.0000001);
44          assertTrue((new BigDecimal("3.1416")).add(bean.getNumber().negate()).doubleValue() < 0.0000001);
45      }
46  
47      public void testConstructorAtomic() {
48          String className = "!!" + this.getClass().getPackage().getName()
49                  + ".AtomicJavaBean {amount: 1.5, atomic: 0}";
50          Yaml yaml = new Yaml();
51          try {
52              yaml.load(className);
53              fail("AtomicLong is not supported.");
54          } catch (Exception e) {
55              assertEquals(
56                      "Cannot create property=atomic for JavaBean=AtomicJavaBean; Unsupported class: class java.util.concurrent.atomic.AtomicLong",
57                      e.getCause().getMessage());
58          }
59      }
60  }