001: /*
002: * $Id: IndexedPropertyTestCase.java 546480 2007-06-12 13:35:32Z niallp $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: package org.apache.commons.beanutils;
021:
022: import java.util.List;
023: import java.util.ArrayList;
024: import java.beans.IndexedPropertyDescriptor;
025: import java.beans.PropertyDescriptor;
026:
027: import junit.framework.Test;
028: import junit.framework.TestCase;
029: import junit.framework.TestSuite;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: /**
035: * <p>Test Case for the Indexed Properties.</p>
036: *
037: * @author Niall Pemberton
038: * @version $Revision: 546480 $ $Date: 2007-06-12 14:35:32 +0100 (Tue, 12 Jun 2007) $
039: */
040:
041: public class IndexedPropertyTestCase extends TestCase {
042:
043: private static final Log log = LogFactory
044: .getLog(IndexedPropertyTestCase.class);
045:
046: // ---------------------------------------------------- Instance Variables
047:
048: /**
049: * The test bean for each test.
050: */
051: private IndexedTestBean bean = null;
052: private BeanUtilsBean beanUtilsBean;
053: private PropertyUtilsBean propertyUtilsBean;
054: private String[] testArray;
055: private String[] newArray;
056: private List testList;
057: private List newList;
058: private ArrayList arrayList;
059:
060: // ---------------------------------------------------------- Constructors
061:
062: /**
063: * Construct a new instance of this test case.
064: *
065: * @param name Name of the test case
066: */
067: public IndexedPropertyTestCase(String name) {
068: super (name);
069: }
070:
071: // -------------------------------------------------- Overall Test Methods
072:
073: /**
074: * Set up instance variables required by this test case.
075: */
076: public void setUp() {
077:
078: // BeanUtils
079: beanUtilsBean = new BeanUtilsBean();
080: propertyUtilsBean = beanUtilsBean.getPropertyUtils();
081:
082: // initialize Arrays and Lists
083: testArray = new String[] { "array-0", "array-1", "array-2" };
084: newArray = new String[] { "newArray-0", "newArray-1",
085: "newArray-2" };
086:
087: testList = new ArrayList();
088: testList.add("list-0");
089: testList.add("list-1");
090: testList.add("list-2");
091:
092: newList = new ArrayList();
093: newList.add("newList-0");
094: newList.add("newList-1");
095: newList.add("newList-2");
096:
097: arrayList = new ArrayList();
098: arrayList.add("arrayList-0");
099: arrayList.add("arrayList-1");
100: arrayList.add("arrayList-2");
101:
102: // initialize Test Bean properties
103: bean = new IndexedTestBean();
104: bean.setStringArray(testArray);
105: bean.setStringList(testList);
106: bean.setArrayList(arrayList);
107: }
108:
109: /**
110: * Return the tests included in this test suite.
111: */
112: public static Test suite() {
113: return (new TestSuite(IndexedPropertyTestCase.class));
114: }
115:
116: /**
117: * Tear down instance variables required by this test case.
118: */
119: public void tearDown() {
120: bean = null;
121: }
122:
123: // ------------------------------------------------ Individual Test Methods
124:
125: /**
126: * Test IndexedPropertyDescriptor for an Array
127: */
128: public void testArrayIndexedPropertyDescriptor() {
129:
130: try {
131: PropertyDescriptor descriptor = propertyUtilsBean
132: .getPropertyDescriptor(bean, "stringArray");
133: assertNotNull("No Array Descriptor", descriptor);
134: assertEquals("Not IndexedPropertyDescriptor",
135: IndexedPropertyDescriptor.class, descriptor
136: .getClass());
137: assertEquals("PropertDescriptor Type invalid", testArray
138: .getClass(), descriptor.getPropertyType());
139: } catch (Exception e) {
140: fail("Threw exception " + e);
141: }
142: }
143:
144: /**
145: * Test IndexedPropertyDescriptor for a List
146: */
147: public void testListIndexedPropertyDescriptor() {
148:
149: try {
150: PropertyDescriptor descriptor = propertyUtilsBean
151: .getPropertyDescriptor(bean, "stringList");
152: assertNotNull("No List Descriptor", descriptor);
153: assertEquals("Not IndexedPropertyDescriptor",
154: IndexedPropertyDescriptor.class, descriptor
155: .getClass());
156: assertEquals("PropertDescriptor Type invalid", List.class,
157: descriptor.getPropertyType());
158: } catch (Exception e) {
159: fail("Threw exception " + e);
160: }
161: }
162:
163: /**
164: * Test IndexedPropertyDescriptor for an ArrayList
165: */
166: public void testArrayListIndexedPropertyDescriptor() {
167:
168: try {
169: PropertyDescriptor descriptor = propertyUtilsBean
170: .getPropertyDescriptor(bean, "arrayList");
171: assertNotNull("No ArrayList Descriptor", descriptor);
172: assertEquals("Not IndexedPropertyDescriptor",
173: IndexedPropertyDescriptor.class, descriptor
174: .getClass());
175: assertEquals("PropertDescriptor Type invalid",
176: ArrayList.class, descriptor.getPropertyType());
177: } catch (Exception e) {
178: fail("Threw exception " + e);
179: }
180: }
181:
182: /**
183: * Test Read Method for an Array
184: */
185: public void testArrayReadMethod() {
186:
187: try {
188: IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) propertyUtilsBean
189: .getPropertyDescriptor(bean, "stringArray");
190: assertNotNull("No Array Read Method", descriptor
191: .getReadMethod());
192: } catch (Exception e) {
193: fail("Threw exception " + e);
194: }
195: }
196:
197: /**
198: * Test Write Method for an Array
199: */
200: public void testArrayWriteMethod() {
201:
202: try {
203: IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) propertyUtilsBean
204: .getPropertyDescriptor(bean, "stringArray");
205: assertNotNull("No Array Write Method", descriptor
206: .getWriteMethod());
207: } catch (Exception e) {
208: fail("Threw exception " + e);
209: }
210: }
211:
212: /**
213: * Test Indexed Read Method for an Array
214: */
215: public void testArrayIndexedReadMethod() {
216:
217: try {
218: IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) propertyUtilsBean
219: .getPropertyDescriptor(bean, "stringArray");
220: assertNotNull("No Array Indexed Read Method", descriptor
221: .getIndexedReadMethod());
222: } catch (Exception e) {
223: fail("Threw exception " + e);
224: }
225: }
226:
227: /**
228: * Test Indexed Write Method for an Array
229: */
230: public void testArrayIndexedWriteMethod() {
231:
232: try {
233: IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) propertyUtilsBean
234: .getPropertyDescriptor(bean, "stringArray");
235: assertNotNull("No Array Indexed Write Method", descriptor
236: .getIndexedWriteMethod());
237: } catch (Exception e) {
238: fail("Threw exception " + e);
239: }
240: }
241:
242: /**
243: * Test Read Method for a List
244: *
245: * JDK 1.3.1_04: Test Passes
246: * JDK 1.4.2_05: Test Fails - getter which returns java.util.List not returned
247: * by IndexedPropertyDescriptor.getReadMethod();
248: */
249: public void testListReadMethod() {
250:
251: try {
252: IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) propertyUtilsBean
253: .getPropertyDescriptor(bean, "stringList");
254: assertNotNull("No List Read Method", descriptor
255: .getReadMethod());
256: } catch (Exception e) {
257: fail("Threw exception " + e);
258: }
259: }
260:
261: /**
262: * Test Write Method for a List
263: *
264: * JDK 1.3.1_04: Test Passes
265: * JDK 1.4.2_05: Test Fails - setter whith java.util.List argument not returned
266: * by IndexedPropertyDescriptor.getWriteMethod();
267: */
268: public void testListWriteMethod() {
269:
270: try {
271: IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) propertyUtilsBean
272: .getPropertyDescriptor(bean, "stringList");
273: assertNotNull("No List Write Method", descriptor
274: .getWriteMethod());
275: } catch (Exception e) {
276: fail("Threw exception " + e);
277: }
278: }
279:
280: /**
281: * Test Indexed Read Method for a List
282: */
283: public void testListIndexedReadMethod() {
284:
285: try {
286: IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) propertyUtilsBean
287: .getPropertyDescriptor(bean, "stringList");
288: assertNotNull("No List Indexed Read Method", descriptor
289: .getIndexedReadMethod());
290: } catch (Exception e) {
291: fail("Threw exception " + e);
292: }
293: }
294:
295: /**
296: * Test Indexed Write Method for a List
297: */
298: public void testListIndexedWriteMethod() {
299:
300: try {
301: IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) propertyUtilsBean
302: .getPropertyDescriptor(bean, "stringList");
303: assertNotNull("No List Indexed Write Method", descriptor
304: .getIndexedWriteMethod());
305: } catch (Exception e) {
306: fail("Threw exception " + e);
307: }
308: }
309:
310: /**
311: * Test Read Method for an ArrayList
312: */
313: public void testArrayListReadMethod() {
314:
315: try {
316: IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) propertyUtilsBean
317: .getPropertyDescriptor(bean, "arrayList");
318: assertNotNull("No ArrayList Read Method", descriptor
319: .getReadMethod());
320: } catch (Exception e) {
321: fail("Threw exception " + e);
322: }
323: }
324:
325: /**
326: * Test Write Method for an ArrayList
327: */
328: public void testArrayListWriteMethod() {
329:
330: try {
331: IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) propertyUtilsBean
332: .getPropertyDescriptor(bean, "arrayList");
333: assertNotNull("No ArrayList Write Method", descriptor
334: .getWriteMethod());
335: } catch (Exception e) {
336: fail("Threw exception " + e);
337: }
338: }
339:
340: /**
341: * Test getting an array property
342: */
343: public void testGetArray() {
344: try {
345: assertEquals(testArray, propertyUtilsBean.getProperty(bean,
346: "stringArray"));
347: } catch (Exception e) {
348: fail("Threw exception " + e);
349: }
350: }
351:
352: /**
353: * Test getting an array property as a String
354: *
355: * NOTE: Why does retrieving array just return the first element in the array, whereas
356: * retrieveing a List returns a comma separated list of all the elements?
357: */
358: public void testGetArrayAsString() {
359: try {
360: assertEquals("array-0", beanUtilsBean.getProperty(bean,
361: "stringArray"));
362: } catch (Exception e) {
363: fail("Threw exception " + e);
364: }
365: }
366:
367: /**
368: * Test getting an indexed item of an Array using getProperty("name[x]")
369: */
370: public void testGetArrayItemA() {
371:
372: try {
373: assertEquals("array-1", beanUtilsBean.getProperty(bean,
374: "stringArray[1]"));
375: } catch (Exception e) {
376: fail("Threw exception " + e);
377: }
378: }
379:
380: /**
381: * Test getting an indexed item of an Array using getIndexedProperty("name")
382: */
383: public void testGetArrayItemB() {
384:
385: try {
386: assertEquals("array-1", beanUtilsBean.getIndexedProperty(
387: bean, "stringArray", 1));
388: } catch (Exception e) {
389: fail("Threw exception " + e);
390: }
391: }
392:
393: /**
394: * Test getting a List
395: *
396: * JDK 1.3.1_04: Test Passes
397: * JDK 1.4.2_05: Test Fails - fails NoSuchMethodException, i.e. reason as testListReadMethod()
398: * failed.
399: */
400: public void testGetList() {
401:
402: try {
403: assertEquals(testList, propertyUtilsBean.getProperty(bean,
404: "stringList"));
405: } catch (Exception e) {
406: fail("Threw exception " + e);
407: }
408: }
409:
410: /**
411: * Test getting a List property as a String
412: *
413: * JDK 1.3.1_04: Test Passes
414: * JDK 1.4.2_05: Test Fails - fails NoSuchMethodException, i.e. reason as testListReadMethod()
415: * failed.
416: */
417: public void testGetListAsString() {
418:
419: try {
420: assertEquals("list-0", beanUtilsBean.getProperty(bean,
421: "stringList"));
422: } catch (Exception e) {
423: fail("Threw exception " + e);
424: }
425: }
426:
427: /**
428: * Test getting an indexed item of a List using getProperty("name[x]")
429: */
430: public void testGetListItemA() {
431:
432: try {
433: assertEquals("list-1", beanUtilsBean.getProperty(bean,
434: "stringList[1]"));
435: } catch (Exception e) {
436: fail("Threw exception " + e);
437: }
438: }
439:
440: /**
441: * Test getting an indexed item of a List using getIndexedProperty("name")
442: */
443: public void testGetListItemB() {
444:
445: try {
446: assertEquals("list-1", beanUtilsBean.getIndexedProperty(
447: bean, "stringList", 1));
448: } catch (Exception e) {
449: fail("Threw exception " + e);
450: }
451: }
452:
453: /**
454: * Test setting an Array property
455: *
456: * JDK 1.3.1_04 and 1.4.2_05: Test Fails - IllegalArgumentException can't invoke setter, argument type mismatch
457: *
458: * Fails because of a bug in BeanUtilsBean.setProperty() method. Value is always converted to the array's component
459: * type which in this case is a String. Then it calls the setStringArray(String[]) passing a String rather than
460: * String[] causing this exception. If there isn't an "index" value then the PropertyType (rather than
461: * IndexedPropertyType) should be used.
462: *
463: */
464: public void testSetArray() {
465: try {
466: beanUtilsBean.setProperty(bean, "stringArray", newArray);
467: Object value = bean.getStringArray();
468: assertEquals("Type is different", newArray.getClass(),
469: value.getClass());
470: String[] array = (String[]) value;
471: assertEquals("Array Length is different", newArray.length,
472: array.length);
473: for (int i = 0; i < array.length; i++) {
474: assertEquals("Element " + i + " is different",
475: newArray[i], array[i]);
476: }
477: } catch (Exception e) {
478: log.error("testSetArray()", e);
479: fail("Threw exception " + e);
480: }
481: }
482:
483: /**
484: * Test setting an indexed item of an Array using setProperty("name[x]", value)
485: */
486: public void testSetArrayItemA() {
487:
488: try {
489: beanUtilsBean.setProperty(bean, "stringArray[1]",
490: "modified-1");
491: assertEquals("modified-1", bean.getStringArray(1));
492: } catch (Exception e) {
493: fail("Threw exception " + e);
494: }
495: }
496:
497: /**
498: * Test setting an indexed item of an Array using setIndexedProperty("name", value)
499: */
500: public void testSetArrayItemB() {
501:
502: try {
503: propertyUtilsBean.setIndexedProperty(bean, "stringArray",
504: 1, "modified-1");
505: assertEquals("modified-1", bean.getStringArray(1));
506: } catch (Exception e) {
507: fail("Threw exception " + e);
508: }
509: }
510:
511: /**
512: * Test setting a List property
513: *
514: * JDK 1.3.1_04: Test Passes
515: * JDK 1.4.2_05: Test Fails - setter which returns java.util.List not returned
516: * by IndexedPropertyDescriptor.getWriteMethod() - therefore
517: * setProperty does nothing and values remain unchanged.
518: */
519: public void testSetList() {
520: try {
521: beanUtilsBean.setProperty(bean, "stringList", newList);
522: Object value = bean.getStringList();
523: assertEquals("Type is different", newList.getClass(), value
524: .getClass());
525: List list = (List) value;
526: assertEquals("List size is different", newList.size(), list
527: .size());
528: for (int i = 0; i < list.size(); i++) {
529: assertEquals("Element " + i + " is different", newList
530: .get(i), list.get(i));
531: }
532: } catch (Exception e) {
533: log.error("testSetList()", e);
534: fail("Threw exception " + e);
535: }
536: }
537:
538: /**
539: * Test setting an indexed item of a List using setProperty("name[x]", value)
540: */
541: public void testSetListItemA() {
542:
543: try {
544: beanUtilsBean.setProperty(bean, "stringList[1]",
545: "modified-1");
546: assertEquals("modified-1", bean.getStringList(1));
547: } catch (Exception e) {
548: fail("Threw exception " + e);
549: }
550: }
551:
552: /**
553: * Test setting an indexed item of a List using setIndexedProperty("name", value)
554: */
555: public void testSetListItemB() {
556:
557: try {
558: propertyUtilsBean.setIndexedProperty(bean, "stringList", 1,
559: "modified-1");
560: assertEquals("modified-1", bean.getStringList(1));
561: } catch (Exception e) {
562: fail("Threw exception " + e);
563: }
564: }
565:
566: /**
567: * Test getting an ArrayList
568: */
569: public void testGetArrayList() {
570:
571: try {
572: assertEquals(arrayList, propertyUtilsBean.getProperty(bean,
573: "arrayList"));
574: } catch (Exception e) {
575: fail("Threw exception " + e);
576: }
577: }
578:
579: /**
580: * Test setting an ArrayList property
581: */
582: public void testSetArrayList() {
583: try {
584: beanUtilsBean.setProperty(bean, "arrayList", newList);
585: Object value = bean.getArrayList();
586: assertEquals("Type is different", newList.getClass(), value
587: .getClass());
588: List list = (List) value;
589: assertEquals("List size is different", newList.size(), list
590: .size());
591: for (int i = 0; i < list.size(); i++) {
592: assertEquals("Element " + i + " is different", newList
593: .get(i), list.get(i));
594: }
595: } catch (Exception e) {
596: log.error("testSetList()", e);
597: fail("Threw exception " + e);
598: }
599: }
600:
601: }
|