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 FloatArrayMorpherTest 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( FloatArrayMorpherTest.class );
38        suite.setName( "FloatArrayMorpher Tests" );
39        return suite;
40     }
41  
42     private FloatArrayMorpher anotherMorpher;
43     private FloatArrayMorpher anotherMorpherWithDefaultValue;
44     private FloatArrayMorpher morpher;
45     private FloatArrayMorpher morpherWithDefaultValue;
46  
47     public FloatArrayMorpherTest( String name )
48     {
49        super( name );
50     }
51  
52     // -----------------------------------------------------------------------
53  
54     public void testMorph_floatArray()
55     {
56        float[] expected = { 1f, 2f, 3f };
57        float[] actual = (float[]) morpher.morph( expected );
58        ArrayAssertions.assertEquals( expected, actual );
59     }
60  
61     public void testMorph_floatArray_threedims()
62     {
63        float[][][] expected = { { { 1 }, { 2 } }, { { 3 }, { 4 } } };
64        float[][][] actual = (float[][][]) morpher.morph( expected );
65        ArrayAssertions.assertEquals( expected, actual );
66     }
67  
68     public void testMorph_floatArray_twodims()
69     {
70        float[][] expected = { { 1, 2, 3 }, { 4, 5, 6 } };
71        float[][] actual = (float[][]) morpher.morph( expected );
72        ArrayAssertions.assertEquals( expected, actual );
73     }
74  
75     public void testMorph_illegalArgument()
76     {
77        try{
78           // argument is not an array
79           morpher.morph( "" );
80        }
81        catch( MorphException expected ){
82           // ok
83        }
84     }
85  
86     public void testMorph_null()
87     {
88        assertNull( morpher.morph( null ) );
89     }
90  
91     public void testMorph_strings()
92     {
93        String[] expected = { "1", "2", "3.3" };
94        float[] actual = (float[]) morpher.morph( expected );
95        ArrayAssertions.assertEquals( new float[] { 1f, 2f, 3.3f }, actual );
96     }
97  
98     public void testMorph_strings_twodims()
99     {
100       String[][] expected = { { "1", "2", "3.3" }, { "4", "5", "6.6" } };
101       float[][] actual = (float[][]) morpher.morph( expected );
102       ArrayAssertions.assertEquals( new float[][] { { 1f, 2f, 3.3f }, { 4f, 5f, 6.6f } }, actual );
103    }
104 
105    public void testMorph_throwException()
106    {
107       try{
108          new FloatArrayMorpher().morph( new String[] { null } );
109          fail( "Should have thrown an Exception" );
110       }
111       catch( MorphException expected ){
112          // ok
113       }
114    }
115 
116    protected AbstractArrayMorpher getAnotherMorpher()
117    {
118       return anotherMorpher;
119    }
120 
121    protected AbstractArrayMorpher getAnotherMorpherWithDefaultValue()
122    {
123       return anotherMorpherWithDefaultValue;
124    }
125 
126    protected AbstractArrayMorpher getMorpher()
127    {
128       return morpher;
129    }
130 
131    protected AbstractArrayMorpher getMorpherWithDefaultValue()
132    {
133       return morpherWithDefaultValue;
134    }
135 
136    protected Class getMorphsToClass()
137    {
138       return float[].class;
139    }
140 
141    protected void setUp() throws Exception
142    {
143       morpher = new FloatArrayMorpher();
144       morpherWithDefaultValue = new FloatArrayMorpher( 0 );
145       anotherMorpher = new FloatArrayMorpher();
146       anotherMorpherWithDefaultValue = new FloatArrayMorpher( 1 );
147    }
148 }