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;
18  
19  import java.math.BigDecimal;
20  import java.math.BigInteger;
21  import java.util.Calendar;
22  import java.util.Date;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  import junit.framework.TestSuite;
29  import junit.textui.TestRunner;
30  import net.sf.ezmorph.object.DateMorpher;
31  import net.sf.ezmorph.object.IdentityObjectMorpher;
32  import net.sf.ezmorph.object.MapToDateMorpher;
33  import net.sf.ezmorph.object.StringMorpher;
34  import net.sf.ezmorph.primitive.BooleanMorpher;
35  import net.sf.ezmorph.primitive.IntMorpher;
36  import net.sf.ezmorph.test.ArrayAssertions;
37  
38  public class MorpherRegistryTest extends TestCase
39  {
40     public static void main( String[] args )
41     {
42        TestRunner.run( suite() );
43     }
44  
45     public static Test suite()
46     {
47        TestSuite suite = new TestSuite( MorpherRegistryTest.class );
48        suite.setName( "MorpherRegistry Tests" );
49        return suite;
50     }
51  
52     private MorpherRegistry morpherRegistry;
53  
54     public MorpherRegistryTest( String name )
55     {
56        super( name );
57     }
58  
59     // -----------------------------------------------------------------------
60  
61     public void testClear_Class()
62     {
63        MorphUtils.registerStandardMorphers( morpherRegistry );
64        Morpher expected = StringMorpher.getInstance();
65        Morpher actual = morpherRegistry.getMorpherFor( String.class );
66        assertEquals( expected, actual );
67        morpherRegistry.clear( String.class );
68        actual = morpherRegistry.getMorpherFor( String.class );
69        assertFalse( expected.equals( actual ) );
70     }
71  
72     public void testMorph_array_of_objects()
73     {
74        MorphUtils.registerStandardObjectArrayMorphers( morpherRegistry );
75  
76        ArrayAssertions.assertEquals( new Byte[] { Byte.valueOf( "0" ), Byte.valueOf( "1" ) },
77              (Byte[]) morpherRegistry.morph( Byte[].class, new String[] { "0", "1" } ) );
78        ArrayAssertions.assertEquals( new Short[] { Short.valueOf( "0" ), Short.valueOf( "1" ) },
79              (Short[]) morpherRegistry.morph( Short[].class, new String[] { "0", "1" } ) );
80        ArrayAssertions.assertEquals(
81              new Integer[] { Integer.valueOf( "0" ), Integer.valueOf( "1" ) },
82              (Integer[]) morpherRegistry.morph( Integer[].class, new String[] { "0", "1" } ) );
83        ArrayAssertions.assertEquals( new Long[] { Long.valueOf( "0" ), Long.valueOf( "1" ) },
84              (Long[]) morpherRegistry.morph( Long[].class, new String[] { "0", "1" } ) );
85        ArrayAssertions.assertEquals( new Float[] { Float.valueOf( "0" ), Float.valueOf( "1" ) },
86              (Float[]) morpherRegistry.morph( Float[].class, new String[] { "0", "1" } ) );
87        ArrayAssertions.assertEquals( new Double[] { Double.valueOf( "0" ), Double.valueOf( "1" ) },
88              (Double[]) morpherRegistry.morph( Double[].class, new String[] { "0", "1" } ) );
89        ArrayAssertions.assertEquals( new BigInteger[] { BigInteger.ZERO, BigInteger.ONE },
90              (BigInteger[]) morpherRegistry.morph( BigInteger[].class, new String[] { "0", "1" } ) );
91        ArrayAssertions.assertEquals( new BigDecimal[] { MorphUtils.BIGDECIMAL_ZERO,
92              MorphUtils.BIGDECIMAL_ONE }, (BigDecimal[]) morpherRegistry.morph( BigDecimal[].class,
93              new String[] { "0", "1" } ) );
94        ArrayAssertions.assertEquals( new Boolean[] { Boolean.FALSE },
95              (Boolean[]) morpherRegistry.morph( Boolean[].class, new String[] { "" } ) );
96        ArrayAssertions.assertEquals( new Character[] { new Character( '\0' ) },
97              (Character[]) morpherRegistry.morph( Character[].class, new String[] { "" } ) );
98        ArrayAssertions.assertEquals( new Class[] { Object.class, boolean[].class },
99              (Class[]) morpherRegistry.morph( Class[].class,
100                   new String[] { "java.lang.Object", "[Z" } ) );
101    }
102 
103    public void testMorph_array_of_objects__empty_string()
104    {
105       MorphUtils.registerStandardObjectArrayMorphers( morpherRegistry );
106 
107       ArrayAssertions.assertEquals( new Byte[] { null }, (Byte[]) morpherRegistry.morph(
108             Byte[].class, new String[] { "" } ) );
109       ArrayAssertions.assertEquals( new Short[] { null }, (Short[]) morpherRegistry.morph(
110             Short[].class, new String[] { "" } ) );
111       ArrayAssertions.assertEquals( new Integer[] { null }, (Integer[]) morpherRegistry.morph(
112             Integer[].class, new String[] { "" } ) );
113       ArrayAssertions.assertEquals( new Long[] { null }, (Long[]) morpherRegistry.morph(
114             Long[].class, new String[] { "" } ) );
115       ArrayAssertions.assertEquals( new Float[] { null }, (Float[]) morpherRegistry.morph(
116             Float[].class, new String[] { "" } ) );
117       ArrayAssertions.assertEquals( new Double[] { null }, (Double[]) morpherRegistry.morph(
118             Double[].class, new String[] { "" } ) );
119       ArrayAssertions.assertEquals( new BigInteger[] { null },
120             (BigInteger[]) morpherRegistry.morph( BigInteger[].class, new String[] { "" } ) );
121       ArrayAssertions.assertEquals( new BigDecimal[] { null },
122             (BigDecimal[]) morpherRegistry.morph( BigDecimal[].class, new String[] { "" } ) );
123    }
124 
125    public void testMorph_array_of_objects__null_string()
126    {
127       MorphUtils.registerStandardObjectArrayMorphers( morpherRegistry );
128 
129       ArrayAssertions.assertEquals( new Byte[] { null }, (Byte[]) morpherRegistry.morph(
130             Byte[].class, new String[] { "null" } ) );
131       ArrayAssertions.assertEquals( new Short[] { null }, (Short[]) morpherRegistry.morph(
132             Short[].class, new String[] { "null" } ) );
133       ArrayAssertions.assertEquals( new Integer[] { null }, (Integer[]) morpherRegistry.morph(
134             Integer[].class, new String[] { "null" } ) );
135       ArrayAssertions.assertEquals( new Long[] { null }, (Long[]) morpherRegistry.morph(
136             Long[].class, new String[] { "null" } ) );
137       ArrayAssertions.assertEquals( new Float[] { null }, (Float[]) morpherRegistry.morph(
138             Float[].class, new String[] { "null" } ) );
139       ArrayAssertions.assertEquals( new Double[] { null }, (Double[]) morpherRegistry.morph(
140             Double[].class, new String[] { "null" } ) );
141       ArrayAssertions.assertEquals( new BigInteger[] { null },
142             (BigInteger[]) morpherRegistry.morph( BigInteger[].class, new String[] { "null" } ) );
143       ArrayAssertions.assertEquals( new BigDecimal[] { null },
144             (BigDecimal[]) morpherRegistry.morph( BigDecimal[].class, new String[] { "null" } ) );
145    }
146 
147    public void testMorph_array_of_objects__numbers()
148    {
149       MorphUtils.registerStandardObjectArrayMorphers( morpherRegistry );
150 
151       ArrayAssertions.assertEquals( new Byte[] { new Byte( (byte) 0 ) },
152             (Byte[]) morpherRegistry.morph( Byte[].class, new String[] { "a" } ) );
153       ArrayAssertions.assertEquals( new Short[] { new Short( (short) 0 ) },
154             (Short[]) morpherRegistry.morph( Short[].class, new String[] { "a" } ) );
155       ArrayAssertions.assertEquals( new Integer[] { new Integer( 0 ) },
156             (Integer[]) morpherRegistry.morph( Integer[].class, new String[] { "a" } ) );
157       ArrayAssertions.assertEquals( new Long[] { new Long( 0 ) }, (Long[]) morpherRegistry.morph(
158             Long[].class, new String[] { "a" } ) );
159       ArrayAssertions.assertEquals( new Float[] { new Float( 0 ) },
160             (Float[]) morpherRegistry.morph( Float[].class, new String[] { "a" } ) );
161       ArrayAssertions.assertEquals( new Double[] { new Double( 0 ) },
162             (Double[]) morpherRegistry.morph( Double[].class, new String[] { "a" } ) );
163       ArrayAssertions.assertEquals( new BigInteger[] { BigInteger.ZERO },
164             (BigInteger[]) morpherRegistry.morph( BigInteger[].class, new String[] { "a" } ) );
165       ArrayAssertions.assertEquals( new BigDecimal[] { MorphUtils.BIGDECIMAL_ZERO },
166             (BigDecimal[]) morpherRegistry.morph( BigDecimal[].class, new String[] { "a" } ) );
167    }
168 
169    public void testMorph_array_of_primitives()
170    {
171       MorphUtils.registerStandardPrimitiveArrayMorphers( morpherRegistry );
172 
173       ArrayAssertions.assertEquals( new boolean[] { false }, (boolean[]) morpherRegistry.morph(
174             boolean[].class, new String[] { "" } ) );
175       ArrayAssertions.assertEquals( new char[] { '\0' }, (char[]) morpherRegistry.morph(
176             char[].class, new String[] { "" } ) );
177       ArrayAssertions.assertEquals( new byte[] { 0 }, (byte[]) morpherRegistry.morph( byte[].class,
178             new String[] { "" } ) );
179       ArrayAssertions.assertEquals( new short[] { 0 }, (short[]) morpherRegistry.morph(
180             short[].class, new String[] { "" } ) );
181       ArrayAssertions.assertEquals( new int[] { 0 }, (int[]) morpherRegistry.morph( int[].class,
182             new String[] { "" } ) );
183       ArrayAssertions.assertEquals( new long[] { 0 }, (long[]) morpherRegistry.morph( long[].class,
184             new String[] { "" } ) );
185       ArrayAssertions.assertEquals( new float[] { 0 }, (float[]) morpherRegistry.morph(
186             float[].class, new String[] { "" } ) );
187       ArrayAssertions.assertEquals( new double[] { 0 }, (double[]) morpherRegistry.morph(
188             double[].class, new String[] { "" } ) );
189    }
190 
191    public void testMorph_objects()
192    {
193       MorphUtils.registerStandardObjectMorphers( morpherRegistry );
194 
195       assertEquals( Boolean.FALSE, morpherRegistry.morph( Boolean.class, null ) );
196       assertEquals( new Character( '\0' ), morpherRegistry.morph( Character.class, null ) );
197       assertEquals( Object.class, morpherRegistry.morph( Class.class, "java.lang.Object" ) );
198    }
199 
200    public void testMorph_objects__empty_string()
201    {
202       MorphUtils.registerStandardObjectMorphers( morpherRegistry );
203 
204       assertNull( morpherRegistry.morph( Byte.class, "" ) );
205       assertNull( morpherRegistry.morph( Short.class, "" ) );
206       assertNull( morpherRegistry.morph( Integer.class, "" ) );
207       assertNull( morpherRegistry.morph( Long.class, "" ) );
208       assertNull( morpherRegistry.morph( Float.class, "" ) );
209       assertNull( morpherRegistry.morph( Double.class, "" ) );
210       assertNull( morpherRegistry.morph( BigInteger.class, "" ) );
211       assertNull( morpherRegistry.morph( BigDecimal.class, "" ) );
212       assertEquals( "", morpherRegistry.morph( String.class, "" ) );
213    }
214 
215    public void testMorph_objects__null()
216    {
217       MorphUtils.registerStandardObjectMorphers( morpherRegistry );
218 
219       assertEquals( (Byte) null, morpherRegistry.morph( Byte.class, null ) );
220       assertEquals( (Short) null, morpherRegistry.morph( Short.class, null ) );
221       assertEquals( (Integer) null, morpherRegistry.morph( Integer.class, null ) );
222       assertEquals( (Long) null, morpherRegistry.morph( Long.class, null ) );
223       assertEquals( (Float) null, morpherRegistry.morph( Float.class, null ) );
224       assertEquals( (Double) null, morpherRegistry.morph( Double.class, null ) );
225       assertEquals( (BigInteger) null, morpherRegistry.morph( BigInteger.class, null ) );
226       assertEquals( (BigDecimal) null, morpherRegistry.morph( BigDecimal.class, null ) );
227       assertEquals( (String) null, morpherRegistry.morph( String.class, null ) );
228       assertEquals( (Class) null, morpherRegistry.morph( Class.class, null ) );
229    }
230 
231    public void testMorph_objects__null_string()
232    {
233       MorphUtils.registerStandardObjectMorphers( morpherRegistry );
234 
235       assertNull( morpherRegistry.morph( Byte.class, "null" ) );
236       assertNull( morpherRegistry.morph( Short.class, "null" ) );
237       assertNull( morpherRegistry.morph( Integer.class, "null" ) );
238       assertNull( morpherRegistry.morph( Long.class, "null" ) );
239       assertNull( morpherRegistry.morph( Float.class, "null" ) );
240       assertNull( morpherRegistry.morph( Double.class, "null" ) );
241       assertNull( morpherRegistry.morph( BigInteger.class, "null" ) );
242       assertNull( morpherRegistry.morph( BigDecimal.class, "null" ) );
243       assertEquals( "null", morpherRegistry.morph( String.class, "null" ) );
244       assertNull( morpherRegistry.morph( Class.class, "null" ) );
245    }
246 
247    public void testMorph_primitives()
248    {
249       MorphUtils.registerStandardPrimitiveMorphers( morpherRegistry );
250 
251       assertEquals( Boolean.FALSE, morpherRegistry.morph( boolean.class, null ) );
252       assertEquals( new Character( '\0' ), morpherRegistry.morph( char.class, null ) );
253       assertEquals( new Byte( (byte) 0 ), morpherRegistry.morph( byte.class, null ) );
254       assertEquals( new Short( (short) 0 ), morpherRegistry.morph( short.class, null ) );
255       assertEquals( new Integer( 0 ), morpherRegistry.morph( int.class, null ) );
256       assertEquals( new Long( 0 ), morpherRegistry.morph( long.class, null ) );
257       assertEquals( new Float( 0 ), morpherRegistry.morph( float.class, null ) );
258       assertEquals( new Double( 0 ), morpherRegistry.morph( double.class, null ) );
259    }
260 
261    public void testMorph_severalMorphersForTargetClass()
262    {
263       Map map = new HashMap();
264       map.put( "year", new Integer( 2007 ) );
265       map.put( "month", new Integer( 5 ) );
266       map.put( "day", new Integer( 17 ) );
267       map.put( "hour", new Integer( 12 ) );
268       map.put( "minutes", new Integer( 13 ) );
269       map.put( "seconds", new Integer( 14 ) );
270       map.put( "milliseconds", new Integer( 150 ) );
271 
272       MorpherRegistry morpherRegistry = new MorpherRegistry();
273       morpherRegistry.registerMorpher( new DateMorpher( new String[] { "mm/dd/yyyy" } ) );
274       morpherRegistry.registerMorpher( new MapToDateMorpher() );
275       Date date = (Date) morpherRegistry.morph( Date.class, map );
276       assertNotNull( date );
277       Calendar c = Calendar.getInstance();
278       c.setTime( date );
279       assertEquals( 2007, c.get( Calendar.YEAR ) );
280       assertEquals( 5, c.get( Calendar.MONTH ) );
281       assertEquals( 17, c.get( Calendar.DATE ) );
282       assertEquals( 12, c.get( Calendar.HOUR_OF_DAY ) );
283       assertEquals( 13, c.get( Calendar.MINUTE ) );
284       assertEquals( 14, c.get( Calendar.SECOND ) );
285       assertEquals( 150, c.get( Calendar.MILLISECOND ) );
286    }
287 
288    public void testRegistry()
289    {
290       Morpher morpher = new BooleanMorpher();
291       morpherRegistry.registerMorpher( morpher );
292       assertEquals( morpher, morpherRegistry.getMorpherFor( boolean.class ) );
293       morpherRegistry.deregisterMorpher( morpher );
294       assertTrue( !morpher.equals( morpherRegistry.getMorpherFor( boolean.class ) ) );
295       assertEquals( IdentityObjectMorpher.getInstance(),
296             morpherRegistry.getMorpherFor( boolean.class ) );
297    }
298 
299    public void testRegistry_morphers_for_unknown_target_class()
300    {
301       Morpher[] morphers = morpherRegistry.getMorphersFor( int.class );
302       assertEquals( 1, morphers.length );
303       assertSame( IdentityObjectMorpher.getInstance(), morphers[0] );
304    }
305 
306    public void testRegistry_standardMorphers()
307    {
308       MorphUtils.registerStandardMorphers( morpherRegistry );
309       Morpher morpher = new IntMorpher( 1 );
310       morpherRegistry.registerMorpher( morpher );
311       Morpher[] morphers = morpherRegistry.getMorphersFor( int.class );
312 
313       assertEquals( 2, morphers.length );
314       assertEquals( 0, ((IntMorpher) morphers[0]).getDefaultValue() );
315       assertSame( morpher, morphers[1] );
316    }
317 
318    public void testRegistry_wacky_morpher()
319    {
320       morpherRegistry.registerMorpher( new Morpher(){
321          // no mroph() method
322          public Class morphsTo()
323          {
324             return String.class;
325          }
326 
327          public boolean supports( Class clazz )
328          {
329             return true;
330          }
331       } );
332 
333       try{
334          morpherRegistry.morph( String.class, null );
335          fail( "Expected a MorphException" );
336       }
337       catch( MorphException expected ){
338          // OK
339       }
340    }
341 
342    protected void setUp() throws Exception
343    {
344       morpherRegistry = new MorpherRegistry();
345    }
346 }