Coverage Report - org.org.usurper.handlers.basic.ArrayHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayHandler
79 %
22/28
67 %
8/12
0
 
 1  
 /*
 2  
  ORG Usurper is a random value object generator library 
 3  
  Copyright (C) 2007  Pierre-Antoine GrĂ©goire
 4  
  
 5  
  This library is free software; you can redistribute it and/or
 6  
  modify it under the terms of the GNU Lesser General Public
 7  
  License as published by the Free Software Foundation; either
 8  
  version 2.1 of the License, or (at your option) any later version.
 9  
  
 10  
  This library is distributed in the hope that it will be useful,
 11  
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
  Lesser General Public License for more details.
 14  
  
 15  
  You should have received a copy of the GNU Lesser General Public
 16  
  License along with this library; if not, write to the Free Software
 17  
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 18  
  */
 19  
 
 20  
 /**
 21  
  * 
 22  
  */
 23  
 package org.org.usurper.handlers.basic;
 24  
 
 25  
 import java.lang.reflect.Array;
 26  
 import java.lang.reflect.Field;
 27  
 
 28  
 import org.org.usurper.handlers.IHandler;
 29  
 import org.org.usurper.handlers.exceptions.NoHandlerDefinedException;
 30  
 import org.org.usurper.handlers.exceptions.PropertyTypeHandlingException;
 31  
 import org.org.usurper.model.HandledBeanProperty;
 32  
 import org.org.usurper.model.HandledConstructorArg;
 33  
 import org.org.usurper.model.PropertyTypeDefinition;
 34  
 import org.org.usurper.utils.ReflectionUtils;
 35  
 
 36  
 /**
 37  
  * This is the generic handler for Arrays of types.
 38  
  * 
 39  
  * @author pagregoire
 40  
  */
 41  355
 public final class ArrayHandler implements IHandler {
 42  
 
 43  
    
 44  
     /**
 45  
      * @see org.org.usurper.handlers.IHandler#handle(org.org.usurper.model.HandledBeanProperty)
 46  
      */
 47  
     public Object handle(HandledBeanProperty handledBeanProperty) {
 48  11
         Object result = null;
 49  11
         final Integer ARRAY_ENTRIES = handledBeanProperty.getUsurperGeneratorSetup().getCountCallback().determineCount(handledBeanProperty);
 50  
         try {
 51  11
             Field attribute = ReflectionUtils.getField(handledBeanProperty.getTargetObject(), handledBeanProperty.getPropertyName());
 52  11
             Class<?> usurpedClass = attribute.getType().getComponentType();
 53  11
             result = Array.newInstance(ReflectionUtils.toNotPrimitiveType(usurpedClass), ARRAY_ENTRIES);
 54  11
             HandledBeanProperty handledArrayItem = new HandledBeanProperty(handledBeanProperty.getTargetObject(), usurpedClass, handledBeanProperty.getPropertyName(), handledBeanProperty.getUsurperGeneratorSetup());
 55  121
             for (int i = 0; i < ARRAY_ENTRIES; i++) {
 56  110
                 if (handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)) != null) {
 57  100
                     Array.set(result, i, handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)).handle(handledArrayItem));
 58  
                 } else {
 59  10
                     if (usurpedClass.isEnum()) {
 60  10
                         Array.set(result, i, new EnumHandler().handle(handledArrayItem));
 61  
                     } else {
 62  0
                         throw new NoHandlerDefinedException("no handler found for Array property <" + handledArrayItem.getPropertyName() + "> of Class <" + usurpedClass.getName() + ">.");
 63  
                     }
 64  
                 }
 65  
             }
 66  0
         } catch (NoSuchFieldException e) {
 67  0
             throw new PropertyTypeHandlingException("Unable to handle property <" + handledBeanProperty.getPropertyName() + "(" + handledBeanProperty.getPropertyClass().getName() + ")> from object " + handledBeanProperty.getTargetObject(), e);
 68  11
         }
 69  11
         return result;
 70  
     }
 71  
 
 72  
     /**
 73  
      * @see org.org.usurper.handlers.IHandler#handle(org.org.usurper.model.HandledConstructorArg)
 74  
      */
 75  
     public Object handle(HandledConstructorArg handledConstructorArg) {
 76  1
         final Integer ARRAY_ENTRIES = handledConstructorArg.getUsurperGeneratorSetup().getCountCallback().determineCount(handledConstructorArg);
 77  
 
 78  1
         Class<?> usurpedClass = handledConstructorArg.getConstructorArgClass().getComponentType();
 79  1
         Object result = Array.newInstance(usurpedClass, ARRAY_ENTRIES);
 80  1
         HandledConstructorArg handledArrayItem = new HandledConstructorArg(handledConstructorArg.getTargetConstructor(), usurpedClass, handledConstructorArg.getConstructorArgOrderingNumber(), handledConstructorArg.getUsurperGeneratorSetup());
 81  11
         for (int i = 0; i < ARRAY_ENTRIES; i++) {
 82  10
             if (handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)) != null) {
 83  10
                 Array.set(result, i, handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)).handle(handledArrayItem));
 84  
             } else {
 85  0
                 if (usurpedClass.isEnum()) {
 86  0
                     Array.set(result, i, new EnumHandler().handle(handledArrayItem));
 87  
                 } else {
 88  0
                     throw new NoHandlerDefinedException("no handler found for Array constructor arg <#" + handledArrayItem.getConstructorArgOrderingNumber() + "> of Class <" + usurpedClass.getName() + ">.");
 89  
                 }
 90  
             }
 91  
         }
 92  1
         return result;
 93  
     }
 94  
 
 95  
 }