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 java.util.HashMap;
20  import java.util.Map;
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  import junit.textui.TestRunner;
26  import net.sf.ezmorph.MorphUtils;
27  import net.sf.ezmorph.MorpherRegistry;
28  import net.sf.ezmorph.bean.BeanMorpher;
29  import net.sf.ezmorph.object.sample.BeanA;
30  import net.sf.ezmorph.object.sample.BeanB;
31  import net.sf.ezmorph.object.sample.WrapperA;
32  import net.sf.ezmorph.object.sample.WrapperB;
33  
34  /**
35   * @author Andres Almiray <aalmiray@users.sourceforge.net>
36   */
37  public class SwitchingMorpherTest extends TestCase
38  {
39     public static void main( String[] args )
40     {
41        TestRunner.run( suite() );
42     }
43  
44     public static Test suite()
45     {
46        TestSuite suite = new TestSuite( SwitchingMorpherTest.class );
47        suite.setName( "SwitchingMorpher Tests" );
48        return suite;
49     }
50  
51     private SwitchingMorpher morpher;
52  
53     public SwitchingMorpherTest( String name )
54     {
55        super( name );
56     }
57  
58     // -----------------------------------------------------------------------
59  
60     public void testMorphWrapperAToBeanA()
61     {
62        WrapperA wrapper = new WrapperA();
63        wrapper.setInteger( "12" );
64        BeanA actual = (BeanA) morpher.morph( wrapper );
65        BeanA expected = new BeanA();
66        expected.setInteger( 12 );
67        assertEquals( expected, actual );
68     }
69  
70     public void testMorphWrapperBToBeanB()
71     {
72        WrapperB wrapper = new WrapperB();
73        wrapper.setBool( "false" );
74        BeanB actual = (BeanB) morpher.morph( wrapper );
75        BeanB expected = new BeanB();
76        expected.setBool( false );
77        assertEquals( expected, actual );
78     }
79  
80     public void testMorph_null()
81     {
82        assertNull( morpher.morph( null ) );
83     }
84  
85     protected void setUp() throws Exception
86     {
87        Map classMap = new HashMap();
88        classMap.put( WrapperA.class, BeanA.class );
89        classMap.put( WrapperB.class, BeanB.class );
90        MorpherRegistry morpherRegistry = new MorpherRegistry();
91        MorphUtils.registerStandardMorphers( morpherRegistry );
92        morpherRegistry.registerMorpher( new BeanMorpher( BeanA.class, morpherRegistry ) );
93        morpherRegistry.registerMorpher( new BeanMorpher( BeanB.class, morpherRegistry ) );
94        morpher = new SwitchingMorpher( classMap, morpherRegistry );
95     }
96  }