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.bugs;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.beanutils.BeanUtils;
027: import org.apache.commons.beanutils.PropertyUtils;
028: import org.apache.commons.beanutils.WrapDynaBean;
029: import org.apache.commons.beanutils.bugs.other.Jira61BeanFactory;
030: import org.apache.commons.beanutils.bugs.other.Jira61BeanFactory.TestBean;
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: /**
035: * Test case for Jira issue# BEANUTILS-61.
036: * <p />
037: * See https://issues.apache.org/jira/browse/BEANUTILS-61
038: * <p />
039: *
040: * {@link WrapDynaBean} is a secial case for the PropertyUtils's
041: * isReadable() and isWriteable() methods - since the bean being
042: * wrapped may have read-only or write-only properties (unlike
043: * regular DynaBeans.
044: *
045: * @version $Revision: 553357 $ $Date: 2007-07-05 02:10:25 +0100 (Thu, 05 Jul 2007) $
046: */
047: public class Jira61TestCase extends TestCase {
048:
049: private Log log = LogFactory.getLog(Jira61TestCase.class);
050: private Jira61BeanFactory.TestBean testBean;
051: private WrapDynaBean wrapDynaBean;
052:
053: /**
054: * Create a test case with the specified name.
055: *
056: * @param name The name of the test
057: */
058: public Jira61TestCase(String name) {
059: super (name);
060: }
061:
062: /**
063: * Run the Test.
064: *
065: * @param args Arguments
066: */
067: public static void main(String[] args) {
068: junit.textui.TestRunner.run(suite());
069: }
070:
071: /**
072: * Create a test suite for this test.
073: *
074: * @return a test suite
075: */
076: public static Test suite() {
077: return (new TestSuite(Jira61TestCase.class));
078: }
079:
080: /**
081: * Set up.
082: *
083: * @throws java.lang.Exception
084: */
085: protected void setUp() throws Exception {
086: super .setUp();
087: testBean = Jira61BeanFactory.createBean();
088: PropertyUtils.getPropertyDescriptor(testBean, "mappedReadOnly");
089: PropertyUtils
090: .getPropertyDescriptor(testBean, "mappedWriteOnly");
091: wrapDynaBean = new WrapDynaBean(testBean);
092: }
093:
094: /**
095: * Tear Down.
096: *
097: * @throws java.lang.Exception
098: */
099: protected void tearDown() throws Exception {
100: super .tearDown();
101: }
102:
103: /**
104: * Test {@link PropertyUtils#isReadable(Object, String)}
105: * for simple properties.
106: */
107: public void testIssue_BEANUTILS_61_PropertyUtils_isReadable() {
108: boolean result = false;
109:
110: try {
111: result = PropertyUtils.isReadable(wrapDynaBean,
112: "simpleReadOnly");
113: } catch (Throwable t) {
114: log.error("ERROR " + t, t);
115: fail("simpleReadOnly Threw exception: " + t);
116: }
117: assertTrue(
118: "PropertyUtils.isReadable(bean, \"simpleReadOnly\") returned "
119: + result, result);
120:
121: try {
122: result = PropertyUtils.isReadable(wrapDynaBean,
123: "simpleWriteOnly");
124: } catch (Throwable t) {
125: log.error("ERROR " + t, t);
126: fail("simpleWriteOnly Threw exception: " + t);
127: }
128: assertFalse(
129: "PropertyUtils.isReadable(bean, \"simpleWriteOnly\") returned "
130: + result, result);
131: }
132:
133: /**
134: * Test {@link PropertyUtils#isWriteable(Object, String)}
135: * for simple properties.
136: */
137: public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable() {
138: boolean result = false;
139:
140: try {
141: result = PropertyUtils.isWriteable(wrapDynaBean,
142: "simpleReadOnly");
143: } catch (Throwable t) {
144: log.error("ERROR " + t, t);
145: fail("simpleReadOnly Threw exception: " + t);
146: }
147: assertFalse(
148: "PropertyUtils.isWriteable(bean, \"simpleReadOnly\") returned "
149: + result, result);
150:
151: try {
152: result = PropertyUtils.isWriteable(wrapDynaBean,
153: "simpleWriteOnly");
154: } catch (Throwable t) {
155: log.error("ERROR " + t, t);
156: fail("simpleWriteOnly Threw exception: " + t);
157: }
158: assertTrue(
159: "PropertyUtils.isWriteable(bean, \"simpleWriteOnly\") returned "
160: + result, result);
161: }
162:
163: /**
164: * Test {@link PropertyUtils#isReadable(Object, String)}
165: * for indexed properties.
166: */
167: public void testIssue_BEANUTILS_61_PropertyUtils_isReadable_Indexed() {
168: boolean result = false;
169:
170: try {
171: result = PropertyUtils.isReadable(wrapDynaBean,
172: "indexedReadOnly");
173: } catch (Throwable t) {
174: log.error("ERROR " + t, t);
175: fail("indexedReadOnly Threw exception: " + t);
176: }
177: assertTrue(
178: "PropertyUtils.isReadable(bean, \"indexedReadOnly\") returned "
179: + result, result);
180:
181: try {
182: result = PropertyUtils.isReadable(wrapDynaBean,
183: "indexedWriteOnly");
184: } catch (Throwable t) {
185: log.error("ERROR " + t, t);
186: fail("indexedWriteOnly Threw exception: " + t);
187: }
188: assertFalse(
189: "PropertyUtils.isReadable(bean, \"indexedWriteOnly\") returned "
190: + result, result);
191: }
192:
193: /**
194: * Test {@link PropertyUtils#isReadable(Object, String)}
195: * for mapped properties.
196: */
197: public void testIssue_BEANUTILS_61_PropertyUtils_isReadable_Mapped() {
198: boolean result = false;
199:
200: try {
201: result = PropertyUtils.isReadable(wrapDynaBean,
202: "mappedReadOnly");
203: } catch (Throwable t) {
204: log.error("ERROR " + t, t);
205: fail("mappedReadOnly Threw exception: " + t);
206: }
207: assertTrue(
208: "PropertyUtils.isReadable(bean, \"mappedReadOnly\") returned "
209: + result, result);
210:
211: try {
212: result = PropertyUtils.isReadable(wrapDynaBean,
213: "mappedWriteOnly");
214: } catch (Throwable t) {
215: log.error("ERROR " + t, t);
216: fail("mappedWriteOnly Threw exception: " + t);
217: }
218: assertFalse(
219: "PropertyUtils.isReadable(bean, \"mappedWriteOnly\") returned "
220: + result, result);
221: }
222:
223: /**
224: * Test {@link PropertyUtils#isWriteable(Object, String)}
225: * for indexed properties.
226: */
227: public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable_Indexed() {
228: boolean result = false;
229:
230: try {
231: result = PropertyUtils.isWriteable(wrapDynaBean,
232: "indexedReadOnly");
233: } catch (Throwable t) {
234: log.error("ERROR " + t, t);
235: fail("indexedReadOnly Threw exception: " + t);
236: }
237: assertFalse(
238: "PropertyUtils.isWriteable(bean, \"indexedReadOnly\") returned "
239: + result, result);
240:
241: try {
242: result = PropertyUtils.isWriteable(wrapDynaBean,
243: "indexedWriteOnly");
244: } catch (Throwable t) {
245: log.error("ERROR " + t, t);
246: fail("indexedWriteOnly Threw exception: " + t);
247: }
248: assertTrue(
249: "PropertyUtils.isWriteable(bean, \"indexedWriteOnly\") returned "
250: + result, result);
251: }
252:
253: /**
254: * Test {@link PropertyUtils#isWriteable(Object, String)}
255: * for mapped properties.
256: */
257: public void testIssue_BEANUTILS_61_PropertyUtils_isWriteable_Mapped() {
258: boolean result = false;
259:
260: try {
261: result = PropertyUtils.isWriteable(wrapDynaBean,
262: "mappedReadOnly");
263: } catch (Throwable t) {
264: log.error("ERROR " + t, t);
265: fail("mappedReadOnly Threw exception: " + t);
266: }
267: assertFalse(
268: "PropertyUtils.isWriteable(bean, \"mappedReadOnly\") returned "
269: + result, result);
270:
271: try {
272: result = PropertyUtils.isWriteable(wrapDynaBean,
273: "mappedWriteOnly");
274: } catch (Throwable t) {
275: log.error("ERROR " + t, t);
276: fail("mappedWriteOnly Threw exception: " + t);
277: }
278: assertTrue(
279: "PropertyUtils.isWriteable(bean, \"mappedWriteOnly\") returned "
280: + result, result);
281: }
282:
283: /**
284: * Test {@link PropertyUtils#getProperty(Object, String)}
285: * for simple properties.
286: */
287: public void testIssue_BEANUTILS_61_PropertyUtils_getProperty() {
288: boolean threwIllegalArgumentException = false;
289: Object result = null;
290: try {
291: result = PropertyUtils.getProperty(wrapDynaBean,
292: "simpleReadOnly");
293: } catch (Throwable t) {
294: log.error("ERROR " + t, t);
295: fail("simpleWriteOnly Threw exception: " + t);
296: }
297: assertEquals("simpleReadOnly", testBean.getSimpleReadOnly(),
298: result);
299:
300: try {
301: result = PropertyUtils.getProperty(wrapDynaBean,
302: "simpleWriteOnly");
303: } catch (IllegalArgumentException ex) {
304: threwIllegalArgumentException = true; // expected result
305: } catch (Throwable t) {
306: log.error("ERROR " + t, t);
307: fail("simpleWriteOnly Threw exception: " + t);
308: }
309: assertTrue("Expected IllegalArgumentException but returned '"
310: + result + "'", threwIllegalArgumentException);
311: }
312:
313: /**
314: * Test {@link PropertyUtils#setProperty(Object, String, Object)}
315: * for simple properties.
316: */
317: public void testIssue_BEANUTILS_61_PropertyUtils_setProperty() {
318: boolean threwIllegalArgumentException = false;
319: try {
320: PropertyUtils.setProperty(wrapDynaBean, "simpleReadOnly",
321: "READONLY-SIMPLE-BAR");
322: } catch (IllegalArgumentException ex) {
323: threwIllegalArgumentException = true; // expected result
324: } catch (Throwable t) {
325: log.error("ERROR " + t, t);
326: fail("simpleReadOnly Threw exception: " + t);
327: }
328: assertTrue("Expected IllegalArgumentException",
329: threwIllegalArgumentException);
330:
331: try {
332: PropertyUtils.setProperty(wrapDynaBean, "simpleWriteOnly",
333: "SIMPLE-BAR");
334: } catch (Throwable t) {
335: log.error("ERROR " + t, t);
336: fail("simpleWriteOnly Threw exception: " + t);
337: }
338: assertEquals("simpleWriteOnly", testBean.getSimpleReadOnly(),
339: "SIMPLE-BAR");
340: }
341:
342: /**
343: * Test {@link PropertyUtils#getProperty(Object, String)}
344: * for indexed properties.
345: */
346: public void testIssue_BEANUTILS_61_PropertyUtils_getProperty_Indexed() {
347: boolean threwIllegalArgumentException = false;
348: Object result = null;
349: try {
350: result = PropertyUtils.getProperty(wrapDynaBean,
351: "indexedReadOnly[0]");
352: } catch (Throwable t) {
353: log.error("ERROR " + t, t);
354: fail("indexedReadOnly Threw exception: " + t);
355: }
356: assertEquals("indexedReadOnly", testBean.getIndexedReadOnly(0),
357: result);
358:
359: try {
360: result = PropertyUtils.getProperty(wrapDynaBean,
361: "indexedWriteOnly[0]");
362: } catch (IllegalArgumentException ex) {
363: threwIllegalArgumentException = true; // expected result
364: } catch (Throwable t) {
365: log.error("ERROR " + t, t);
366: fail("indexedWriteOnly Threw exception: " + t);
367: }
368: assertTrue("Expected IllegalArgumentException but returned '"
369: + result + "'", threwIllegalArgumentException);
370: }
371:
372: /**
373: * Test {@link PropertyUtils#setProperty(Object, String, Object)}
374: * for indexed properties.
375: */
376: public void testIssue_BEANUTILS_61_PropertyUtils_setProperty_Indexed() {
377: boolean threwIllegalArgumentException = false;
378: try {
379: PropertyUtils.setProperty(wrapDynaBean,
380: "indexedReadOnly[0]", "READONLY-INDEXED-BAR");
381: } catch (IllegalArgumentException ex) {
382: threwIllegalArgumentException = true; // expected result
383: } catch (Throwable t) {
384: log.error("ERROR " + t, t);
385: fail("indexedReadOnly Threw exception: " + t);
386: }
387: assertTrue("Expected IllegalArgumentException",
388: threwIllegalArgumentException);
389:
390: try {
391: PropertyUtils.setProperty(wrapDynaBean,
392: "indexedWriteOnly[0]", "INDEXED-BAR");
393: } catch (Throwable t) {
394: log.error("ERROR " + t, t);
395: fail("indexedWriteOnly Threw exception: " + t);
396: }
397: assertEquals("indexedWriteOnly",
398: testBean.getIndexedReadOnly(0), "INDEXED-BAR");
399: }
400:
401: /**
402: * Test {@link PropertyUtils#getProperty(Object, String)}
403: * for mapped properties.
404: */
405: public void testIssue_BEANUTILS_61_PropertyUtils_getProperty_Mapped() {
406: boolean threwIllegalArgumentException = false;
407: Object result = null;
408: try {
409: result = PropertyUtils.getProperty(wrapDynaBean,
410: "mappedReadOnly(foo-key)");
411: } catch (Throwable t) {
412: log.error("ERROR " + t, t);
413: fail("mappedReadOnly Threw exception: " + t);
414: }
415: assertEquals("mappedReadOnly", testBean
416: .getMappedReadOnly("foo-key"), result);
417:
418: try {
419: result = PropertyUtils.getProperty(wrapDynaBean,
420: "mappedWriteOnly(foo-key)");
421: } catch (IllegalArgumentException ex) {
422: threwIllegalArgumentException = true; // expected result
423: } catch (Throwable t) {
424: log.error("ERROR " + t, t);
425: fail("mappedWriteOnly Threw exception: " + t);
426: }
427: assertTrue("Expected IllegalArgumentException but returned '"
428: + result + "'", threwIllegalArgumentException);
429: }
430:
431: /**
432: * Test {@link PropertyUtils#setProperty(Object, String, Object)}
433: * for mapped properties.
434: */
435: public void testIssue_BEANUTILS_61_PropertyUtils_setProperty_Mapped() {
436: boolean threwIllegalArgumentException = false;
437: try {
438: PropertyUtils.setProperty(wrapDynaBean,
439: "mappedReadOnly(foo-key)", "READONLY-MAPPED-BAR");
440: } catch (IllegalArgumentException ex) {
441: threwIllegalArgumentException = true; // expected result
442: } catch (Throwable t) {
443: log.error("ERROR " + t, t);
444: fail("mappedReadOnly Threw exception: " + t);
445: }
446: assertTrue("Expected IllegalArgumentException",
447: threwIllegalArgumentException);
448:
449: try {
450: PropertyUtils.setProperty(wrapDynaBean,
451: "mappedWriteOnly(foo-key)", "MAPPED-BAR");
452: } catch (Throwable t) {
453: log.error("ERROR " + t, t);
454: fail("mappedWriteOnly Threw exception: " + t);
455: }
456: assertEquals("mappedWriteOnly", testBean
457: .getMappedReadOnly("foo-key"), "MAPPED-BAR");
458: }
459:
460: /**
461: * Test {@link PropertyUtils#copyProperties(Object, Object)}
462: * to a read-only WrapDynaBean property.
463: */
464: public void testIssue_BEANUTILS_61_PropertyUtils_copyProperties_to_WrapDynaBean() {
465: String value = "copied simpleReadOnly";
466: Map source = new HashMap();
467: source.put("simpleReadOnly", value);
468: try {
469: PropertyUtils.copyProperties(wrapDynaBean, source);
470: } catch (Throwable t) {
471: log.error("ERROR " + t, t);
472: fail("copyProperties Threw exception: " + t);
473: }
474: assertFalse("Target value='" + value + "'", value
475: .equals(testBean.getSimpleReadOnly()));
476: }
477:
478: /**
479: * Test {@link PropertyUtils#copyProperties(Object, Object)}
480: * to a read-only WrapDynaBean property.
481: */
482: public void testIssue_BEANUTILS_61_PropertyUtils_copyProperties_from_WrapDynaBean() {
483: String value = "ORIG TARGET VALUE";
484: TestBean targetBean = Jira61BeanFactory.createBean();
485: targetBean.setSimpleWriteOnly(value);
486: try {
487: PropertyUtils.copyProperties(targetBean, wrapDynaBean);
488: } catch (Throwable t) {
489: log.error("ERROR " + t, t);
490: fail("copyProperties Threw exception: " + t);
491: }
492: assertTrue("Target value='" + targetBean.getSimpleReadOnly()
493: + "'", value.equals(targetBean.getSimpleReadOnly()));
494: }
495:
496: /**
497: * Test {@link BeanUtils#copyProperties(Object, Object)}
498: * to a read-only WrapDynaBean property.
499: */
500: public void testIssue_BEANUTILS_61_BeanUtils_copyProperties_to_WrapDynaBean() {
501: String value = "copied simpleReadOnly";
502: Map source = new HashMap();
503: source.put("simpleReadOnly", value);
504: try {
505: BeanUtils.copyProperties(wrapDynaBean, source);
506: } catch (Throwable t) {
507: log.error("ERROR " + t, t);
508: fail("copyProperties Threw exception: " + t);
509: }
510: assertFalse("Target value='" + value + "'", value
511: .equals(testBean.getSimpleReadOnly()));
512: }
513:
514: /**
515: * Test {@link BeanUtils#copyProperties(Object, Object)}
516: * to a read-only WrapDynaBean property.
517: */
518: public void testIssue_BEANUTILS_61_BeanUtils_copyProperties_from_WrapDynaBean() {
519: String value = "ORIG TARGET VALUE";
520: TestBean targetBean = Jira61BeanFactory.createBean();
521: targetBean.setSimpleWriteOnly(value);
522: try {
523: BeanUtils.copyProperties(targetBean, wrapDynaBean);
524: } catch (Throwable t) {
525: log.error("ERROR " + t, t);
526: fail("copyProperties Threw exception: " + t);
527: }
528: assertTrue("Target value='" + targetBean.getSimpleReadOnly()
529: + "'", value.equals(targetBean.getSimpleReadOnly()));
530: }
531: }
|