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 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           // ok
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           // ok
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 }