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 ByteMorpherTest 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( ByteMorpherTest.class );
38 suite.setName( "ByteMorpher 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 ByteMorpherTest( String name )
48 {
49 super( name );
50 }
51
52
53
54 public void testByteMorph_throwException()
55 {
56 try{
57 ((ByteMorpher) getMorpher()).morph( String.valueOf( "A" ) );
58 fail( "Should have thrown an Exception" );
59 }
60 catch( MorphException expected ){
61
62 }
63 }
64
65 public void testByteMorph_throwException_null()
66 {
67 try{
68 ((ByteMorpher) getMorpher()).morph( null );
69 fail( "Should have thrown an Exception" );
70 }
71 catch( MorphException expected ){
72
73 }
74 }
75
76 public void testByteMorph_useDefault()
77 {
78 String expected = String.valueOf( "A" );
79 byte actual = ((ByteMorpher) getMorpherWithDefaultValue()).morph( expected );
80 assertEquals( 0, actual );
81 }
82
83 public void testByteMorph_useDefault_null()
84 {
85 byte actual = ((ByteMorpher) getMorpherWithDefaultValue()).morph( null );
86 assertEquals( 0, actual );
87 }
88
89 public void testByteMorphDecimalValue_Number()
90 {
91 Double expected = new Double( 3.1416d );
92 byte actual = ((ByteMorpher) getMorpher()).morph( expected );
93 assertEquals( 3, actual );
94 }
95
96 public void testByteMorphDecimalValue_Number_outOfRange()
97 {
98 byte actual = ((ByteMorpher) getMorpher()).morph( new Double( Double.MAX_VALUE ) );
99 assertEquals( -1, actual );
100 }
101
102 public void testByteMorphDecimalValue_String()
103 {
104 String expected = "3.1416";
105 byte actual = ((ByteMorpher) getMorpher()).morph( expected );
106 assertEquals( 3, actual );
107 }
108
109 public void testByteMorphMaxValue_Number()
110 {
111 Byte expected = new Byte( Byte.MAX_VALUE );
112 byte actual = ((ByteMorpher) getMorpher()).morph( expected );
113 assertEquals( expected.byteValue(), actual );
114 }
115
116 public void testByteMorphMaxValue_String()
117 {
118 String expected = String.valueOf( new Byte( Byte.MAX_VALUE ) );
119 byte actual = ((ByteMorpher) getMorpher()).morph( expected );
120 assertEquals( expected, String.valueOf( actual ) );
121 }
122
123 public void testByteMorphMinValue_Number()
124 {
125 Byte expected = new Byte( Byte.MIN_VALUE );
126 byte actual = ((ByteMorpher) getMorpher()).morph( expected );
127 assertEquals( expected.byteValue(), actual );
128 }
129
130 public void testByteMorphMinValue_String()
131 {
132 String expected = String.valueOf( new Byte( Byte.MIN_VALUE ) );
133 byte actual = ((ByteMorpher) 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 Byte.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 ByteMorpher();
165 morpherWithDefaultValue = new ByteMorpher( (byte) 0 );
166 anotherMorpher = new ByteMorpher();
167 anotherMorpherWithDefaultValue = new ByteMorpher( (byte) 1 );
168 }
169 }