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