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.object;
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 CharacterObjectMorpherTest extends AbstractObjectMorpherTestCase
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( CharacterObjectMorpherTest.class );
38        suite.setName( "CharacterObjectMorpher Tests" );
39        return suite;
40     }
41  
42     private CharacterObjectMorpher anotherMorpher;
43     private CharacterObjectMorpher anotherMorpherWithDefaultValue;
44     private CharacterObjectMorpher morpher;
45     private CharacterObjectMorpher morpherWithDefaultValue;
46  
47     public CharacterObjectMorpherTest( String name )
48     {
49        super( name );
50     }
51  
52     // -----------------------------------------------------------------------
53  
54     public void testCharMorph()
55     {
56        String expected = String.valueOf( "A" );
57        Character actual = (Character) new CharacterObjectMorpher().morph( expected );
58        assertEquals( new Character( 'A' ), actual );
59     }
60  
61     public void testCharMorph_noConversion()
62     {
63        Character expected = new Character( 'A' );
64        Character actual = (Character) new CharacterObjectMorpher().morph( expected );
65        assertEquals( expected, actual );
66        assertSame( expected, actual );
67     }
68  
69     public void testCharMorph_throwException_emptyString()
70     {
71        try{
72           new CharacterObjectMorpher().morph( "" );
73           fail( "Should have thrown an Exception" );
74        }
75        catch( MorphException expected ){
76           // ok
77        }
78     }
79  
80     public void testCharMorph_throwException_null()
81     {
82        try{
83           new CharacterObjectMorpher().morph( null );
84           fail( "Should have thrown an Exception" );
85        }
86        catch( MorphException expected ){
87           // ok
88        }
89     }
90  
91     public void testCharMorph_useDefault()
92     {
93        String expected = String.valueOf( "" );
94        Character actual = (Character) new CharacterObjectMorpher( new Character( 'A' ) ).morph( expected );
95        assertEquals( new Character( 'A' ), actual );
96     }
97  
98     public void testCharMorph_useDefault_null()
99     {
100       Character actual = (Character) new CharacterObjectMorpher( new Character( 'A' ) ).morph( null );
101       assertEquals( new Character( 'A' ), actual );
102    }
103 
104    protected Morpher getAnotherMorpher()
105    {
106       return anotherMorpher;
107    }
108 
109    protected Morpher getAnotherMorpherWithDefaultValue()
110    {
111       return anotherMorpherWithDefaultValue;
112    }
113 
114    protected Morpher getMorpher()
115    {
116       return morpher;
117    }
118 
119    protected Morpher getMorpherWithDefaultValue()
120    {
121       return morpherWithDefaultValue;
122    }
123 
124    protected void setUp() throws Exception
125    {
126       morpher = new CharacterObjectMorpher();
127       morpherWithDefaultValue = new CharacterObjectMorpher( new Character( 'A' ) );
128       anotherMorpher = new CharacterObjectMorpher();
129       anotherMorpherWithDefaultValue = new CharacterObjectMorpher( new Character( 'B' ) );
130    }
131 }