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.TestCase;
21  import junit.framework.TestSuite;
22  import junit.textui.TestRunner;
23  import net.sf.ezmorph.MorphException;
24  
25  /**
26   * @author Andres Almiray <aalmiray@users.sourceforge.net>
27   */
28  public class ClassMorpherTest extends TestCase
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( ClassMorpherTest.class );
38        suite.setName( "ClassMorpher Tests" );
39        return suite;
40     }
41  
42     private ClassMorpher morpher = ClassMorpher.getInstance();
43  
44     public ClassMorpherTest( String name )
45     {
46        super( name );
47     }
48  
49     // -----------------------------------------------------------------------
50  
51     public void testEquals()
52     {
53        assertTrue( ClassMorpher.getInstance()
54              .equals( ClassMorpher.getInstance() ) );
55        assertFalse( ClassMorpher.getInstance()
56              .equals( StringMorpher.getInstance() ) );
57     }
58  
59     public void testHashCode()
60     {
61        assertEquals( ClassMorpher.getInstance()
62              .hashCode(), ClassMorpher.getInstance()
63              .hashCode() );
64        assertTrue( ClassMorpher.getInstance()
65              .hashCode() != StringMorpher.getInstance()
66              .hashCode() );
67     }
68  
69     public void testMorph()
70     {
71        Class expected = Object.class;
72        Class actual = (Class) morpher.morph( "java.lang.Object" );
73        assertEquals( expected, actual );
74     }
75  
76     public void testMorph_array()
77     {
78        try{
79           morpher.morph( new boolean[] { true, false } );
80           fail( "Expected a MorphException" );
81        }
82        catch( MorphException expected ){
83           // ok
84        }
85     }
86  
87     public void testMorph_arrayClass()
88     {
89        Class expected = int[].class;
90        Class actual = (Class) morpher.morph( "[I" );
91        assertEquals( expected, actual );
92     }
93  
94     public void testMorph_class()
95     {
96        Class expected = Object.class;
97        Class actual = (Class) morpher.morph( Object.class );
98        assertEquals( expected, actual );
99     }
100 
101    public void testMorph_null()
102    {
103       assertNull( morpher.morph( null ) );
104    }
105 
106    public void testMorph_unknownClassname()
107    {
108       try{
109          morpher.morph( "bogusClass.I.do.not.exist" );
110          fail( "Expected a MorphException" );
111       }
112       catch( MorphException expected ){
113          // ok
114       }
115    }
116 
117    public void testMorph_withtoString()
118    {
119       Class expected = MyClass.class;
120       Class actual = (Class) morpher.morph( new MyClass() );
121       assertEquals( expected, actual );
122    }
123 
124    public static class MyClass{
125       public String toString(){
126          return MyClass.class.getName();
127       }
128    }
129 }