1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
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 }