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