1   /*
2    * Copyright 2006-2007 the original author or authors.
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 net.sf.ezmorph.object;
18  
19  import java.math.BigDecimal;
20  import java.math.BigInteger;
21  
22  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  import junit.textui.TestRunner;
25  import net.sf.ezmorph.MorphException;
26  import net.sf.ezmorph.MorphUtils;
27  import net.sf.ezmorph.Morpher;
28  
29  public class BigDecimalMorpherTest extends AbstractObjectMorpherTestCase
30  {
31  
32     public static void main( String[] args )
33     {
34        TestRunner.run( suite() );
35     }
36  
37     public static Test suite()
38     {
39        TestSuite suite = new TestSuite( BigDecimalMorpherTest.class );
40        suite.setName( "BigDecimalMorpher Tests" );
41        return suite;
42     }
43  
44     private BigDecimalMorpher anotherMorpher;
45     private BigDecimalMorpher anotherMorpherWithDefaultValue;
46     private BigDecimalMorpher morpher;
47     private BigDecimalMorpher morpherWithDefaultValue;
48  
49     public BigDecimalMorpherTest( String name )
50     {
51        super( name );
52     }
53  
54     // -----------------------------------------------------------------------
55  
56     public void testBigDecimalMorph_BigDecimal()
57     {
58        Object actual = ((BigDecimalMorpher) getMorpherWithDefaultValue()).morph( MorphUtils.BIGDECIMAL_ZERO );
59        assertEquals( MorphUtils.BIGDECIMAL_ZERO, actual );
60     }
61  
62     public void testBigDecimalMorph_BigInteger()
63     {
64        Object actual = ((BigDecimalMorpher) getMorpherWithDefaultValue()).morph( BigInteger.ZERO );
65        assertEquals( MorphUtils.BIGDECIMAL_ZERO, actual );
66     }
67  
68     public void testBigDecimalMorph_Number()
69     {
70        Object actual = ((BigDecimalMorpher) getMorpherWithDefaultValue()).morph( new Float( 1f ) );
71        assertEquals( MorphUtils.BIGDECIMAL_ONE, actual );
72        actual = ((BigDecimalMorpher) getMorpherWithDefaultValue()).morph( new Double( 1d ) );
73        assertEquals( MorphUtils.BIGDECIMAL_ONE, actual );
74     }
75  
76     public void testBigDecimalMorph_Number__Double_INFINITY()
77     {
78        try{
79           ((BigDecimalMorpher) getMorpher()).morph( new Double( Double.POSITIVE_INFINITY ) );
80           fail( "Should have thrown an Exception" );
81        }
82        catch( MorphException expected ){
83           // ok
84        }
85     }
86  
87     public void testBigDecimalMorph_Number__Double_NAN()
88     {
89        try{
90           ((BigDecimalMorpher) getMorpher()).morph( new Double( Double.NaN ) );
91           fail( "Should have thrown an Exception" );
92        }
93        catch( MorphException expected ){
94           // ok
95        }
96     }
97  
98     public void testBigDecimalMorph_Number__Float_INFINITY()
99     {
100       try{
101          ((BigDecimalMorpher) getMorpher()).morph( new Float( Float.POSITIVE_INFINITY ) );
102          fail( "Should have thrown an Exception" );
103       }
104       catch( MorphException expected ){
105          // ok
106       }
107    }
108 
109    public void testBigDecimalMorph_Number__Float_NAN()
110    {
111       try{
112          ((BigDecimalMorpher) getMorpher()).morph( new Float( Float.NaN ) );
113          fail( "Should have thrown an Exception" );
114       }
115       catch( MorphException expected ){
116          // ok
117       }
118    }
119 
120    public void testBigDecimalMorph_String()
121    {
122       Object actual = ((BigDecimalMorpher) getMorpherWithDefaultValue()).morph( "123.45" );
123       assertEquals( new BigDecimal( "123.45" ), actual );
124    }
125 
126    public void testBigDecimalMorph_String_empty()
127    {
128       assertNull( ((BigDecimalMorpher) getMorpher()).morph( "" ) );
129    }
130 
131    public void testBigDecimalMorph_String_null()
132    {
133       assertNull( ((BigDecimalMorpher) getMorpher()).morph( null ) );
134    }
135 
136    public void testBigDecimalMorph_String_null2()
137    {
138       assertNull( ((BigDecimalMorpher) getMorpher()).morph( "null" ) );
139    }
140 
141    public void testBigDecimalMorph_throwException()
142    {
143       try{
144          ((BigDecimalMorpher) getMorpher()).morph( String.valueOf( "A" ) );
145          fail( "Should have thrown an Exception" );
146       }
147       catch( MorphException expected ){
148          // ok
149       }
150    }
151 
152    public void testBigDecimalMorph_useDefault()
153    {
154       String expected = String.valueOf( "A" );
155       Object actual = ((BigDecimalMorpher) getMorpherWithDefaultValue()).morph( expected );
156       assertEquals( MorphUtils.BIGDECIMAL_ZERO, actual );
157    }
158 
159    public void testBigDecimalMorph_useDefault_null()
160    {
161       Object actual = ((BigDecimalMorpher) getMorpherWithDefaultValue()).morph( null );
162       assertEquals( MorphUtils.BIGDECIMAL_ZERO, actual );
163    }
164 
165    protected Morpher getAnotherMorpher()
166    {
167       return anotherMorpher;
168    }
169 
170    protected Morpher getAnotherMorpherWithDefaultValue()
171    {
172       return anotherMorpherWithDefaultValue;
173    }
174 
175    protected Morpher getMorpher()
176    {
177       return morpher;
178    }
179 
180    protected Morpher getMorpherWithDefaultValue()
181    {
182       return morpherWithDefaultValue;
183    }
184 
185    protected void setUp() throws Exception
186    {
187       morpher = new BigDecimalMorpher();
188       morpherWithDefaultValue = new BigDecimalMorpher( MorphUtils.BIGDECIMAL_ZERO );
189       anotherMorpher = new BigDecimalMorpher();
190       anotherMorpherWithDefaultValue = new BigDecimalMorpher( MorphUtils.BIGDECIMAL_ONE );
191    }
192 }