1   /*
2    * Copyright 2006-2007-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.bean;
18  
19  import java.math.BigDecimal;
20  import java.math.BigInteger;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import junit.framework.Test;
26  import junit.framework.TestCase;
27  import junit.framework.TestSuite;
28  import junit.textui.TestRunner;
29  import net.sf.ezmorph.MorphException;
30  
31  /**
32   * @author Andres Almiray <aalmiray@users.sourceforge.net>
33   */
34  public class MorphDynaClassTest extends TestCase
35  {
36     public static void main( String[] args )
37     {
38        TestRunner.run( suite() );
39     }
40  
41     public static Test suite()
42     {
43        TestSuite suite = new TestSuite( MorphDynaClassTest.class );
44        suite.setName( "MorphDynaClass Tests" );
45        return suite;
46     }
47  
48     public void testConstructor_emptyAttributes()
49     {
50        MorphDynaClass dynaClass = new MorphDynaClass( null );
51        assertEquals( 0, dynaClass.getDynaProperties().length );
52        dynaClass = new MorphDynaClass( new HashMap() );
53        assertEquals( 0, dynaClass.getDynaProperties().length );
54     }
55  
56     public void testConstructor_emptyAttributes_throwException()
57     {
58        try{
59           new MorphDynaClass( null, true );
60           fail( "Expected a MorphException" );
61        }
62        catch( MorphException exception ){
63           // ok
64        }
65  
66        try{
67           new MorphDynaClass( new HashMap(), true );
68           fail( "Expected a MorphException" );
69        }
70        catch( MorphException exception ){
71           // ok
72        }
73     }
74  
75     public void testEquals()
76     {
77        Map properties = new HashMap();
78        properties.put( "byte", Byte.class );
79        Map props = new HashMap();
80        props.put( "byte", byte.class );
81        MorphDynaClass class1 = new MorphDynaClass( properties );
82        MorphDynaClass class2 = new MorphDynaClass( properties );
83        MorphDynaClass class3 = new MorphDynaClass( props );
84  
85        assertFalse( class1.equals( null ) );
86        assertTrue( class1.equals( class1 ) );
87        assertTrue( class1.equals( class2 ) );
88        assertFalse( class1.equals( new Object() ) );
89        assertFalse( class1.equals( class3 ) );
90     }
91  
92     public void testGetDynaProperty_null()
93     {
94        try{
95           Map properties = new HashMap();
96           properties.put( "obj", Object.class.getName() );
97           MorphDynaClass dynaClass = new MorphDynaClass( properties );
98           dynaClass.getDynaProperty( null );
99           fail( "Expected a MorphException" );
100       }
101       catch( MorphException exception ){
102          // ok
103       }
104    }
105 
106    public void testHashcode()
107    {
108       Map properties = new HashMap();
109       properties.put( "byte", Byte.class );
110       Map props = new HashMap();
111       props.put( "byte", byte.class );
112       MorphDynaClass class1 = new MorphDynaClass( properties );
113       MorphDynaClass class2 = new MorphDynaClass( properties );
114       MorphDynaClass class3 = new MorphDynaClass( props );
115 
116       assertEquals( class1.hashCode(), class1.hashCode() );
117       assertEquals( class1.hashCode(), class2.hashCode() );
118       assertTrue( class1.hashCode() != class3.hashCode() );
119    }
120 
121    public void testMiscelaneousClasses()
122    {
123       Map properties = new HashMap();
124       properties.put( "byte", Byte.class );
125       properties.put( "short", Short.class );
126       properties.put( "int", Integer.class );
127       properties.put( "long", Long.class );
128       properties.put( "float", Float.class );
129       properties.put( "double", Double.class );
130       properties.put( "bi", BigInteger.class );
131       properties.put( "bd", BigDecimal.class );
132       properties.put( "boolean", Boolean.class );
133       properties.put( "char", Character.class );
134       properties.put( "map", Map.class );
135       properties.put( "strs", String[].class );
136       properties.put( "list", List.class );
137       new MorphDynaClass( properties );
138    }
139 
140    public void testMultidimensionalArrayClass_Class()
141    {
142       try{
143          Map properties = new HashMap();
144          properties.put( "array", Object[][].class );
145          new MorphDynaClass( properties );
146          fail( "Expected a MorphException" );
147       }
148       catch( MorphException exception ){
149          // ok
150       }
151    }
152 
153    public void testMultidimensionalArrayClass_String()
154    {
155       try{
156          Map properties = new HashMap();
157          properties.put( "array", Object[][].class.getName() );
158          new MorphDynaClass( properties );
159          fail( "Expected a MorphException" );
160       }
161       catch( MorphException exception ){
162          // ok
163       }
164    }
165 }