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: package org.apache.commons.beanutils;
018:
019: import junit.framework.TestCase;
020: import junit.framework.Test;
021: import junit.framework.TestSuite;
022: import java.lang.reflect.Method;
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025:
026: /**
027: * <p>Test Case for the <code>MappedPropertyDescriptor</code>.</p>
028: *
029: * @author Niall Pemberton
030: */
031: public class MappedPropertyTestCase extends TestCase {
032:
033: private static final Log log = LogFactory
034: .getLog(MappedPropertyTestCase.class);
035:
036: // ---------------------------------------------------------- Constructors
037:
038: /**
039: * Construct a new instance of this test case.
040: *
041: * @param name Name of the test case
042: */
043: public MappedPropertyTestCase(String name) {
044: super (name);
045: }
046:
047: // -------------------------------------------------- Overall Test Methods
048:
049: /**
050: * Run this Test
051: */
052: public static void main(String[] args) {
053: junit.textui.TestRunner.run(suite());
054: }
055:
056: /**
057: * Set up instance variables required by this test case.
058: */
059: public void setUp() throws Exception {
060: }
061:
062: /**
063: * Return the tests included in this test suite.
064: */
065: public static Test suite() {
066: return (new TestSuite(MappedPropertyTestCase.class));
067: }
068:
069: /**
070: * Tear down instance variables required by this test case.
071: */
072: public void tearDown() {
073: }
074:
075: // ------------------------------------------------ Individual Test Methods
076:
077: /**
078: * Test valid method name
079: */
080: public void testFound() {
081: String property = "mapproperty";
082: Class clazz = MappedPropertyTestBean.class;
083: try {
084: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
085: property, clazz);
086: assertNotNull("Getter is missing", desc
087: .getMappedReadMethod());
088: assertNotNull("Setter is missing", desc
089: .getMappedWriteMethod());
090: } catch (Exception ex) {
091: fail("Property '" + property + "' Not Found in "
092: + clazz.getName() + ": " + ex);
093: }
094: }
095:
096: /**
097: * Test boolean "is" method name
098: */
099: public void testBooleanMapped() {
100: String property = "mappedBoolean";
101: Class clazz = MappedPropertyTestBean.class;
102: try {
103: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
104: property, clazz);
105: assertNotNull("Getter is missing", desc
106: .getMappedReadMethod());
107: assertNotNull("Setter is missing", desc
108: .getMappedWriteMethod());
109: } catch (Exception ex) {
110: fail("Property '" + property + "' Not Found in "
111: + clazz.getName() + ": " + ex);
112: }
113: }
114:
115: /**
116: * Test invalid method name
117: */
118: public void testNotFound() {
119: String property = "xxxxxxx";
120: Class clazz = MappedPropertyTestBean.class;
121: try {
122: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
123: property, clazz);
124: fail("Property '" + property + "' found in "
125: + clazz.getName());
126: } catch (Exception ex) {
127: // expected result
128: }
129: }
130:
131: /**
132: * Test Mapped Property - Getter only
133: */
134: public void testMappedGetterOnly() {
135: String property = "mappedGetterOnly";
136: Class clazz = MappedPropertyTestBean.class;
137: try {
138: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
139: property, clazz);
140: assertNotNull("Getter is missing", desc
141: .getMappedReadMethod());
142: assertNull("Setter is found", desc.getMappedWriteMethod());
143: } catch (Exception ex) {
144: fail("Property '" + property + "' Not Found in "
145: + clazz.getName() + ": " + ex);
146: }
147: }
148:
149: /**
150: * Test Mapped Property - Setter Only
151: */
152: public void testMappedSetterOnly() {
153: String property = "mappedSetterOnly";
154: Class clazz = MappedPropertyTestBean.class;
155: try {
156: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
157: property, clazz);
158: assertNull("Getter is found", desc.getMappedReadMethod());
159: assertNotNull("Setter is missing", desc
160: .getMappedWriteMethod());
161: } catch (Exception ex) {
162: fail("Property '" + property + "' Not Found in "
163: + clazz.getName() + ": " + ex);
164: }
165: }
166:
167: /**
168: * Test Mapped Property - Invalid Setter
169: */
170: public void testInvalidSetter() {
171: String property = "invalidSetter";
172: Class clazz = MappedPropertyTestBean.class;
173: try {
174: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
175: property, clazz);
176: assertNotNull("Getter is missing", desc
177: .getMappedReadMethod());
178: assertNull("Setter is found", desc.getMappedWriteMethod());
179: } catch (Exception ex) {
180: fail("Property '" + property + "' Not Found in "
181: + clazz.getName() + ": " + ex);
182: }
183: }
184:
185: /**
186: * Test Mapped Property - Invalid Getter
187: */
188: public void testInvalidGetter() {
189: String property = "invalidGetter";
190: Class clazz = MappedPropertyTestBean.class;
191: try {
192: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
193: property, clazz);
194: assertNull("Getter is found", desc.getMappedReadMethod());
195: assertNotNull("Setter is missing", desc
196: .getMappedWriteMethod());
197: } catch (Exception ex) {
198: fail("Property '" + property + "' Not Found in "
199: + clazz.getName() + ": " + ex);
200: }
201: }
202:
203: /**
204: * Test Mapped Property - Different Types
205: *
206: * Expect to find the getDifferentTypes() method, but not
207: * the setDifferentTypes() method because setDifferentTypes()
208: * sets and Integer, while getDifferentTypes() returns a Long.
209: */
210: public void testDifferentTypes() {
211: String property = "differentTypes";
212: Class clazz = MappedPropertyTestBean.class;
213: try {
214: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
215: property, clazz);
216: assertNotNull("Getter is missing", desc
217: .getMappedReadMethod());
218: assertNull("Setter is found", desc.getMappedWriteMethod());
219: } catch (Exception ex) {
220: fail("Property '" + property + "' Not Found in "
221: + clazz.getName() + ": " + ex);
222: }
223: }
224:
225: /**
226: * Test Mpa getter
227: */
228: public void testMapGetter() {
229: MappedPropertyTestBean bean = new MappedPropertyTestBean();
230: Class clazz = MappedPropertyTestBean.class;
231: String property = "myMap";
232: try {
233: String testValue = "test value";
234: String testKey = "testKey";
235: BeanUtils.setProperty(bean, "myMap(" + testKey + ")",
236: "test value");
237: assertEquals("Map getter", testValue, bean.getMyMap().get(
238: testKey));
239: } catch (Exception ex) {
240: fail("Test set mapped property failed: " + ex);
241: }
242: }
243:
244: /**
245: * Test property with any two args
246: */
247: public void testAnyArgsProperty() {
248: String property = "anyMapped";
249: Class clazz = MappedPropertyTestBean.class;
250: try {
251: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
252: property, clazz);
253: assertNull("Getter is found", desc.getMappedReadMethod());
254: assertNotNull("Setter is missing", desc
255: .getMappedWriteMethod());
256: } catch (Exception ex) {
257: fail("Property '" + property + "' Not Found in "
258: + clazz.getName() + ": " + ex);
259: }
260: }
261:
262: /**
263: * Test property with two primitve args
264: */
265: public void testPrimitiveArgsProperty() {
266: String property = "mappedPrimitive";
267: Class clazz = MappedPropertyTestBean.class;
268: try {
269: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
270: property, clazz);
271: assertNull("Getter is found", desc.getMappedReadMethod());
272: assertNotNull("Setter is missing", desc
273: .getMappedWriteMethod());
274: } catch (Exception ex) {
275: fail("Property '" + property + "' Not Found in "
276: + clazz.getName() + ": " + ex);
277: }
278: }
279:
280: /**
281: * Test 'protected' mapped property
282: */
283: public void testProtected() {
284: String property = "protectedProperty";
285: Class clazz = MappedPropertyTestBean.class;
286: try {
287: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
288: property, clazz);
289: fail("Property '" + property + "' found in "
290: + clazz.getName());
291: } catch (Exception ex) {
292: // expected result
293: }
294: }
295:
296: /**
297: * Test 'public' method in parent
298: */
299: public void testPublicParentMethod() {
300: String property = "mapproperty";
301: Class clazz = MappedPropertyChildBean.class;
302: try {
303: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
304: property, clazz);
305: assertNotNull("Getter is missing", desc
306: .getMappedReadMethod());
307: assertNotNull("Setter is missing", desc
308: .getMappedWriteMethod());
309: } catch (Exception ex) {
310: fail("Property '" + property + "' Not Found in "
311: + clazz.getName() + ": " + ex);
312: }
313: }
314:
315: /**
316: * Test 'protected' method in parent
317: */
318: public void testProtectedParentMethod() {
319: String property = "protectedMapped";
320: Class clazz = MappedPropertyChildBean.class;
321: try {
322: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
323: property, clazz);
324: fail("Property '" + property + "' found in "
325: + clazz.getName());
326: } catch (Exception ex) {
327: }
328: }
329:
330: /**
331: * Test Interface with mapped property
332: */
333: public void testInterfaceMapped() {
334: String property = "mapproperty";
335: Class clazz = MappedPropertyTestInterface.class;
336: try {
337: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
338: property, clazz);
339: assertNotNull("Getter is missing", desc
340: .getMappedReadMethod());
341: assertNotNull("Setter is missing", desc
342: .getMappedWriteMethod());
343: } catch (Exception ex) {
344: fail("Property '" + property + "' Not Found in "
345: + clazz.getName() + ": " + ex);
346: }
347: }
348:
349: /**
350: * Test property not found in interface
351: */
352: public void testInterfaceNotFound() {
353: String property = "XXXXXX";
354: Class clazz = MappedPropertyTestInterface.class;
355: try {
356: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
357: property, clazz);
358: fail("Property '" + property + "' found in "
359: + clazz.getName());
360: } catch (Exception ex) {
361: }
362: }
363:
364: /**
365: * Test Interface Inherited mapped property
366: */
367: public void testChildInterfaceMapped() {
368: String property = "mapproperty";
369: Class clazz = MappedPropertyChildInterface.class;
370: try {
371: MappedPropertyDescriptor desc = new MappedPropertyDescriptor(
372: property, clazz);
373: assertNotNull("Getter is missing", desc
374: .getMappedReadMethod());
375: assertNotNull("Setter is missing", desc
376: .getMappedWriteMethod());
377: } catch (Exception ex) {
378: fail("Property '" + property + "' Not Found in "
379: + clazz.getName() + ": " + ex);
380: }
381: }
382: }
|