1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
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  
18  package org.apache.commons.beanutils;
19  
20  
21  import junit.framework.Test;
22  import junit.framework.TestSuite;
23  
24  
25  /**
26   * <p>Test Case for the <code>WrapDynaBean</code> implementation class.
27   * These tests were based on the ones in <code>PropertyUtilsTestCase</code>
28   * because the two classes provide similar levels of functionality.</p>
29   *
30   * @author Craig R. McClanahan
31   * @version $Revision: 1.9 $ $Date: 2004/02/28 13:18:36 $
32   */
33  
34  public class WrapDynaBeanTestCase extends BasicDynaBeanTestCase {
35  
36  
37      // ---------------------------------------------------- Instance Variables
38  
39  
40      // ---------------------------------------------------------- Constructors
41  
42  
43      /**
44       * Construct a new instance of this test case.
45       *
46       * @param name Name of the test case
47       */
48      public WrapDynaBeanTestCase(String name) {
49  
50          super(name);
51  
52      }
53  
54  
55      // -------------------------------------------------- Overall Test Methods
56  
57  
58      /**
59       * Set up instance variables required by this test case.
60       */
61      public void setUp() throws Exception {
62  
63          bean = new WrapDynaBean(new TestBean());
64  
65      }
66  
67  
68      /**
69       * Return the tests included in this test suite.
70       */
71      public static Test suite() {
72  
73          return (new TestSuite(WrapDynaBeanTestCase.class));
74  
75      }
76  
77  
78      /**
79       * Tear down instance variables required by this test case.
80       */
81      public void tearDown() {
82  
83          bean = null;
84  
85      }
86  
87  
88  
89      // ------------------------------------------------ Individual Test Methods
90  
91  
92      /**
93       * The <code>contains()</code> method is not supported by the
94       * <code>WrapDynaBean</code> implementation class.
95       */
96      public void testMappedContains() {
97  
98          try {
99              assertTrue("Can see first key",
100                     bean.contains("mappedProperty", "First Key"));
101             fail("Should have thrown UnsupportedOperationException");
102         } catch (UnsupportedOperationException t) {
103             ; // Expected result
104         } catch (Throwable t) {
105             fail("Exception: " + t);
106         }
107 
108 
109         try {
110             assertTrue("Can not see unknown key",
111                     !bean.contains("mappedProperty", "Unknown Key"));
112             fail("Should have thrown UnsupportedOperationException");
113         } catch (UnsupportedOperationException t) {
114             ; // Expected result
115         } catch (Throwable t) {
116             fail("Exception: " + t);
117         }
118 
119     }
120 
121 
122     /**
123      * The <code>remove()</code> method is not supported by the
124      * <code>WrapDynaBean</code> implementation class.
125      */
126     public void testMappedRemove() {
127 
128         try {
129             assertTrue("Can see first key",
130                     bean.contains("mappedProperty", "First Key"));
131             bean.remove("mappedProperty", "First Key");
132             fail("Should have thrown UnsupportedOperationException");
133             //            assertTrue("Can not see first key",
134             //         !bean.contains("mappedProperty", "First Key"));
135         } catch (UnsupportedOperationException t) {
136             ; // Expected result
137         } catch (Throwable t) {
138             fail("Exception: " + t);
139         }
140 
141         try {
142             assertTrue("Can not see unknown key",
143                     !bean.contains("mappedProperty", "Unknown Key"));
144             bean.remove("mappedProperty", "Unknown Key");
145             fail("Should have thrown UnsupportedOperationException");
146             //            assertTrue("Can not see unknown key",
147             //         !bean.contains("mappedProperty", "Unknown Key"));
148         } catch (UnsupportedOperationException t) {
149             ; // Expected result
150         } catch (Throwable t) {
151             fail("Exception: " + t);
152         }
153 
154     }
155 
156 
157     /**
158      * Suppress serialization and deserialization tests.  WrapDynaClass
159      * is not serializable.
160      */
161     public void testSerialization() { }
162     
163     /** Tests getInstance method */
164     public void testGetInstance() {
165         AlphaBean alphaBean = new AlphaBean("Now On Air... John Peel");
166         WrapDynaBean dynaBean = new WrapDynaBean(alphaBean);
167         Object wrappedInstance = dynaBean.getInstance();
168         assertTrue("Object type is AlphaBean", wrappedInstance instanceof AlphaBean);
169         AlphaBean wrappedAlphaBean = (AlphaBean) wrappedInstance;
170         assertTrue("Same Object", wrappedAlphaBean == alphaBean);
171     }
172 
173     /** Tests the newInstance implementation for WrapDynaClass */
174     public void testNewInstance() throws Exception {
175         WrapDynaClass dynaClass = WrapDynaClass.createDynaClass(AlphaBean.class);
176         Object createdInstance = dynaClass.newInstance();
177         assertTrue("Object type is WrapDynaBean", createdInstance instanceof WrapDynaBean);
178         WrapDynaBean dynaBean = (WrapDynaBean) createdInstance;
179         assertTrue("Object type is AlphaBean", dynaBean.getInstance() instanceof AlphaBean);
180     }
181 
182 }