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.ArrayList;
20  import java.util.List;
21  
22  import junit.framework.Test;
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.test.ArrayAssertions;
28  
29  /**
30   * @author Andres Almiray <aalmiray@users.sourceforge.net>
31   */
32  public class ObjectListMorpherTest extends AbstractObjectMorpherTestCase
33  {
34     public static void main( String[] args )
35     {
36        TestRunner.run( suite() );
37     }
38  
39     public static Test suite()
40     {
41        TestSuite suite = new TestSuite( ObjectListMorpherTest.class );
42        suite.setName( "ObjectListMorpher Tests" );
43        return suite;
44     }
45  
46     private ObjectListMorpher anotherMorpher;
47     private ObjectListMorpher anotherMorpherWithDefaultValue;
48     private ObjectListMorpher morpher;
49     private ObjectListMorpher morpherWithDefaultValue;
50  
51     public ObjectListMorpherTest( String name )
52     {
53        super( name );
54     }
55  
56     // -----------------------------------------------------------------------
57  
58     public void testMorph_illegalArgument()
59     {
60        try{
61           // argument is not a list
62           morpher.morph( "" );
63        }
64        catch( MorphException expected ){
65           // ok
66        }
67     }
68  
69     public void testMorph_IntegerList()
70     {
71        List expected = new ArrayList();
72        expected.add( new Integer( 1 ) );
73        expected.add( new Integer( 2 ) );
74        expected.add( new Integer( 3 ) );
75        List actual = (List) morpher.morph( expected );
76        ArrayAssertions.assertEquals( expected, actual );
77     }
78  
79     public void testMorph_null()
80     {
81        assertNull( morpher.morph( null ) );
82     }
83  
84     public void testMorph_NullList()
85     {
86        List expected = new ArrayList();
87        expected.add( null );
88        expected.add( null );
89        expected.add( null );
90        List actual = (List) morpher.morph( expected );
91        ArrayAssertions.assertEquals( expected, actual );
92     }
93  
94     public void testMorph_NullList_withDefaultValue()
95     {
96        List expected = new ArrayList();
97        expected.add( new Integer( 0 ) );
98        expected.add( new Integer( 0 ) );
99        expected.add( new Integer( 0 ) );
100       List input = new ArrayList();
101       input.add( null );
102       input.add( null );
103       input.add( null );
104       List actual = (List) morpherWithDefaultValue.morph( input );
105       ArrayAssertions.assertEquals( expected, actual );
106    }
107 
108    public void testMorph_StringList()
109    {
110       List expected = new ArrayList();
111       expected.add( new Integer( 1 ) );
112       expected.add( new Integer( 2 ) );
113       expected.add( new Integer( 3 ) );
114       List input = new ArrayList();
115       input.add( "1" );
116       input.add( "2" );
117       input.add( "3" );
118       List actual = (List) morpher.morph( input );
119       ArrayAssertions.assertEquals( expected, actual );
120    }
121 
122    public void testObjectListMorpher_illegalMorpher_noMorphMethod()
123    {
124       try{
125          morpher = new ObjectListMorpher( new Morpher(){
126             public Class morphsTo()
127             {
128                return Object.class;
129             }
130 
131             public boolean supports( Class clazz )
132             {
133                return false;
134             }
135          } );
136       }
137       catch( IllegalArgumentException expected ){
138          // ok
139       }
140    }
141 
142    public void testObjectListMorpher_illegalMorpher_nullMorpher()
143    {
144       try{
145          morpher = new ObjectListMorpher( null );
146       }
147       catch( IllegalArgumentException expected ){
148          // ok
149       }
150    }
151 
152    public void testObjectListMorpher_illegalMorpher_supportsList()
153    {
154       try{
155          morpher = new ObjectListMorpher( new Morpher(){
156             public Class morphsTo()
157             {
158                return List.class;
159             }
160 
161             public boolean supports( Class clazz )
162             {
163                return false;
164             }
165          } );
166       }
167       catch( IllegalArgumentException expected ){
168          // ok
169       }
170    }
171 
172    protected Morpher getAnotherMorpher()
173    {
174       return anotherMorpher;
175    }
176 
177    protected Morpher getAnotherMorpherWithDefaultValue()
178    {
179       return anotherMorpherWithDefaultValue;
180    }
181 
182    protected Morpher getMorpher()
183    {
184       return morpher;
185    }
186 
187    protected Morpher getMorpherWithDefaultValue()
188    {
189       return morpherWithDefaultValue;
190    }
191 
192    protected void setUp() throws Exception
193    {
194       morpher = new ObjectListMorpher( new NumberMorpher( Integer.class ) );
195       morpherWithDefaultValue = new ObjectListMorpher( new NumberMorpher( Integer.class,
196             new Integer( 0 ) ), new Integer( 0 ) );
197       anotherMorpher = new ObjectListMorpher( new NumberMorpher( Integer.class ) );
198       anotherMorpherWithDefaultValue = new ObjectListMorpher( new NumberMorpher( Integer.class,
199             new Integer( 1 ) ), new Integer( 1 ) );
200    }
201 }