Coverage report

  %line %branch
org.apache.commons.configuration.beanutils.ConfigurationDynaBean
86% 
96% 

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

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