1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ezmorph.array;
18
19 import java.lang.reflect.Array;
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24 import junit.textui.TestRunner;
25 import net.sf.ezmorph.MorphException;
26 import net.sf.ezmorph.Morpher;
27 import net.sf.ezmorph.object.IdentityObjectMorpher;
28 import net.sf.ezmorph.object.StringMorpher;
29 import net.sf.ezmorph.test.ArrayAssertions;
30
31
32
33
34 public class ObjectArrayMorpherTest extends TestCase
35 {
36 public static void main( String[] args )
37 {
38 TestRunner.run( suite() );
39 }
40
41 public static Test suite()
42 {
43 TestSuite suite = new TestSuite( ObjectArrayMorpherTest.class );
44 suite.setName( "ObjectListMorpher Tests" );
45 return suite;
46 }
47
48 private ObjectArrayMorpher anotherMorpher;
49 private ObjectArrayMorpher morpher;
50
51 public ObjectArrayMorpherTest( String name )
52 {
53 super( name );
54 }
55
56
57
58 public ObjectArrayMorpher getAnotherMorpher()
59 {
60 return anotherMorpher;
61 }
62
63 public ObjectArrayMorpher getMorpher()
64 {
65 return morpher;
66 }
67
68 public void testEquals_another_Morpher()
69 {
70 assertFalse( getMorpher().equals( getAnotherMorpher() ) );
71 }
72
73 public void testEquals_different_morpher()
74 {
75 assertFalse( getMorpher().equals( new Morpher(){
76 public Class morphsTo()
77 {
78 return null;
79 }
80
81 public boolean supports( Class clazz )
82 {
83 return false;
84 }
85 } ) );
86 }
87
88 public void testEquals_null()
89 {
90 assertFalse( getMorpher().equals( null ) );
91 }
92
93 public void testEquals_same_morpher()
94 {
95 assertTrue( getMorpher().equals( getMorpher() ) );
96 assertTrue( getAnotherMorpher().equals( getAnotherMorpher() ) );
97 }
98
99 public void testHashCode_same_morpher()
100 {
101 assertEquals( getMorpher().hashCode(), getMorpher().hashCode() );
102 assertEquals( getAnotherMorpher().hashCode(), getAnotherMorpher().hashCode() );
103 }
104
105 public void testMorph_illegalArgument()
106 {
107 try{
108
109 morpher.morph( "" );
110 }
111 catch( MorphException expected ){
112
113 }
114 }
115
116 public void testMorph_null()
117 {
118 assertNull( morpher.morph( null ) );
119 }
120
121 public void testMorph_onedim()
122 {
123 Object[] input = new Object[] { new Integer( 1 ), Boolean.TRUE };
124 String[] expected = new String[] { "1", "true" };
125 String[] actual = (String[]) morpher.morph( input );
126 ArrayAssertions.assertEquals( expected, actual );
127 }
128
129 public void testMorph_threedims()
130 {
131 Object[][][] input = new Object[][][] { { { new Integer( 1 ), Boolean.TRUE } },
132 { { new Character( 'A' ) } } };
133 String[][][] expected = new String[][][] { { { "1", "true" } }, { { "A" } } };
134 String[][][] actual = (String[][][]) morpher.morph( input );
135 ArrayAssertions.assertEquals( expected, actual );
136 }
137
138 public void testMorph_twodims()
139 {
140 Object[][] input = new Object[][] { { new Integer( 1 ), Boolean.TRUE },
141 { new Character( 'A' ) } };
142 String[][] expected = new String[][] { { "1", "true" }, { "A" } };
143 String[][] actual = (String[][]) morpher.morph( input );
144 ArrayAssertions.assertEquals( expected, actual );
145 }
146
147 public void testMorph_twodims_reflection()
148 {
149 Object input = Array.newInstance( Object.class, new int[] { 2, 2 } );
150 Object[] a = new Object[] { new Integer( 1 ), Boolean.TRUE };
151 Object[] b = new Object[] { new Character( 'A' ) };
152 Array.set( input, 0, a );
153 Array.set( input, 1, b );
154
155 String[][] expected = new String[][] { { "1", "true" }, { "A" } };
156 String[][] actual = (String[][]) morpher.morph( input );
157 ArrayAssertions.assertEquals( expected, actual );
158 }
159
160 public void testMorphsTo()
161 {
162 assertEquals( String[].class, getMorpher().morphsTo() );
163 }
164
165 public void testObjectArrayMorpher_illegalMorpher_noMorphMethod()
166 {
167 try{
168 morpher = new ObjectArrayMorpher( new Morpher(){
169 public Class morphsTo()
170 {
171 return Object.class;
172 }
173
174 public boolean supports( Class clazz )
175 {
176 return false;
177 }
178 } );
179 }
180 catch( IllegalArgumentException expected ){
181
182 }
183 }
184
185 public void testObjectArrayMorpher_illegalMorpher_nullMorpher()
186 {
187 try{
188 morpher = new ObjectArrayMorpher( null );
189 }
190 catch( IllegalArgumentException expected ){
191
192 }
193 }
194
195 public void testObjectArrayMorpher_illegalMorpher_supportsArray()
196 {
197 try{
198 morpher = new ObjectArrayMorpher( new Morpher(){
199 public Class morphsTo()
200 {
201 return Object[].class;
202 }
203
204 public boolean supports( Class clazz )
205 {
206 return false;
207 }
208 } );
209 }
210 catch( IllegalArgumentException expected ){
211
212 }
213 }
214
215 protected void setUp() throws Exception
216 {
217 morpher = new ObjectArrayMorpher( StringMorpher.getInstance() );
218 anotherMorpher = new ObjectArrayMorpher( IdentityObjectMorpher.getInstance() );
219 }
220 }