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 BooleanArrayMorpherTest 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( BooleanArrayMorpherTest.class );
38        suite.setName( "BooleanArrayMorpher Tests" );
39        return suite;
40     }
41  
42     private BooleanArrayMorpher anotherMorpher;
43     private BooleanArrayMorpher anotherMorpherWithDefaultValue;
44     private BooleanArrayMorpher morpher;
45     private BooleanArrayMorpher morpherWithDefaultValue;
46  
47     public BooleanArrayMorpherTest( 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_threedims()
62     {
63        boolean[][][] expected = { { { true }, { false } }, { { true }, { false } } };
64        boolean[][][] actual = (boolean[][][]) morpher.morph( expected );
65        ArrayAssertions.assertEquals( expected, actual );
66     }
67  
68     public void testMorph_booleanArray_twodims()
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_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 = { "true", "yes", "on", "false", "no", "off" };
94        boolean[] actual = (boolean[]) morpher.morph( expected );
95        ArrayAssertions.assertEquals( new boolean[] { true, true, true, false, false, false }, actual );
96     }
97  
98     public void testMorph_strings_twodims()
99     {
100       String[][] expected = { { "true", "yes", "on" }, { "false", "no", "off" } };
101       boolean[][] actual = (boolean[][]) morpher.morph( expected );
102       ArrayAssertions.assertEquals(
103             new boolean[][] { { true, true, true }, { false, false, false } }, actual );
104    }
105 
106    public void testMorph_throwException()
107    {
108       try{
109          new BooleanArrayMorpher().morph( new String[] { null } );
110          fail( "Should have thrown an Exception" );
111       }
112       catch( MorphException expected ){
113          // ok
114       }
115    }
116 
117    protected AbstractArrayMorpher getAnotherMorpher()
118    {
119       return anotherMorpher;
120    }
121 
122    protected AbstractArrayMorpher getAnotherMorpherWithDefaultValue()
123    {
124       return anotherMorpherWithDefaultValue;
125    }
126 
127    protected AbstractArrayMorpher getMorpher()
128    {
129       return morpher;
130    }
131 
132    protected AbstractArrayMorpher getMorpherWithDefaultValue()
133    {
134       return morpherWithDefaultValue;
135    }
136 
137    protected Class getMorphsToClass()
138    {
139       return boolean[].class;
140    }
141 
142    protected void setUp() throws Exception
143    {
144       morpher = new BooleanArrayMorpher();
145       morpherWithDefaultValue = new BooleanArrayMorpher( true );
146       anotherMorpher = new BooleanArrayMorpher();
147       anotherMorpherWithDefaultValue = new BooleanArrayMorpher( false );
148    }
149 }