1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
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
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
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 }