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.array;
18  
19  import junit.framework.Test;
20  import junit.framework.TestSuite;
21  import junit.textui.TestRunner;
22  import net.sf.ezmorph.MorphException;
23  import net.sf.ezmorph.test.ArrayAssertions;
24  
25  /**
26   * @author Andres Almiray <aalmiray@users.sourceforge.net>
27   */
28  public class CharacterObjectArrayMorpherTest extends AbstractArrayMorpherTestCase
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( CharacterObjectArrayMorpherTest.class );
38        suite.setName( "CharacterObjectArrayMorpher Tests" );
39        return suite;
40     }
41  
42     private CharacterObjectArrayMorpher anotherMorpher;
43     private CharacterObjectArrayMorpher anotherMorpherWithDefaultValue;
44     private CharacterObjectArrayMorpher morpher;
45     private CharacterObjectArrayMorpher morpherWithDefaultValue;
46  
47     public CharacterObjectArrayMorpherTest( String name )
48     {
49        super( name );
50     }
51  
52     // -----------------------------------------------------------------------
53  
54     public void testMorph_CharacterArray()
55     {
56        Character[] expected = { new Character( 'A' ), new Character( 'B' ) };
57        Character[] actual = (Character[]) morpher.morph( expected );
58        ArrayAssertions.assertEquals( expected, actual );
59     }
60  
61     public void testMorph_CharacterArray_threedims()
62     {
63        Character[][][] expected = { { { new Character( 'A' ) }, { new Character( 'B' ) } },
64              { { new Character( 'A' ) }, { new Character( 'B' ) } } };
65        Character[][][] actual = (Character[][][]) morpher.morph( expected );
66        ArrayAssertions.assertEquals( expected, actual );
67     }
68  
69     public void testMorph_CharacterArray_twodims()
70     {
71        Character[][] expected = { { new Character( 'A' ), new Character( 'B' ) },
72              { new Character( 'A' ), new Character( 'B' ) } };
73        Character[][] actual = (Character[][]) morpher.morph( expected );
74        ArrayAssertions.assertEquals( expected, actual );
75     }
76  
77     public void testMorph_charArray()
78     {
79        char[] input = { 'A', 'B' };
80        Character[] actual = (Character[]) morpher.morph( input );
81        ArrayAssertions.assertEquals( new Character[] { new Character( 'A' ), new Character( 'B' ) },
82              actual );
83     }
84  
85     public void testMorph_charArray_threedims()
86     {
87        char[][][] input = { { { 'A' } }, { { 'B' } } };
88        Character[][][] actual = (Character[][][]) morpher.morph( input );
89        ArrayAssertions.assertEquals( new Character[][][] { { { new Character( 'A' ) } },
90              { { new Character( 'B' ) } } }, actual );
91     }
92  
93     public void testMorph_charArray_twodims()
94     {
95        char[][] input = { { 'A' }, { 'B' } };
96        Character[][] actual = (Character[][]) morpher.morph( input );
97        ArrayAssertions.assertEquals( new Character[][] { { new Character( 'A' ) },
98              { new Character( 'B' ) } }, actual );
99     }
100 
101    public void testMorph_illegalArgument()
102    {
103       try{
104          // argument is not an array
105          morpher.morph( "" );
106       }
107       catch( MorphException expected ){
108          // ok
109       }
110    }
111 
112    public void testMorph_IntegerArray_default()
113    {
114       Character[] expected = { new Character( 'A' ), new Character( 'A' ) };
115       morpher = new CharacterObjectArrayMorpher( new Character( 'A' ) );
116       Character[] actual = (Character[]) morpher.morph( new Integer[] { null, null } );
117       ArrayAssertions.assertEquals( expected, actual );
118    }
119 
120    public void testMorph_null()
121    {
122       assertNull( morpher.morph( null ) );
123    }
124 
125    public void testMorph_StringArray_null_default()
126    {
127       Character[] expected = { null, null };
128       morpher = new CharacterObjectArrayMorpher( null );
129       Character[] actual = (Character[]) morpher.morph( new String[] { "A", "B" } );
130       ArrayAssertions.assertEquals( expected, actual );
131    }
132 
133    public void testMorph_strings()
134    {
135       String[] expected = { "A", "B" };
136       Character[] actual = (Character[]) morpher.morph( expected );
137       ArrayAssertions.assertEquals( new Character[] { new Character( 'A' ), new Character( 'B' ) },
138             actual );
139    }
140 
141    public void testMorph_strings_twodims()
142    {
143       String[][] expected = { { "A" }, { "B" } };
144       Character[][] actual = (Character[][]) morpher.morph( expected );
145       ArrayAssertions.assertEquals( new Character[][] { { new Character( 'A' ) },
146             { new Character( 'B' ) } }, actual );
147    }
148 
149    protected AbstractArrayMorpher getAnotherMorpher()
150    {
151       return anotherMorpher;
152    }
153 
154    protected AbstractArrayMorpher getAnotherMorpherWithDefaultValue()
155    {
156       return anotherMorpherWithDefaultValue;
157    }
158 
159    protected AbstractArrayMorpher getMorpher()
160    {
161       return morpher;
162    }
163 
164    protected AbstractArrayMorpher getMorpherWithDefaultValue()
165    {
166       return morpherWithDefaultValue;
167    }
168 
169    protected Class getMorphsToClass()
170    {
171       return Character[].class;
172    }
173 
174    protected void setUp() throws Exception
175    {
176       morpher = new CharacterObjectArrayMorpher();
177       morpherWithDefaultValue = new CharacterObjectArrayMorpher( new Character( 'A' ) );
178       anotherMorpher = new CharacterObjectArrayMorpher();
179       anotherMorpherWithDefaultValue = new CharacterObjectArrayMorpher( new Character( 'B' ) );
180    }
181 }