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.BigInteger;
20  
21  import junit.framework.Test;
22  import junit.framework.TestSuite;
23  import junit.textui.TestRunner;
24  import net.sf.ezmorph.MorphException;
25  import net.sf.ezmorph.MorphUtils;
26  import net.sf.ezmorph.Morpher;
27  
28  public class BigIntegerMorpherTest extends AbstractObjectMorpherTestCase
29  {
30  
31     public static void main( String[] args )
32     {
33        TestRunner.run( suite() );
34     }
35  
36     public static Test suite()
37     {
38        TestSuite suite = new TestSuite( BigIntegerMorpherTest.class );
39        suite.setName( "BigDecimalMorpher Tests" );
40        return suite;
41     }
42  
43     private BigIntegerMorpher anotherMorpher;
44     private BigIntegerMorpher anotherMorpherWithDefaultValue;
45     private BigIntegerMorpher morpher;
46     private BigIntegerMorpher morpherWithDefaultValue;
47  
48     public BigIntegerMorpherTest( String name )
49     {
50        super( name );
51     }
52  
53     // -----------------------------------------------------------------------
54  
55     public void testBigIntegerMorph_BigDecimal()
56     {
57        Object actual = ((BigIntegerMorpher) getMorpherWithDefaultValue()).morph( MorphUtils.BIGDECIMAL_ZERO );
58        assertEquals( BigInteger.ZERO, actual );
59     }
60  
61     public void testBigIntegerMorph_BigInteger()
62     {
63        Object actual = ((BigIntegerMorpher) getMorpherWithDefaultValue()).morph( BigInteger.ZERO );
64        assertEquals( BigInteger.ZERO, actual );
65     }
66  
67     public void testBigIntegerMorph_Number()
68     {
69        Object actual = ((BigIntegerMorpher) getMorpher()).morph( new Byte( (byte) 1 ) );
70        assertEquals( BigInteger.ONE, actual );
71        actual = ((BigIntegerMorpher) getMorpher()).morph( new Short( (short) 1 ) );
72        assertEquals( BigInteger.ONE, actual );
73        actual = ((BigIntegerMorpher) getMorpher()).morph( new Integer( 1 ) );
74        assertEquals( BigInteger.ONE, actual );
75        actual = ((BigIntegerMorpher) getMorpher()).morph( new Long( 1L ) );
76        assertEquals( BigInteger.ONE, actual );
77        actual = ((BigIntegerMorpher) getMorpher()).morph( new Double( 1d ) );
78        assertEquals( BigInteger.ONE, actual );
79        actual = ((BigIntegerMorpher) getMorpher()).morph( new Float( 1f ) );
80        assertEquals( BigInteger.ONE, actual );
81        actual = ((BigIntegerMorpher) getMorpher()).morph( MorphUtils.BIGDECIMAL_ONE );
82        assertEquals( BigInteger.ONE, actual );
83     }
84  
85     public void testBigIntegerMorph_Number__Double_INFINITY()
86     {
87        try{
88           ((BigIntegerMorpher) getMorpher()).morph( new Double( Double.POSITIVE_INFINITY ) );
89           fail( "Should have thrown an Exception" );
90        }
91        catch( MorphException expected ){
92           // ok
93        }
94     }
95  
96     public void testBigIntegerMorph_Number__Double_NAN()
97     {
98        try{
99           ((BigIntegerMorpher) getMorpher()).morph( new Double( Double.NaN ) );
100          fail( "Should have thrown an Exception" );
101       }
102       catch( MorphException expected ){
103          // ok
104       }
105    }
106 
107    public void testBigIntegerMorph_Number__Float_INFINITY()
108    {
109       try{
110          ((BigIntegerMorpher) getMorpher()).morph( new Float( Float.POSITIVE_INFINITY ) );
111          fail( "Should have thrown an Exception" );
112       }
113       catch( MorphException expected ){
114          // ok
115       }
116    }
117 
118    public void testBigIntegerMorph_Number__Float_NAN()
119    {
120       try{
121          ((BigIntegerMorpher) getMorpher()).morph( new Float( Float.NaN ) );
122          fail( "Should have thrown an Exception" );
123       }
124       catch( MorphException expected ){
125          // ok
126       }
127    }
128 
129    public void testBigIntegerMorph_String__decimal()
130    {
131       Object actual = ((BigIntegerMorpher) getMorpherWithDefaultValue()).morph( "123.45" );
132       assertEquals( new BigInteger( "123" ), actual );
133    }
134 
135    public void testBigIntegerMorph_String__int()
136    {
137       Object actual = ((BigIntegerMorpher) getMorpherWithDefaultValue()).morph( "123" );
138       assertEquals( new BigInteger( "123" ), actual );
139    }
140 
141    public void testBigIntegerMorph_String_empty()
142    {
143       assertNull( ((BigIntegerMorpher) getMorpher()).morph( "" ) );
144    }
145 
146    public void testBigIntegerMorph_String_null()
147    {
148       assertNull( ((BigIntegerMorpher) getMorpher()).morph( null ) );
149    }
150 
151    public void testBigIntegerMorph_String_null2()
152    {
153       assertNull( ((BigIntegerMorpher) getMorpher()).morph( "null" ) );
154    }
155 
156    public void testBigIntegerMorph_throwException()
157    {
158       try{
159          ((BigIntegerMorpher) getMorpher()).morph( String.valueOf( "A" ) );
160          fail( "Should have thrown an Exception" );
161       }
162       catch( MorphException expected ){
163          // ok
164       }
165    }
166 
167    public void testBigIntegerMorph_useDefault()
168    {
169       String expected = String.valueOf( "A" );
170       Object actual = ((BigIntegerMorpher) getMorpherWithDefaultValue()).morph( expected );
171       assertEquals( BigInteger.ZERO, actual );
172    }
173 
174    public void testBigIntegerMorph_useDefault_null()
175    {
176       Object actual = ((BigIntegerMorpher) getMorpherWithDefaultValue()).morph( null );
177       assertEquals( BigInteger.ZERO, actual );
178    }
179 
180    protected Morpher getAnotherMorpher()
181    {
182       return anotherMorpher;
183    }
184 
185    protected Morpher getAnotherMorpherWithDefaultValue()
186    {
187       return anotherMorpherWithDefaultValue;
188    }
189 
190    protected Morpher getMorpher()
191    {
192       return morpher;
193    }
194 
195    protected Morpher getMorpherWithDefaultValue()
196    {
197       return morpherWithDefaultValue;
198    }
199 
200    protected void setUp() throws Exception
201    {
202       morpher = new BigIntegerMorpher();
203       morpherWithDefaultValue = new BigIntegerMorpher( BigInteger.ZERO );
204       anotherMorpher = new BigIntegerMorpher();
205       anotherMorpherWithDefaultValue = new BigIntegerMorpher( BigInteger.ONE );
206    }
207 }