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 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
27
28 public class IntMorpherTest 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( IntMorpherTest.class );
38 suite.setName( "IntMorpher 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 IntMorpherTest( String name )
48 {
49 super( name );
50 }
51
52
53
54 public void testIntMorph_throwException()
55 {
56 try{
57 ((IntMorpher) getMorpher()).morph( String.valueOf( "A" ) );
58 fail( "Should have thrown an Exception" );
59 }
60 catch( MorphException expected ){
61
62 }
63 }
64
65 public void testIntMorph_throwException_null()
66 {
67 try{
68 ((IntMorpher) getMorpher()).morph( null );
69 fail( "Should have thrown an Exception" );
70 }
71 catch( MorphException expected ){
72
73 }
74 }
75
76 public void testIntMorph_useDefault()
77 {
78 String expected = String.valueOf( "A" );
79 int actual = ((IntMorpher) getMorpherWithDefaultValue()).morph( expected );
80 assertEquals( 0, actual );
81 }
82
83 public void testIntMorph_useDefault_null()
84 {
85 int actual = ((IntMorpher) getMorpherWithDefaultValue()).morph( null );
86 assertEquals( 0, actual );
87 }
88
89 public void testIntMorphDecimalValue_Number()
90 {
91 Double expected = new Double( 3.1416d );
92 int actual = ((IntMorpher) getMorpher()).morph( expected );
93 assertEquals( 3, actual );
94 }
95
96 public void testIntMorphDecimalValue_Number_outOfRange()
97 {
98 int actual = ((IntMorpher) getMorpher()).morph( new Double( Double.MAX_VALUE ) );
99 assertEquals( Integer.MAX_VALUE, actual );
100 }
101
102 public void testIntMorphDecimalValue_String()
103 {
104 String expected = "3.1416";
105 int actual = ((IntMorpher) getMorpher()).morph( expected );
106 assertEquals( 3, actual );
107 }
108
109 public void testIntMorphMaxValue_Number()
110 {
111 Integer expected = new Integer( Integer.MAX_VALUE );
112 int actual = ((IntMorpher) getMorpher()).morph( expected );
113 assertEquals( expected.intValue(), actual );
114 }
115
116 public void testIntMorphMaxValue_String()
117 {
118 String expected = String.valueOf( new Integer( Integer.MAX_VALUE ) );
119 int actual = ((IntMorpher) getMorpher()).morph( expected );
120 assertEquals( expected, String.valueOf( actual ) );
121 }
122
123 public void testIntMorphMinValue_Number()
124 {
125 Integer expected = new Integer( Integer.MIN_VALUE );
126 int actual = ((IntMorpher) getMorpher()).morph( expected );
127 assertEquals( expected.intValue(), actual );
128 }
129
130 public void testIntMorphMinValue_String()
131 {
132 String expected = String.valueOf( new Integer( Integer.MIN_VALUE ) );
133 int actual = ((IntMorpher) getMorpher()).morph( expected );
134 assertEquals( expected, String.valueOf( actual ) );
135 }
136
137 protected Morpher getMorpher()
138 {
139 return morpher;
140 }
141
142 protected Morpher getMorpherWithDefaultValue()
143 {
144 return morpherWithDefaultValue;
145 }
146
147 protected Class getMorphsToClass()
148 {
149 return Integer.TYPE;
150 }
151
152 protected Morpher getAnotherMorpher()
153 {
154 return anotherMorpher;
155 }
156
157 protected Morpher getAnotherMorpherWithDefaultValue()
158 {
159 return anotherMorpherWithDefaultValue;
160 }
161
162 protected void setUp() throws Exception
163 {
164 morpher = new IntMorpher();
165 morpherWithDefaultValue = new IntMorpher( 0 );
166 anotherMorpher = new IntMorpher();
167 anotherMorpherWithDefaultValue = new IntMorpher( 1 );
168 }
169 }