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.primitive;
18  
19  import junit.framework.Test;
20  import junit.framework.TestSuite;
21  import junit.textui.TestRunner;
22  import net.sf.ezmorph.MorphException;
23  import net.sf.ezmorph.Morpher;
24  
25  /**
26   * @author Andres Almiray <aalmiray@users.sourceforge.net>
27   */
28  public class DoubleMorpherTest extends AbstractMorpherTestCase
29  {
30     public static void main( String[] args )
31     {
32        TestRunner.run( suite() );
33     }
34  
35     public static Test suite()
36     {
37        TestSuite suite = new TestSuite( DoubleMorpherTest.class );
38        suite.setName( "DoubleMorpher Tests" );
39        return suite;
40     }
41  
42     private Morpher anotherMorpher;
43     private Morpher anotherMorpherWithDefaultValue;
44     private Morpher morpher;
45     private Morpher morpherWithDefaultValue;
46  
47     public DoubleMorpherTest( String name )
48     {
49        super( name );
50     }
51  
52     // -----------------------------------------------------------------------
53  
54     public void testDoubleMorph_throwException()
55     {
56        try{
57           ((DoubleMorpher) getMorpher()).morph( String.valueOf( "A" ) );
58           fail( "Should have thrown an Exception" );
59        }
60        catch( MorphException expected ){
61           // ok
62        }
63     }
64  
65     public void testDoubleMorph_throwException_null()
66     {
67        try{
68           ((DoubleMorpher) getMorpher()).morph( null );
69           fail( "Should have thrown an Exception" );
70        }
71        catch( MorphException expected ){
72           // ok
73        }
74     }
75  
76     public void testDoubleMorph_useDefault()
77     {
78        String expected = String.valueOf( "A" );
79        double actual = ((DoubleMorpher) getMorpherWithDefaultValue()).morph( expected );
80        assertEquals( 0d, actual, 0d );
81     }
82  
83     public void testDoubleMorph_useDefault_null()
84     {
85        double actual = ((DoubleMorpher) getMorpherWithDefaultValue()).morph( null );
86        assertEquals( 0d, actual, 0d );
87     }
88  
89     public void testDoubleMorphDecimalValue_Number()
90     {
91        Double expected = new Double( 3.1416d );
92        double actual = ((DoubleMorpher) getMorpher()).morph( expected );
93        assertEquals( 3.1416d, actual, 0d );
94     }
95  
96     public void testDoubleMorphDecimalValue_String()
97     {
98        String expected = "3.1416";
99        double actual = ((DoubleMorpher) getMorpher()).morph( expected );
100       assertEquals( 3.1416d, actual, 0d );
101    }
102 
103    public void testDoubleMorphMaxValue_Number()
104    {
105       Double expected = new Double( Double.MAX_VALUE );
106       double actual = ((DoubleMorpher) getMorpher()).morph( expected );
107       assertEquals( expected.doubleValue(), actual, 0d );
108    }
109 
110    public void testDoubleMorphMaxValue_String()
111    {
112       String expected = String.valueOf( new Double( Double.MAX_VALUE ) );
113       double actual = ((DoubleMorpher) getMorpher()).morph( expected );
114       assertEquals( expected, String.valueOf( actual ) );
115    }
116 
117    public void testDoubleMorphMinValue_Number()
118    {
119       Double expected = new Double( Double.MIN_VALUE );
120       double actual = ((DoubleMorpher) getMorpher()).morph( expected );
121       assertEquals( expected.doubleValue(), actual, 0d );
122    }
123 
124    public void testDoubleMorphMinValue_String()
125    {
126       String expected = String.valueOf( new Double( Double.MIN_VALUE ) );
127       double actual = ((DoubleMorpher) getMorpher()).morph( expected );
128       assertEquals( expected, String.valueOf( actual ) );
129    }
130 
131    protected Morpher getMorpher()
132    {
133       return morpher;
134    }
135 
136    protected Morpher getMorpherWithDefaultValue()
137    {
138       return morpherWithDefaultValue;
139    }
140 
141    protected Class getMorphsToClass()
142    {
143       return Double.TYPE;
144    }
145 
146    protected Morpher getAnotherMorpher()
147    {
148       return anotherMorpher;
149    }
150 
151    protected Morpher getAnotherMorpherWithDefaultValue()
152    {
153       return anotherMorpherWithDefaultValue;
154    }
155 
156    protected void setUp() throws Exception
157    {
158       morpher = new DoubleMorpher();
159       morpherWithDefaultValue = new DoubleMorpher( 0 );
160       anotherMorpher = new DoubleMorpher();
161       anotherMorpherWithDefaultValue = new DoubleMorpher( 1 );
162    }
163 }