2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

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  
18  package org.apache.shale.test.mock;
19  
20  import java.beans.BeanInfo;
21  import java.beans.IntrospectionException;
22  import java.beans.Introspector;
23  import java.beans.PropertyDescriptor;
24  import java.lang.reflect.InvocationTargetException;
25  import java.util.Map;
26  import javax.faces.el.EvaluationException;
27  import javax.faces.el.PropertyNotFoundException;
28  import javax.faces.el.PropertyResolver;
29  
30  /***
31   * <p>Mock implementation of <code>PropertyResolver</code>.</p>
32   *
33   * $Id$
34   */
35  
36  public class MockPropertyResolver extends PropertyResolver {
37  
38  
39      // ------------------------------------------------------------ Constructors
40  
41  
42      /***
43       * <p>Construct a default instance.</p>
44       */
45      public MockPropertyResolver() {
46      }
47  
48  
49      // ----------------------------------------------------- Mock Object Methods
50  
51  
52      // ------------------------------------------------------ Instance Variables
53  
54  
55      // ------------------------------------------------ PropertyResolver Methods
56  
57  
58      /*** {@inheritDoc} */
59      public Object getValue(Object base, Object property)
60          throws EvaluationException, PropertyNotFoundException {
61  
62          if (base == null) {
63              throw new NullPointerException();
64          }
65          if (base instanceof Map) {
66              return ((Map) base).get(property);
67          }
68          String name = property.toString();
69          PropertyDescriptor descriptor = descriptor(base.getClass(), name);
70          try {
71              return descriptor.getReadMethod().invoke(base, (Object[]) null);
72          } catch (IllegalAccessException e) {
73              throw new EvaluationException(e);
74          } catch (InvocationTargetException e) {
75              throw new EvaluationException(e.getTargetException());
76          }
77  
78      }
79  
80  
81      /*** {@inheritDoc} */
82      public Object getValue(Object base, int index)
83          throws PropertyNotFoundException {
84  
85          return getValue(base, "" + index);
86  
87      }
88  
89  
90      /*** {@inheritDoc} */
91      public void setValue(Object base, Object property, Object value)
92          throws PropertyNotFoundException {
93  
94          if (base == null) {
95              throw new NullPointerException();
96          }
97          if (base instanceof Map) {
98              ((Map) base).put(property, value);
99              return;
100         }
101         String name = property.toString();
102         PropertyDescriptor descriptor = descriptor(base.getClass(), name);
103         try {
104             descriptor.getWriteMethod().invoke(base, new Object[] { value });
105         } catch (IllegalAccessException e) {
106             throw new EvaluationException(e);
107         } catch (InvocationTargetException e) {
108             throw new EvaluationException(e.getTargetException());
109         }
110 
111     }
112 
113 
114     /*** {@inheritDoc} */
115     public void setValue(Object base, int index, Object value)
116         throws PropertyNotFoundException {
117 
118         setValue(base, "" + index, value);
119 
120     }
121 
122 
123     /*** {@inheritDoc} */
124     public boolean isReadOnly(Object base, Object property)
125         throws PropertyNotFoundException {
126 
127         if (base == null) {
128             throw new NullPointerException();
129         }
130         if (base instanceof Map) {
131             return false; // We have no way to know anything more specific
132         }
133         String name = property.toString();
134         PropertyDescriptor descriptor = descriptor(base.getClass(), name);
135         return (descriptor.getWriteMethod() == null);
136 
137     }
138 
139 
140     /*** {@inheritDoc} */
141     public boolean isReadOnly(Object base, int index)
142         throws PropertyNotFoundException {
143 
144         return isReadOnly(base, "" + index);
145 
146     }
147 
148 
149     /*** {@inheritDoc} */
150     public Class getType(Object base, Object property)
151         throws PropertyNotFoundException {
152 
153         if (base == null) {
154             throw new NullPointerException();
155         }
156         if (base instanceof Map) {
157             Object value = ((Map) base).get(property);
158             if (value != null) {
159                 return value.getClass();
160             } else {
161                 return Object.class;
162             }
163         }
164         String name = property.toString();
165         PropertyDescriptor descriptor = descriptor(base.getClass(), name);
166         return descriptor.getPropertyType();
167 
168     }
169 
170 
171     /*** {@inheritDoc} */
172     public Class getType(Object base, int index)
173         throws PropertyNotFoundException {
174 
175         return getType(base, "" + index);
176 
177     }
178 
179 
180     // --------------------------------------------------------- Private Methods
181 
182 
183     /***
184      * <p>Return the <code>PropertyDescriptor</code> for the specified
185      * property of the specified class.</p>
186      *
187      * @param clazz Class from which to extract a property descriptor
188      * @param name Name of the desired property
189      *
190      * @exception EvaluationException if we cannot access the introspecition
191      *  information for this class
192      * @exception PropertyNotFoundException if the specified property does
193      *  not exist on the specified class
194      */
195     private PropertyDescriptor descriptor(Class clazz, String name) {
196 
197         System.err.println("descriptor(class=" + clazz.getName() + ", name=" + name);
198         BeanInfo info = null;
199         try {
200             info = Introspector.getBeanInfo(clazz);
201             System.err.println("  Found BeanInfo " + info);
202         } catch (IntrospectionException e) {
203             throw new EvaluationException(e);
204         }
205         PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
206         for (int i = 0; i < descriptors.length; i++) {
207             if (name.equals(descriptors[i].getName())) {
208                 System.err.print("  Found PropertyDescriptor " + descriptors[i]);
209                 return descriptors[i];
210             }
211         }
212         System.err.print("  No property descriptor for property " + name);
213         throw new PropertyNotFoundException(name);
214 
215     }
216 
217 
218 }