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 junit.framework.Test;
20  import junit.framework.TestSuite;
21  import junit.textui.TestRunner;
22  import net.sf.ezmorph.MorphException;
23  import net.sf.ezmorph.Morpher;
24  
25  /**
26   * @author Andres Almiray <aalmiray@users.sourceforge.net>
27   */
28  public class BooleanObjectMorpherTest extends AbstractObjectMorpherTestCase
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( BooleanObjectMorpherTest.class );
38        suite.setName( "BooleanObjectMorpher Tests" );
39        return suite;
40     }
41  
42     private BooleanObjectMorpher anotherMorpher;
43     private BooleanObjectMorpher anotherMorpherWithDefaultValue;
44     private BooleanObjectMorpher morpher;
45     private BooleanObjectMorpher morpherWithDefaultValue;
46  
47     public BooleanObjectMorpherTest( String name )
48     {
49        super( name );
50     }
51  
52     // -----------------------------------------------------------------------
53  
54     public void testBooleanMorph_noConversion()
55     {
56        Boolean actual = (Boolean) new BooleanObjectMorpher( Boolean.TRUE ).morph( Boolean.TRUE );
57        assertEquals( Boolean.TRUE, actual );
58     }
59  
60     public void testBooleanMorph_throwException()
61     {
62        try{
63           new BooleanObjectMorpher().morph( "A" );
64           fail( "Should have thrown an Exception" );
65        }
66        catch( MorphException expected ){
67           // ok
68        }
69     }
70  
71     public void testBooleanMorph_throwException_null()
72     {
73        try{
74           new BooleanObjectMorpher().morph( null );
75           fail( "Should have thrown an Exception" );
76        }
77        catch( MorphException expected ){
78           // ok
79        }
80     }
81  
82     public void testBooleanMorph_useDefault()
83     {
84        String expected = String.valueOf( "A" );
85        Boolean actual = (Boolean) new BooleanObjectMorpher( Boolean.TRUE ).morph( expected );
86        assertEquals( Boolean.TRUE, actual );
87     }
88  
89     public void testBooleanMorph_useDefault_null()
90     {
91        Boolean actual = (Boolean) new BooleanObjectMorpher( Boolean.TRUE ).morph( null );
92        assertEquals( Boolean.TRUE, actual );
93     }
94  
95     public void testBooleanMorphStringValues_false()
96     {
97        assertEquals( Boolean.FALSE, new BooleanObjectMorpher().morph( "false" ) );
98        assertEquals( Boolean.FALSE, new BooleanObjectMorpher().morph( "no" ) );
99        assertEquals( Boolean.FALSE, new BooleanObjectMorpher().morph( "off" ) );
100    }
101 
102    public void testBooleanMorphStringValues_true()
103    {
104       assertEquals( Boolean.TRUE, new BooleanObjectMorpher().morph( "true" ) );
105       assertEquals( Boolean.TRUE, new BooleanObjectMorpher().morph( "yes" ) );
106       assertEquals( Boolean.TRUE, new BooleanObjectMorpher().morph( "on" ) );
107    }
108 
109    protected Morpher getAnotherMorpher()
110    {
111       return anotherMorpher;
112    }
113 
114    protected Morpher getAnotherMorpherWithDefaultValue()
115    {
116       return anotherMorpherWithDefaultValue;
117    }
118 
119    protected Morpher getMorpher()
120    {
121       return morpher;
122    }
123 
124    protected Morpher getMorpherWithDefaultValue()
125    {
126       return morpherWithDefaultValue;
127    }
128 
129    protected void setUp() throws Exception
130    {
131       morpher = new BooleanObjectMorpher();
132       morpherWithDefaultValue = new BooleanObjectMorpher( Boolean.TRUE );
133       anotherMorpher = new BooleanObjectMorpher();
134       anotherMorpherWithDefaultValue = new BooleanObjectMorpher( Boolean.FALSE );
135    }
136 }