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 BooleanObjectArrayMorpherTest 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( BooleanObjectArrayMorpherTest.class );
38        suite.setName( "BooleanObjectArrayMorpher Tests" );
39        return suite;
40     }
41  
42     private BooleanObjectArrayMorpher anotherMorpher;
43     private BooleanObjectArrayMorpher anotherMorpherWithDefaultValue;
44     private BooleanObjectArrayMorpher morpher;
45     private BooleanObjectArrayMorpher morpherWithDefaultValue;
46  
47     public BooleanObjectArrayMorpherTest( String name )
48     {
49        super( name );
50     }
51  
52     // -----------------------------------------------------------------------
53  
54     public void testMorph_booleanArray()
55     {
56        boolean[] expected = { true, false };
57        Boolean[] actual = (Boolean[]) morpher.morph( expected );
58        ArrayAssertions.assertEquals( expected, actual );
59     }
60  
61     public void testMorph_BooleanArray()
62     {
63        Boolean[] expected = { Boolean.TRUE, Boolean.FALSE };
64        Boolean[] actual = (Boolean[]) morpher.morph( expected );
65        ArrayAssertions.assertEquals( expected, actual );
66     }
67  
68     public void testMorph_booleanArray_threedims()
69     {
70        boolean[][][] expected = { { { true }, { false } }, { { true }, { false } } };
71        Boolean[][][] actual = (Boolean[][][]) morpher.morph( expected );
72        ArrayAssertions.assertEquals( expected, actual );
73     }
74  
75     public void testMorph_BooleanArray_threedims()
76     {
77        Boolean[][][] expected = { { { Boolean.TRUE }, { Boolean.FALSE } },
78              { { Boolean.TRUE }, { Boolean.FALSE } } };
79        Boolean[][][] actual = (Boolean[][][]) morpher.morph( expected );
80        ArrayAssertions.assertEquals( expected, actual );
81     }
82  
83     public void testMorph_booleanArray_twodims()
84     {
85        boolean[][] expected = { { true, false }, { true, false } };
86        Boolean[][] actual = (Boolean[][]) morpher.morph( expected );
87        ArrayAssertions.assertEquals( expected, actual );
88     }
89  
90     public void testMorph_BooleanArray_twodims()
91     {
92        Boolean[][] expected = { { Boolean.TRUE, Boolean.FALSE }, { Boolean.TRUE, Boolean.FALSE } };
93        Boolean[][] actual = (Boolean[][]) morpher.morph( expected );
94        ArrayAssertions.assertEquals( expected, actual );
95     }
96  
97     public void testMorph_illegalArgument()
98     {
99        try{
100          // argument is not an array
101          morpher.morph( "" );
102       }
103       catch( MorphException expected ){
104          // ok
105       }
106    }
107 
108    public void testMorph_null()
109    {
110       assertNull( morpher.morph( null ) );
111    }
112 
113    public void testMorph_StringArray_Boolean_default()
114    {
115       Boolean[] expected = { Boolean.TRUE, Boolean.TRUE };
116       morpher = new BooleanObjectArrayMorpher( Boolean.TRUE );
117       Boolean[] actual = (Boolean[]) morpher.morph( new String[] { "A", "B" } );
118       ArrayAssertions.assertEquals( expected, actual );
119    }
120 
121    public void testMorph_StringArray_null_default()
122    {
123       Boolean[] expected = { null, null };
124       morpher= new BooleanObjectArrayMorpher( null );
125       Boolean[] actual = (Boolean[]) morpher.morph( new String[] { "A", "B" } );
126       ArrayAssertions.assertEquals( expected, actual );
127    }
128 
129    public void testMorph_strings()
130    {
131       String[] expected = { "true", "yes", "on", "false", "no", "off" };
132       Boolean[] actual = (Boolean[]) morpher.morph( expected );
133       ArrayAssertions.assertEquals( new Boolean[] { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE,
134             Boolean.FALSE, Boolean.FALSE, Boolean.FALSE }, actual );
135    }
136 
137    public void testMorph_strings_twodims()
138    {
139       String[][] expected = { { "true", "yes", "on" }, { "false", "no", "off" } };
140       Boolean[][] actual = (Boolean[][]) morpher.morph( expected );
141       ArrayAssertions.assertEquals( new Boolean[][] { { Boolean.TRUE, Boolean.TRUE, Boolean.TRUE },
142             { Boolean.FALSE, Boolean.FALSE, Boolean.FALSE } }, actual );
143    }
144 
145    public void testMorph_throwException()
146    {
147       try{
148          new BooleanObjectArrayMorpher().morph( new String[] { "A" } );
149          fail( "Should have thrown an Exception" );
150       }
151       catch( MorphException expected ){
152          // ok
153       }
154    }
155 
156    protected AbstractArrayMorpher getAnotherMorpher()
157    {
158       return anotherMorpher;
159    }
160 
161    protected AbstractArrayMorpher getAnotherMorpherWithDefaultValue()
162    {
163       return anotherMorpherWithDefaultValue;
164    }
165 
166    protected AbstractArrayMorpher getMorpher()
167    {
168       return morpher;
169    }
170 
171    protected AbstractArrayMorpher getMorpherWithDefaultValue()
172    {
173       return morpherWithDefaultValue;
174    }
175 
176    protected Class getMorphsToClass()
177    {
178       return Boolean[].class;
179    }
180 
181    protected void setUp() throws Exception
182    {
183       morpher = new BooleanObjectArrayMorpher();
184       morpherWithDefaultValue = new BooleanObjectArrayMorpher( Boolean.TRUE );
185       anotherMorpher = new BooleanObjectArrayMorpher();
186       anotherMorpherWithDefaultValue = new BooleanObjectArrayMorpher( Boolean.FALSE );
187    }
188 }