Coverage Report - org.seasar.teeda.core.mock.MockPropertyResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
MockPropertyResolver
47%
43/92
35%
35/100
7.3
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.teeda.core.mock;
 17  
 
 18  
 import java.lang.reflect.Array;
 19  
 import java.util.Iterator;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import javax.faces.component.UIComponent;
 24  
 import javax.faces.el.EvaluationException;
 25  
 import javax.faces.el.PropertyNotFoundException;
 26  
 import javax.faces.el.PropertyResolver;
 27  
 import javax.faces.el.ReferenceSyntaxException;
 28  
 
 29  
 import org.seasar.framework.beans.BeanDesc;
 30  
 import org.seasar.framework.beans.PropertyDesc;
 31  
 import org.seasar.framework.beans.factory.BeanDescFactory;
 32  
 
 33  1350
 public class MockPropertyResolver extends PropertyResolver {
 34  
 
 35  
     public Object getValue(Object base, Object property)
 36  
             throws EvaluationException, PropertyNotFoundException {
 37  
 
 38  35
         if (base == null || property == null || property instanceof String
 39  
                 && ((String) property).length() == 0) {
 40  
 
 41  0
             return null;
 42  
         }
 43  35
         if (base instanceof Map) {
 44  9
             return ((Map) base).get(property);
 45  
         }
 46  26
         if (base instanceof UIComponent) {
 47  0
             for (Iterator children = ((UIComponent) base).getChildren()
 48  0
                     .iterator(); children.hasNext();) {
 49  0
                 UIComponent child = (UIComponent) children.next();
 50  0
                 if (property.equals(child.getId())) {
 51  0
                     return child;
 52  
                 }
 53  
             }
 54  0
             return null;
 55  
         }
 56  26
         return getProperty(base, property.toString());
 57  
     }
 58  
 
 59  
     public Object getValue(Object base, int index) throws EvaluationException,
 60  
             PropertyNotFoundException {
 61  
 
 62  3
         if (base == null) {
 63  0
             return null;
 64  
         }
 65  
         try {
 66  3
             if (base.getClass().isArray()) {
 67  0
                 return Array.get(base, index);
 68  
             }
 69  3
             if (base instanceof List) {
 70  3
                 return ((List) base).get(index);
 71  
             }
 72  0
             if (base instanceof UIComponent) {
 73  0
                 return ((UIComponent) base).getChildren().get(index);
 74  
             }
 75  0
         } catch (IndexOutOfBoundsException e) {
 76  0
             return null;
 77  0
         }
 78  0
         throw new ReferenceSyntaxException("Must be array or List. Bean: "
 79  
                 + base.getClass().getName() + ", index " + index);
 80  
     }
 81  
 
 82  
     public void setValue(Object base, Object property, Object newValue)
 83  
             throws EvaluationException, PropertyNotFoundException {
 84  
 
 85  5
         if (base == null || property == null || property instanceof String
 86  
                 && ((String) property).length() == 0) {
 87  
 
 88  0
             return;
 89  
         }
 90  5
         if (base instanceof Map) {
 91  0
             ((Map) base).put(property, newValue);
 92  
 
 93  0
             return;
 94  
         }
 95  5
         if (base instanceof UIComponent) {
 96  0
             throw new PropertyNotFoundException(
 97  
                     "Bean must not be UIComponent, property: " + property);
 98  
         }
 99  5
         setProperty(base, property.toString(), newValue);
 100  5
     }
 101  
 
 102  
     public void setValue(Object base, int index, Object newValue)
 103  
             throws EvaluationException, PropertyNotFoundException {
 104  
 
 105  1
         if (base == null) {
 106  0
             return;
 107  
         }
 108  1
         if (base.getClass().isArray()) {
 109  0
             Array.set(base, index, newValue);
 110  
 
 111  0
             return;
 112  
         }
 113  1
         if (base instanceof List) {
 114  1
             ((List) base).set(index, newValue);
 115  1
             return;
 116  
         }
 117  0
         throw new EvaluationException("Bean must be array or List. Bean: "
 118  
                 + base.getClass().getName() + ", index " + index);
 119  
     }
 120  
 
 121  
     public boolean isReadOnly(Object base, Object property) {
 122  2
         if (base == null || property == null || property instanceof String
 123  
                 && ((String) property).length() == 0) {
 124  
 
 125  0
             return false;
 126  
         }
 127  2
         if (base instanceof Map) {
 128  0
             return false;
 129  
         }
 130  2
         if (base instanceof UIComponent) {
 131  0
             return true;
 132  
         }
 133  2
         BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass());
 134  2
         String propertyName = property.toString();
 135  2
         if (!beanDesc.hasPropertyDesc(propertyName)) {
 136  0
             return false;
 137  
         }
 138  2
         PropertyDesc pd = beanDesc.getPropertyDesc(propertyName);
 139  2
         return !pd.isWritable();
 140  
     }
 141  
 
 142  
     public boolean isReadOnly(Object base, int index) {
 143  0
         if (base == null) {
 144  0
             return false;
 145  
         }
 146  0
         if (base instanceof List || base.getClass().isArray()) {
 147  0
             return false;
 148  
         }
 149  0
         if (base instanceof UIComponent) {
 150  0
             return true;
 151  
         }
 152  0
         return false;
 153  
     }
 154  
 
 155  
     public Class getType(Object base, Object property) {
 156  1
         if (base == null || property == null || property instanceof String
 157  
                 && ((String) property).length() == 0) {
 158  
 
 159  0
             return null;
 160  
         }
 161  1
         if (base instanceof Map) {
 162  0
             Object value = ((Map) base).get(property);
 163  0
             return (value != null) ? value.getClass() : Object.class;
 164  
         }
 165  1
         if (base instanceof UIComponent) {
 166  0
             return UIComponent.class;
 167  
         }
 168  1
         BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass());
 169  1
         String propertyName = property.toString();
 170  1
         if (!beanDesc.hasPropertyDesc(propertyName)) {
 171  0
             return null;
 172  
         }
 173  1
         PropertyDesc pd = beanDesc.getPropertyDesc(propertyName);
 174  1
         return pd.getPropertyType();
 175  
     }
 176  
 
 177  
     public Class getType(Object base, int index) {
 178  0
         if (base == null) {
 179  0
             return null;
 180  
         }
 181  0
         if (base.getClass().isArray()) {
 182  0
             return base.getClass().getComponentType();
 183  
         }
 184  0
         if (base instanceof List) {
 185  0
             Object value = ((List) base).get(index);
 186  0
             return (value != null) ? value.getClass() : Object.class;
 187  
         }
 188  0
         if (base instanceof UIComponent) {
 189  0
             return UIComponent.class;
 190  
         }
 191  0
         return null;
 192  
     }
 193  
 
 194  
     protected void setProperty(Object base, String name, Object newValue) {
 195  5
         BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass());
 196  5
         PropertyDesc pd = beanDesc.getPropertyDesc(name);
 197  5
         pd.setValue(base, newValue);
 198  5
     }
 199  
 
 200  
     protected Object getProperty(Object base, String name) {
 201  26
         BeanDesc beanDesc = BeanDescFactory.getBeanDesc(base.getClass());
 202  26
         PropertyDesc pd = beanDesc.getPropertyDesc(name);
 203  26
         return pd.getValue(base);
 204  
     }
 205  
 }