View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.beanutils;
18  
19  import java.beans.IntrospectionException;
20  import java.beans.PropertyDescriptor;
21  import java.net.URI;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import junit.framework.TestCase;
26  
27  /**
28   * Test class for {@code FluentPropertyBeanIntrospector}.
29   *
30   * @version $Id$
31   */
32  public class FluentPropertyBeanIntrospectorTestCase extends TestCase {
33      /**
34       * Puts all property descriptors into a map so that they can be accessed by
35       * property name.
36       *
37       * @param descs the array with descriptors
38       * @return a map with property names as keys
39       */
40      private static Map<String, PropertyDescriptor> createDescriptorMap(
41              final PropertyDescriptor[] descs) {
42          final Map<String, PropertyDescriptor> map = new HashMap<String, PropertyDescriptor>();
43          for (final PropertyDescriptor pd : descs) {
44              map.put(pd.getName(), pd);
45          }
46          return map;
47      }
48  
49      /**
50       * Convenience method for obtaining a specific property descriptor and
51       * checking whether it exists.
52       *
53       * @param props the map with property descriptors
54       * @param name the name of the desired descriptor
55       * @return the descriptor from the map
56       */
57      private static PropertyDescriptor fetchDescriptor(
58              final Map<String, PropertyDescriptor> props, final String name) {
59          assertTrue("Property not found: " + name, props.containsKey(name));
60          return props.get(name);
61      }
62  
63      /**
64       * Tries to create an instance without a prefix for write methods.
65       */
66      public void testInitNoPrefix() {
67          try {
68              new FluentPropertyBeanIntrospector(null);
69              fail("Missing prefix for write methods not detected!");
70          } catch (final IllegalArgumentException iex) {
71              // ok
72          }
73      }
74  
75      /**
76       * Tests whether correct property descriptors are detected.
77       */
78      public void testIntrospection() throws IntrospectionException {
79          final PropertyUtilsBean pu = new PropertyUtilsBean();
80          final FluentPropertyBeanIntrospector introspector = new FluentPropertyBeanIntrospector();
81          pu.addBeanIntrospector(introspector);
82          final Map<String, PropertyDescriptor> props = createDescriptorMap(pu
83                  .getPropertyDescriptors(FluentIntrospectionTestBean.class));
84          PropertyDescriptor pd = fetchDescriptor(props, "name");
85          assertNotNull("No read method for name", pd.getReadMethod());
86          assertNotNull("No write method for name", pd.getWriteMethod());
87          fetchDescriptor(props, "stringProperty");
88          pd = fetchDescriptor(props, "fluentProperty");
89          assertNull("Read method for fluentProperty", pd.getReadMethod());
90          assertNotNull("No write method for fluentProperty", pd.getWriteMethod());
91          pd = fetchDescriptor(props, "fluentGetProperty");
92          assertNotNull("No read method for fluentGetProperty",
93                  pd.getReadMethod());
94          assertNotNull("No write method for fluentGetProperty",
95                  pd.getWriteMethod());
96      }
97  
98      public void testIntrospectionCaps() throws Exception {
99  	    final PropertyUtilsBean pu = new PropertyUtilsBean();
100 
101         final FluentPropertyBeanIntrospector introspector = new FluentPropertyBeanIntrospector();
102 
103 	    pu.addBeanIntrospector(introspector);
104 
105 	    final Map<String, PropertyDescriptor> props = createDescriptorMap(
106 			pu.getPropertyDescriptors(CapsBean.class));
107 
108 	    PropertyDescriptor aDescriptor = fetchDescriptor(props, "URI");
109 
110 	    assertNotNull("missing property", aDescriptor);
111 
112 	    assertNotNull("No read method for uri", aDescriptor.getReadMethod());
113 	    assertNotNull("No write method for uri", aDescriptor.getWriteMethod());
114 
115 	    assertNull("Should not find mis-capitalized property", props.get("uRI"));
116     }
117 
118 	public static final class CapsBean {
119 		private URI mURI;
120 
121 		public URI getURI() {
122 			return mURI;
123 		}
124 
125 		public void setURI(final URI theURI) {
126 			mURI = theURI;
127 		}
128 	}
129 }