1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ezmorph.primitive;
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
29
30
31 public class BooleanMorpherTest extends AbstractMorpherTestCase
32 {
33 public static void main( String[] args )
34 {
35 TestRunner.run( suite() );
36 }
37
38 public static Test suite()
39 {
40 TestSuite suite = new TestSuite( BooleanMorpherTest.class );
41 suite.setName( "BooleanMorpher Tests" );
42 return suite;
43 }
44
45 private Morpher anotherMorpher;
46 private Morpher anotherMorpherWithDefaultValue;
47 private Morpher morpher;
48 private Morpher morpherWithDefaultValue;
49
50 public BooleanMorpherTest( String name )
51 {
52 super( name );
53 }
54
55
56
57 public void testBooleanMorph_noConversion()
58 {
59 boolean actual = ((BooleanMorpher) getMorpherWithDefaultValue()).morph( Boolean.TRUE );
60 assertEquals( true, actual );
61 }
62
63 public void testBooleanMorph_throwException()
64 {
65 try{
66 ((BooleanMorpher) getMorpher()).morph( "A" );
67 fail( "Should have thrown an Exception" );
68 }
69 catch( MorphException expected ){
70
71 }
72 }
73
74 public void testBooleanMorph_throwException_null()
75 {
76 try{
77 ((BooleanMorpher) getMorpher()).morph( null );
78 fail( "Should have thrown an Exception" );
79 }
80 catch( MorphException expected ){
81
82 }
83 }
84
85 public void testBooleanMorph_useDefault()
86 {
87 String expected = String.valueOf( "A" );
88 boolean actual = ((BooleanMorpher) getMorpherWithDefaultValue()).morph( expected );
89 assertEquals( true, actual );
90 }
91
92 public void testBooleanMorph_useDefault_null()
93 {
94 boolean actual = ((BooleanMorpher) getMorpherWithDefaultValue()).morph( null );
95 assertEquals( true, actual );
96 }
97
98 public void testBooleanMorphNumberValues_false()
99 {
100 assertFalse( ((BooleanMorpher) getMorpher()).morph( new Byte( (byte) 0 ) ) );
101 assertFalse( ((BooleanMorpher) getMorpher()).morph( new Short( (short) 0 ) ) );
102 assertFalse( ((BooleanMorpher) getMorpher()).morph( new Integer( 0 ) ) );
103 assertFalse( ((BooleanMorpher) getMorpher()).morph( new Long( 0 ) ) );
104 assertFalse( ((BooleanMorpher) getMorpher()).morph( new Float( 0 ) ) );
105 assertFalse( ((BooleanMorpher) getMorpher()).morph( new Double( 0 ) ) );
106 assertFalse( ((BooleanMorpher) getMorpher()).morph( BigInteger.ZERO ) );
107 assertFalse( ((BooleanMorpher) getMorpher()).morph( MorphUtils.BIGDECIMAL_ZERO ) );
108 }
109
110 public void testBooleanMorphNumberValues_true()
111 {
112 assertTrue( ((BooleanMorpher) getMorpher()).morph( new Byte( (byte) 1 ) ) );
113 assertTrue( ((BooleanMorpher) getMorpher()).morph( new Short( (short) 1 ) ) );
114 assertTrue( ((BooleanMorpher) getMorpher()).morph( new Integer( 1 ) ) );
115 assertTrue( ((BooleanMorpher) getMorpher()).morph( new Long( 1 ) ) );
116 assertTrue( ((BooleanMorpher) getMorpher()).morph( new Float( 1 ) ) );
117 assertTrue( ((BooleanMorpher) getMorpher()).morph( new Double( 1 ) ) );
118 assertTrue( ((BooleanMorpher) getMorpher()).morph( BigInteger.ONE ) );
119 assertTrue( ((BooleanMorpher) getMorpher()).morph( MorphUtils.BIGDECIMAL_ONE ) );
120
121 assertTrue( ((BooleanMorpher) getMorpher()).morph( new Double( Float.NEGATIVE_INFINITY ) ) );
122 assertTrue( ((BooleanMorpher) getMorpher()).morph( new Double( Float.POSITIVE_INFINITY ) ) );
123 assertTrue( ((BooleanMorpher) getMorpher()).morph( new Double( Float.NaN ) ) );
124 assertTrue( ((BooleanMorpher) getMorpher()).morph( new Double( Double.NEGATIVE_INFINITY ) ) );
125 assertTrue( ((BooleanMorpher) getMorpher()).morph( new Double( Double.POSITIVE_INFINITY ) ) );
126 assertTrue( ((BooleanMorpher) getMorpher()).morph( new Double( Double.NaN ) ) );
127 }
128
129 public void testBooleanMorphStringValues_false()
130 {
131 assertFalse( ((BooleanMorpher) getMorpher()).morph( "false" ) );
132 assertFalse( ((BooleanMorpher) getMorpher()).morph( "no" ) );
133 assertFalse( ((BooleanMorpher) getMorpher()).morph( "off" ) );
134 }
135
136 public void testBooleanMorphStringValues_true()
137 {
138 assertTrue( ((BooleanMorpher) getMorpher()).morph( "true" ) );
139 assertTrue( ((BooleanMorpher) getMorpher()).morph( "yes" ) );
140 assertTrue( ((BooleanMorpher) getMorpher()).morph( "on" ) );
141 }
142
143 protected Morpher getAnotherMorpher()
144 {
145 return anotherMorpher;
146 }
147
148 protected Morpher getAnotherMorpherWithDefaultValue()
149 {
150 return anotherMorpherWithDefaultValue;
151 }
152
153 protected Morpher getMorpher()
154 {
155 return morpher;
156 }
157
158 protected Morpher getMorpherWithDefaultValue()
159 {
160 return morpherWithDefaultValue;
161 }
162
163 protected Class getMorphsToClass()
164 {
165 return Boolean.TYPE;
166 }
167
168 protected void setUp() throws Exception
169 {
170 morpher = new BooleanMorpher();
171 morpherWithDefaultValue = new BooleanMorpher( true );
172 anotherMorpher = new BooleanMorpher();
173 anotherMorpherWithDefaultValue = new BooleanMorpher( false );
174 }
175 }