001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: /**
028: * <p>Test Case for the <code>WrapDynaBean</code> implementation class.
029: * These tests were based on the ones in <code>PropertyUtilsTestCase</code>
030: * because the two classes provide similar levels of functionality.</p>
031: *
032: * @author Craig R. McClanahan
033: * @version $Revision: 469737 $ $Date: 2006-11-01 01:16:55 +0000 (Wed, 01 Nov 2006) $
034: */
035:
036: public class WrapDynaBeanTestCase extends BasicDynaBeanTestCase {
037:
038: // ---------------------------------------------------- Instance Variables
039:
040: // ---------------------------------------------------------- Constructors
041:
042: /**
043: * Construct a new instance of this test case.
044: *
045: * @param name Name of the test case
046: */
047: public WrapDynaBeanTestCase(String name) {
048:
049: super (name);
050:
051: }
052:
053: // -------------------------------------------------- Overall Test Methods
054:
055: /**
056: * Set up instance variables required by this test case.
057: */
058: public void setUp() throws Exception {
059:
060: bean = new WrapDynaBean(new TestBean());
061:
062: }
063:
064: /**
065: * Return the tests included in this test suite.
066: */
067: public static Test suite() {
068:
069: return (new TestSuite(WrapDynaBeanTestCase.class));
070:
071: }
072:
073: /**
074: * Tear down instance variables required by this test case.
075: */
076: public void tearDown() {
077:
078: bean = null;
079:
080: }
081:
082: // ------------------------------------------------ Individual Test Methods
083:
084: /**
085: * The <code>set()</code> method.
086: */
087: public void testSimpleProperties() {
088:
089: // Invalid getter
090: try {
091: Object result = bean.get("invalidProperty");
092: fail("Invalid get should have thrown IllegalArgumentException");
093: } catch (IllegalArgumentException t) {
094: ; // Expected result
095: }
096:
097: // Invalid setter
098: try {
099: bean.set("invalidProperty", "XYZ");
100: fail("Invalid set should have thrown IllegalArgumentException");
101: } catch (IllegalArgumentException t) {
102: ; // Expected result
103: }
104:
105: // Set up initial Value
106: String testValue = "Original Value";
107: String testProperty = "stringProperty";
108: TestBean instance = (TestBean) ((WrapDynaBean) bean)
109: .getInstance();
110: instance.setStringProperty(testValue);
111: assertEquals("Check String property", testValue, instance
112: .getStringProperty());
113:
114: // Test Valid Get & Set
115: try {
116: testValue = "Some new value";
117: bean.set(testProperty, testValue);
118: assertEquals("Test Set", testValue, instance
119: .getStringProperty());
120: assertEquals("Test Get", testValue, bean.get(testProperty));
121: } catch (IllegalArgumentException t) {
122: fail("Get threw exception: " + t);
123: }
124:
125: }
126:
127: /**
128: * The <code>set()</code> method.
129: */
130: public void testIndexedProperties() {
131:
132: // Invalid getter
133: try {
134: Object result = bean.get("invalidProperty", 0);
135: fail("Invalid get should have thrown IllegalArgumentException");
136: } catch (IllegalArgumentException t) {
137: ; // Expected result
138: }
139:
140: // Invalid setter
141: try {
142: bean.set("invalidProperty", 0, "XYZ");
143: fail("Invalid set should have thrown IllegalArgumentException");
144: } catch (IllegalArgumentException t) {
145: ; // Expected result
146: }
147:
148: // Set up initial Value
149: String testValue = "Original Value";
150: String testProperty = "stringIndexed";
151: TestBean instance = (TestBean) ((WrapDynaBean) bean)
152: .getInstance();
153: instance.setStringIndexed(0, testValue);
154: assertEquals("Check String property", testValue, instance
155: .getStringIndexed(0));
156:
157: // Test Valid Get & Set
158: try {
159: testValue = "Some new value";
160: bean.set(testProperty, 0, testValue);
161: assertEquals("Test Set", testValue, instance
162: .getStringIndexed(0));
163: assertEquals("Test Get", testValue, bean.get(testProperty,
164: 0));
165: } catch (IllegalArgumentException t) {
166: fail("Get threw exception: " + t);
167: }
168:
169: }
170:
171: /**
172: * The <code>contains()</code> method is not supported by the
173: * <code>WrapDynaBean</code> implementation class.
174: */
175: public void testMappedContains() {
176:
177: try {
178: assertTrue("Can see first key", bean.contains(
179: "mappedProperty", "First Key"));
180: fail("Should have thrown UnsupportedOperationException");
181: } catch (UnsupportedOperationException t) {
182: // Expected result
183: } catch (Throwable t) {
184: fail("Exception: " + t);
185: }
186:
187: try {
188: assertTrue("Can not see unknown key", !bean.contains(
189: "mappedProperty", "Unknown Key"));
190: fail("Should have thrown UnsupportedOperationException");
191: } catch (UnsupportedOperationException t) {
192: // Expected result
193: } catch (Throwable t) {
194: fail("Exception: " + t);
195: }
196:
197: }
198:
199: /**
200: * The <code>remove()</code> method is not supported by the
201: * <code>WrapDynaBean</code> implementation class.
202: */
203: public void testMappedRemove() {
204:
205: try {
206: assertTrue("Can see first key", bean.contains(
207: "mappedProperty", "First Key"));
208: bean.remove("mappedProperty", "First Key");
209: fail("Should have thrown UnsupportedOperationException");
210: // assertTrue("Can not see first key",
211: // !bean.contains("mappedProperty", "First Key"));
212: } catch (UnsupportedOperationException t) {
213: // Expected result
214: } catch (Throwable t) {
215: fail("Exception: " + t);
216: }
217:
218: try {
219: assertTrue("Can not see unknown key", !bean.contains(
220: "mappedProperty", "Unknown Key"));
221: bean.remove("mappedProperty", "Unknown Key");
222: fail("Should have thrown UnsupportedOperationException");
223: // assertTrue("Can not see unknown key",
224: // !bean.contains("mappedProperty", "Unknown Key"));
225: } catch (UnsupportedOperationException t) {
226: // Expected result
227: } catch (Throwable t) {
228: fail("Exception: " + t);
229: }
230:
231: }
232:
233: /** Tests getInstance method */
234: public void testGetInstance() {
235: AlphaBean alphaBean = new AlphaBean("Now On Air... John Peel");
236: WrapDynaBean dynaBean = new WrapDynaBean(alphaBean);
237: Object wrappedInstance = dynaBean.getInstance();
238: assertTrue("Object type is AlphaBean",
239: wrappedInstance instanceof AlphaBean);
240: AlphaBean wrappedAlphaBean = (AlphaBean) wrappedInstance;
241: assertTrue("Same Object", wrappedAlphaBean == alphaBean);
242: }
243:
244: /** Tests the newInstance implementation for WrapDynaClass */
245: public void testNewInstance() throws Exception {
246: WrapDynaClass dynaClass = WrapDynaClass
247: .createDynaClass(AlphaBean.class);
248: Object createdInstance = dynaClass.newInstance();
249: assertTrue("Object type is WrapDynaBean",
250: createdInstance instanceof WrapDynaBean);
251: WrapDynaBean dynaBean = (WrapDynaBean) createdInstance;
252: assertTrue("Object type is AlphaBean",
253: dynaBean.getInstance() instanceof AlphaBean);
254: }
255:
256: /**
257: * Serialization and deserialization tests.
258: * (WrapDynaBean is now serializable, although WrapDynaClass still is not)
259: */
260: public void testSerialization() {
261:
262: // Create a bean and set a value
263: WrapDynaBean origBean = new WrapDynaBean(new TestBean());
264: Integer newValue = new Integer(789);
265: assertEquals("origBean default", new Integer(123),
266: (Integer) origBean.get("intProperty"));
267: origBean.set("intProperty", newValue);
268: assertEquals("origBean new value", newValue, (Integer) origBean
269: .get("intProperty"));
270:
271: // Serialize/Deserialize & test value
272: WrapDynaBean bean = (WrapDynaBean) serializeDeserialize(
273: origBean, "First Test");
274: assertEquals("bean value", newValue, (Integer) bean
275: .get("intProperty"));
276:
277: }
278:
279: /**
280: * Do serialization and deserialization.
281: */
282: private Object serializeDeserialize(Object target, String text) {
283:
284: // Serialize the test object
285: ByteArrayOutputStream baos = new ByteArrayOutputStream();
286: try {
287: ObjectOutputStream oos = new ObjectOutputStream(baos);
288: oos.writeObject(target);
289: oos.flush();
290: oos.close();
291: } catch (Exception e) {
292: fail(text + ": Exception during serialization: " + e);
293: }
294:
295: // Deserialize the test object
296: Object result = null;
297: try {
298: ByteArrayInputStream bais = new ByteArrayInputStream(baos
299: .toByteArray());
300: ObjectInputStream ois = new ObjectInputStream(bais);
301: result = ois.readObject();
302: bais.close();
303: } catch (Exception e) {
304: fail(text + ": Exception during deserialization: " + e);
305: }
306: return result;
307:
308: }
309:
310: }
|