Coverage report

  %line %branch
org.apache.commons.configuration.beanutils.ConfigurationDynaClass
87% 
100% 

 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.ArrayList;
 21  
 import java.util.List;
 22  
 
 23  
 import org.apache.commons.beanutils.DynaBean;
 24  
 import org.apache.commons.beanutils.DynaClass;
 25  
 import org.apache.commons.beanutils.DynaProperty;
 26  
 import org.apache.commons.configuration.Configuration;
 27  
 import org.apache.commons.logging.Log;
 28  
 import org.apache.commons.logging.LogFactory;
 29  
 
 30  
 /**
 31  
  * The <tt>ConfigurationDynaClass</tt> dynamically determines properties for
 32  
  * a <code>ConfigurationDynaBean</code> from a wrapped configuration-collection
 33  
  * {@link org.apache.commons.configuration.Configuration} instance.
 34  
  *
 35  
  * @author <a href="mailto:ricardo.gladwell@btinternet.com">Ricardo Gladwell</a>
 36  
  * @version $Revision$, $Date: 2005-12-05 09:23:53 +0100 (Mon, 05 Dec 2005) $
 37  
  * @since 1.0-rc1
 38  
  */
 39  27
 public class ConfigurationDynaClass implements DynaClass
 40  
 {
 41  
     /** The logger.*/
 42  3
     private static Log log = LogFactory.getLog(ConfigurationDynaClass.class);
 43  
 
 44  
     /** Stores the associated configuration.*/
 45  
     private Configuration configuration;
 46  
 
 47  
     /**
 48  
      * Construct an instance of a <code>ConfigurationDynaClass</code>
 49  
      * wrapping the specified <code>Configuration</code> instance.
 50  
      * @param configuration <code>Configuration</code> instance.
 51  
      */
 52  
     public ConfigurationDynaClass(Configuration configuration)
 53  
     {
 54  33
         super();
 55  33
         if (log.isTraceEnabled())
 56  
         {
 57  0
             log.trace("ConfigurationDynaClass(" + configuration + ")");
 58  
         }
 59  33
         this.configuration = configuration;
 60  33
     }
 61  
 
 62  
     /**
 63  
      * @see org.apache.commons.beanutils.DynaClass#getDynaProperty(java.lang.String)
 64  
      */
 65  
     public DynaProperty getDynaProperty(String name)
 66  
     {
 67  108
         if (log.isTraceEnabled())
 68  
         {
 69  0
             log.trace("getDynaProperty(" + name + ")");
 70  
         }
 71  
 
 72  108
         if (name == null)
 73  
         {
 74  3
             throw new IllegalArgumentException("No such property name=[" + name + "]");
 75  
         }
 76  
 
 77  105
         Object value = configuration.getProperty(name);
 78  105
         if (value == null)
 79  
         {
 80  3
             return null;
 81  
         }
 82  
         else
 83  
         {
 84  102
             Class type = value.getClass();
 85  
 
 86  102
             if (type == Byte.class)
 87  
             {
 88  3
                 type = Byte.TYPE;
 89  
             }
 90  102
             if (type == Character.class)
 91  
             {
 92  3
                 type = Character.TYPE;
 93  
             }
 94  99
             else if (type == Boolean.class)
 95  
             {
 96  12
                 type = Boolean.TYPE;
 97  
             }
 98  87
             else if (type == Double.class)
 99  
             {
 100  6
                 type = Double.TYPE;
 101  
             }
 102  81
             else if (type == Float.class)
 103  
             {
 104  6
                 type = Float.TYPE;
 105  
             }
 106  75
             else if (type == Integer.class)
 107  
             {
 108  9
                 type = Integer.TYPE;
 109  
             }
 110  66
             else if (type == Long.class)
 111  
             {
 112  6
                 type = Long.TYPE;
 113  
             }
 114  60
             else if (type == Short.class)
 115  
             {
 116  6
                 type = Short.TYPE;
 117  
             }
 118  
 
 119  102
             return new DynaProperty(name, type);
 120  
         }
 121  
     }
 122  
 
 123  
     /**
 124  
      * @see org.apache.commons.beanutils.DynaClass#getDynaProperties()
 125  
      */
 126  
     public DynaProperty[] getDynaProperties()
 127  
     {
 128  3
         if (log.isTraceEnabled())
 129  
         {
 130  0
             log.trace("getDynaProperties()");
 131  
         }
 132  
 
 133  3
         Iterator keys = configuration.getKeys();
 134  3
         List properties = new ArrayList();
 135  84
         while (keys.hasNext())
 136  
         {
 137  78
             String key = (String) keys.next();
 138  78
             DynaProperty property = getDynaProperty(key);
 139  78
             properties.add(property);
 140  
         }
 141  
 
 142  3
         DynaProperty[] propertyArray = new DynaProperty[properties.size()];
 143  3
         properties.toArray(propertyArray);
 144  3
         if (log.isDebugEnabled())
 145  
         {
 146  0
             log.debug("Found " + properties.size() + " properties.");
 147  
         }
 148  
 
 149  3
         return propertyArray;
 150  
     }
 151  
 
 152  
     /**
 153  
      * @see org.apache.commons.beanutils.DynaClass#getName()
 154  
      */
 155  
     public String getName()
 156  
     {
 157  0
         return ConfigurationDynaBean.class.getName();
 158  
     }
 159  
 
 160  
     /**
 161  
      * @see org.apache.commons.beanutils.DynaClass#newInstance()
 162  
      */
 163  
     public DynaBean newInstance() throws IllegalAccessException, InstantiationException
 164  
     {
 165  0
         return new ConfigurationDynaBean(configuration);
 166  
     }
 167  
 
 168  
 }

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