Coverage report

  %line %branch
org.apache.commons.configuration.beanutils.ConfigurationDynaBean
84% 
85% 

 1  
 /*
 2  
  * Copyright 2001-2005 The Apache Software Foundation.
 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, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.apache.commons.configuration.beanutils;
 18  
 
 19  
 import java.util.Iterator;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.commons.beanutils.DynaBean;
 23  
 import org.apache.commons.beanutils.DynaClass;
 24  
 import org.apache.commons.configuration.Configuration;
 25  
 import org.apache.commons.configuration.ConfigurationMap;
 26  
 import org.apache.commons.configuration.ConversionException;
 27  
 import org.apache.commons.lang.BooleanUtils;
 28  
 import org.apache.commons.logging.Log;
 29  
 import org.apache.commons.logging.LogFactory;
 30  
 
 31  
 /**
 32  
  * The <tt>ConfigurationDynaBean</tt> dynamically reads and writes
 33  
  * configurations properties from a wrapped configuration-collection
 34  
  * {@link org.apache.commons.configuration.Configuration} instance. It also
 35  
  * implements a {@link java.util.Map} interface so that it can be used in
 36  
  * JSP 2.0 Expression Language expressions.
 37  
  *
 38  
  * <p>The <code>ConfigurationDynaBean</code> maps nested and mapped properties
 39  
  * to the appropriate <code>Configuration</code> subset using the
 40  
  * {@link org.apache.commons.configuration.Configuration#subset}
 41  
  * method. Similarly, indexed properties reference lists of configuration
 42  
  * properties using the
 43  
  * {@link org.apache.commons.configuration.Configuration#getList(String)}
 44  
  * method. Setting an indexed property always throws an exception.</p>
 45  
  *
 46  
  * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
 47  
  * @version $Revision$, $Date: 2005-11-14 18:09:48 +0100 (Mon, 14 Nov 2005) $
 48  
  * @since 1.0-rc1
 49  
  */
 50  3
 public class ConfigurationDynaBean extends ConfigurationMap implements DynaBean
 51  
 {
 52  
     /** The logger.*/
 53  3
     private static Log log = LogFactory.getLog(ConfigurationDynaBean.class);
 54  
 
 55  
     /**
 56  
      * Creates a new instance of <code>ConfigurationDynaBean</code> and sets
 57  
      * the configuration this bean is associated with.
 58  
      * @param configuration the configuration
 59  
      */
 60  
     public ConfigurationDynaBean(Configuration configuration)
 61  
     {
 62  117
         super(configuration);
 63  117
         if (log.isTraceEnabled())
 64  
         {
 65  0
             log.trace("ConfigurationDynaBean(" + configuration + ")");
 66  
         }
 67  117
     }
 68  
 
 69  
     /**
 70  
      * @see org.apache.commons.beanutils.DynaBean#set(java.lang.String, java.lang.Object)
 71  
      */
 72  
     public void set(String name, Object value)
 73  
     {
 74  1167
         if (log.isTraceEnabled())
 75  
         {
 76  0
             log.trace("set(" + name + "," + value + ")");
 77  
         }
 78  
 
 79  1167
         if (value == null)
 80  
         {
 81  3
             throw new NullPointerException("Error trying to set property to null.");
 82  
         }
 83  
 
 84  1164
         if (value instanceof List)
 85  
         {
 86  114
             List list = (List) value;
 87  114
             Iterator iterator = list.iterator();
 88  798
             while (iterator.hasNext())
 89  
             {
 90  570
                 getConfiguration().addProperty(name, iterator.next());
 91  
             }
 92  
         }
 93  1050
         else if (value instanceof int[])
 94  
         {
 95  114
             int[] array = (class="keyword">int[]) value;
 96  684
             for (int i = 0; i < array.length; i++)
 97  
             {
 98  570
                 getConfiguration().addProperty(name, new Integer(array[i]));
 99  
             }
 100  
         }
 101  936
         else if (value instanceof boolean[])
 102  
         {
 103  114
             boolean[] array = (class="keyword">boolean[]) value;
 104  684
             for (int i = 0; i < array.length; i++)
 105  
             {
 106  570
                 getConfiguration().addProperty(name, BooleanUtils.toBooleanObject(array[i]));
 107  
             }
 108  
         }
 109  822
         else if (value instanceof char[])
 110  
         {
 111  114
             char[] array = (class="keyword">char[]) value;
 112  684
             for (int i = 0; i < array.length; i++)
 113  
             {
 114  570
                 getConfiguration().addProperty(name, new Character(array[i]));
 115  
             }
 116  
         }
 117  708
         else if (value instanceof byte[])
 118  
         {
 119  114
             byte[] array = (byte[]) value;
 120  684
             for (int i = 0; i < array.length; i++)
 121  
             {
 122  570
                 getConfiguration().addProperty(name, new Byte(array[i]));
 123  
             }
 124  
         }
 125  594
         else if (value instanceof short[])
 126  
         {
 127  114
             short[] array = (class="keyword">short[]) value;
 128  684
             for (int i = 0; i < array.length; i++)
 129  
             {
 130  570
                 getConfiguration().addProperty(name, new Short(array[i]));
 131  
             }
 132  
         }
 133  480
         else if (value instanceof long[])
 134  
         {
 135  114
             long[] array = (class="keyword">long[]) value;
 136  684
             for (int i = 0; i < array.length; i++)
 137  
             {
 138  570
                 getConfiguration().addProperty(name, new Long(array[i]));
 139  
             }
 140  
         }
 141  366
         else if (value instanceof float[])
 142  
         {
 143  114
             float[] array = (class="keyword">float[]) value;
 144  684
             for (int i = 0; i < array.length; i++)
 145  
             {
 146  570
                 getConfiguration().addProperty(name, new Float(array[i]));
 147  
             }
 148  
         }
 149  252
         else if (value instanceof double[])
 150  
         {
 151  114
             double[] array = (class="keyword">double[]) value;
 152  684
             for (int i = 0; i < array.length; i++)
 153  
             {
 154  570
                 getConfiguration().addProperty(name, new Double(array[i]));
 155  
             }
 156  
         }
 157  138
         else if (value instanceof Object[])
 158  
         {
 159  114
             Object[] array = (Object[]) value;
 160  684
             for (int i = 0; i < array.length; i++)
 161  
             {
 162  570
                 getConfiguration().addProperty(name, array[i]);
 163  
             }
 164  
         }
 165  
         else
 166  
         {
 167  24
             getConfiguration().setProperty(name, value);
 168  
         }
 169  1164
     }
 170  
 
 171  
     /**
 172  
      * @see org.apache.commons.beanutils.DynaBean#get(java.lang.String)
 173  
      */
 174  
     public Object get(String name)
 175  
     {
 176  78
         if (log.isTraceEnabled())
 177  
         {
 178  0
             log.trace("get(" + name + ")");
 179  
         }
 180  
 
 181  
         // get configuration property
 182  78
         Object result = getConfiguration().getProperty(name);
 183  78
         if (result == null)
 184  
         {
 185  
             // otherwise attempt to create bean from configuration subset
 186  9
             Configuration subset = getConfiguration().subset(name);
 187  9
             if (!subset.isEmpty())
 188  
             {
 189  3
                 result = new ConfigurationDynaBean(getConfiguration().subset(name));
 190  
             }
 191  
         }
 192  
 
 193  78
         if (log.isDebugEnabled())
 194  
         {
 195  0
             log.debug(name + "=[" + result + "]");
 196  
         }
 197  
 
 198  78
         if (result == null)
 199  
         {
 200  6
             throw new IllegalArgumentException("Property '" + name + "' does not exist.");
 201  
         }
 202  72
         return result;
 203  
     }
 204  
 
 205  
     /**
 206  
      * @see org.apache.commons.beanutils.DynaBean#contains(java.lang.String, java.lang.String)
 207  
      */
 208  
     public boolean contains(String name, String key)
 209  
     {
 210  18
         Configuration subset = getConfiguration().subset(name);
 211  18
         if (subset == null)
 212  
         {
 213  0
             throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
 214  
         }
 215  
 
 216  18
         return subset.containsKey(key);
 217  
     }
 218  
 
 219  
     /**
 220  
      * @see org.apache.commons.beanutils.DynaBean#get(java.lang.String, int)
 221  
      */
 222  
     public Object get(String name, int index)
 223  
     {
 224  
         try
 225  
         {
 226  96
             List list = getConfiguration().getList(name);
 227  93
             if (list.isEmpty())
 228  
             {
 229  0
                 throw new IllegalArgumentException("Indexed property '" + name + "' does not exist.");
 230  
             }
 231  
 
 232  93
             return list.get(index);
 233  
         }
 234  
         catch (ConversionException e)
 235  
         {
 236  3
             throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
 237  
         }
 238  
     }
 239  
 
 240  
     /**
 241  
      * @see org.apache.commons.beanutils.DynaBean#get(java.lang.String, java.lang.String)
 242  
      */
 243  
     public Object get(String name, String key)
 244  
     {
 245  18
         Configuration subset = getConfiguration().subset(name);
 246  18
         if (subset == null)
 247  
         {
 248  0
             throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
 249  
         }
 250  
 
 251  18
         return subset.getProperty(key);
 252  
     }
 253  
 
 254  
     /**
 255  
      * @see org.apache.commons.beanutils.DynaBean#getDynaClass()
 256  
      */
 257  
     public DynaClass getDynaClass()
 258  
     {
 259  33
         return new ConfigurationDynaClass(getConfiguration());
 260  
     }
 261  
 
 262  
     /**
 263  
      * @see org.apache.commons.beanutils.DynaBean#remove(java.lang.String, java.lang.String)
 264  
      */
 265  
     public void remove(String name, String key)
 266  
     {
 267  6
         Configuration subset = getConfiguration().subset(name);
 268  6
         if (subset == null)
 269  
         {
 270  0
             throw new IllegalArgumentException("Mapped property '" + name + "' does not exist.");
 271  
         }
 272  6
         subset.setProperty(key, null);
 273  6
     }
 274  
 
 275  
     /**
 276  
      * @see org.apache.commons.beanutils.DynaBean#set(java.lang.String, int, java.lang.Object)
 277  
      */
 278  
     public void set(String name, int index, Object value)
 279  
     {
 280  
         try
 281  
         {
 282  18
             Object property = getConfiguration().getProperty(name);
 283  
 
 284  18
             if (property == null)
 285  
             {
 286  0
                 throw new IllegalArgumentException("Property '" + name + "' does not exist.");
 287  
             }
 288  18
             else if (property instanceof List)
 289  
             {
 290  18
                 List list = (List) property;
 291  18
                 list.set(index, value);
 292  
             }
 293  0
             else if (property.getClass().isArray())
 294  
             {
 295  0
                 Object[] array = (Object[]) property;
 296  0
                 array[index] = value;
 297  
             }
 298  0
             else if (index == 0)
 299  
             {
 300  0
                 getConfiguration().setProperty(name, value);
 301  
             }
 302  
             else
 303  
             {
 304  0
                 throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
 305  
             }
 306  15
         }
 307  
         catch (ConversionException e)
 308  
         {
 309  0
             throw new IllegalArgumentException("Property '" + name + "' is not indexed.");
 310  
         }
 311  15
     }
 312  
 
 313  
     /**
 314  
      * @see org.apache.commons.beanutils.DynaBean#set(java.lang.String, java.lang.String, java.lang.Object)
 315  
      */
 316  
     public void set(String name, String key, Object value)
 317  
     {
 318  6
         getConfiguration().setProperty(name + "." + key, value);
 319  6
     }
 320  
 
 321  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.