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.primitive;
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  import junit.textui.TestRunner;
23  import net.sf.ezmorph.Morpher;
24  
25  /**
26   * @author Andres Almiray <aalmiray@users.sourceforge.net>
27   */
28  public abstract class AbstractMorpherTestCase extends TestCase
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( AbstractMorpherTestCase.class );
38        suite.setName( "AbstractMorpher Tests" );
39        return suite;
40     }
41  
42     public AbstractMorpherTestCase( String name )
43     {
44        super( name );
45     }
46  
47     // -----------------------------------------------------------------------
48  
49     public void testEquals_another_Morpher()
50     {
51        assertFalse( getMorpherWithDefaultValue().equals( getAnotherMorpherWithDefaultValue() ) );
52        assertTrue( getMorpher().equals( getAnotherMorpher() ) );
53     }
54  
55     public void testEquals_different_morpher()
56     {
57        assertFalse( getMorpher().equals( new Morpher(){
58           public Class morphsTo()
59           {
60              return null;
61           }
62  
63           public boolean supports( Class clazz )
64           {
65              return false;
66           }
67        } ) );
68     }
69  
70     public void testEquals_morpher_withDefaultValue()
71     {
72        assertFalse( getMorpher().equals( getMorpherWithDefaultValue() ) );
73     }
74  
75     public void testEquals_null()
76     {
77        assertFalse( getMorpher().equals( null ) );
78     }
79  
80     public void testEquals_same_morpher()
81     {
82        assertTrue( getMorpher().equals( getMorpher() ) );
83        assertTrue( getMorpherWithDefaultValue().equals( getMorpherWithDefaultValue() ) );
84     }
85  
86     public void testHashCode_morpher_withDefaultValue()
87     {
88        assertTrue( getMorpher().hashCode() != getMorpherWithDefaultValue().hashCode() );
89     }
90  
91     public void testHashCode_same_morpher()
92     {
93        assertEquals( getMorpher().hashCode(), getMorpher().hashCode() );
94        assertEquals( getMorpherWithDefaultValue().hashCode(),
95              getMorpherWithDefaultValue().hashCode() );
96     }
97  
98     public void testMorphsTo()
99     {
100       assertEquals( getMorphsToClass(), getMorpher().morphsTo() );
101    }
102 
103    public void testSupports()
104    {
105       assertTrue( getMorpher().supports( Object.class ) );
106       assertTrue( getMorpher().supports( Number.class ) );
107       assertTrue( getMorpher().supports( String.class ) );
108       assertFalse( getMorpher().supports( Object[].class ) );
109    }
110 
111    // -----------------------------------------------------------------------
112 
113    protected abstract Morpher getAnotherMorpher();
114 
115    protected abstract Morpher getAnotherMorpherWithDefaultValue();
116 
117    protected abstract Morpher getMorpher();
118 
119    protected abstract Morpher getMorpherWithDefaultValue();
120 
121    protected abstract Class getMorphsToClass();
122 }