Coverage Report - org.org.usurper.handlers.defaults.MapPropertyTypeHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
MapPropertyTypeHandler
83 %
49/59
60 %
12/20
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.defaults;
 24  
 
 25  
 import java.lang.reflect.Field;
 26  
 import java.lang.reflect.Type;
 27  
 import java.util.HashSet;
 28  
 import java.util.LinkedHashMap;
 29  
 import java.util.Map;
 30  
 import java.util.Set;
 31  
 
 32  
 import org.org.usurper.handlers.basic.AbstractPropertyTypeHandler;
 33  
 import org.org.usurper.handlers.basic.EnumHandler;
 34  
 import org.org.usurper.handlers.exceptions.NoHandlerDefinedException;
 35  
 import org.org.usurper.handlers.exceptions.PropertyTypeHandlingException;
 36  
 import org.org.usurper.model.HandledBeanProperty;
 37  
 import org.org.usurper.model.HandledConstructorArg;
 38  
 import org.org.usurper.model.PropertyTypeDefinition;
 39  
 import org.org.usurper.utils.ReflectionUtils;
 40  
 
 41  
 /**
 42  
  * This handler deals with Map Types.<br>
 43  
  * The Map must be a generic type (Usurper cannot generate Maps of objects without knowing about the types to be generated) and must be declared using the Map interface.<br>
 44  
  * This handler then delegates to the handlers defined in the generator setup for the proper generation.<br>
 45  
  * It also delegates to the CountCallback defined in the generator setup to determine the number of key/value pairs the Map will contain.
 46  
  * 
 47  
  * @author pagregoire
 48  
  */
 49  
 public class MapPropertyTypeHandler extends AbstractPropertyTypeHandler {
 50  
 
 51  1
     private final static Integer KEY_TYPE_ARGUMENTS_INDEX = 0;
 52  
 
 53  1
     private final static Integer VALUE_TYPE_ARGUMENTS_INDEX = 1;
 54  
 
 55  
     private static Set<PropertyTypeDefinition> handledTypes;
 56  
 
 57  
     static {
 58  1
         handledTypes = new HashSet<PropertyTypeDefinition>();
 59  1
         handledTypes.add(new PropertyTypeDefinition(Map.class));
 60  1
     }
 61  
 
 62  
     /**
 63  
      * Instantiates a new map property type handler.
 64  
      */
 65  
     public MapPropertyTypeHandler() {
 66  1
         super(handledTypes);
 67  1
     }
 68  
 
 69  
     /**
 70  
      * @see org.org.usurper.handlers.basic.AbstractPropertyTypeHandler#handle()
 71  
      */
 72  
     @SuppressWarnings("unchecked")
 73  
     public Object handle(HandledBeanProperty handledBeanProperty) {
 74  3
         Map result = null;
 75  3
         final Integer MAP_ENTRIES = handledBeanProperty.getUsurperGeneratorSetup().getCountCallback().determineCount(handledBeanProperty);
 76  
         try {
 77  3
             Field attribute = ReflectionUtils.getField(handledBeanProperty.getTargetObject(), handledBeanProperty.getPropertyName());
 78  3
             Type[] usurpedClasses = ReflectionUtils.getGenericTypes(attribute);
 79  3
             Class keyGenericType = (Class) usurpedClasses[KEY_TYPE_ARGUMENTS_INDEX];
 80  3
             Class valueGenericType = (Class) usurpedClasses[VALUE_TYPE_ARGUMENTS_INDEX];
 81  3
             int entryNumber = MAP_ENTRIES;
 82  3
             result = new LinkedHashMap(entryNumber);
 83  3
             HandledBeanProperty handledMapKeyItem = new HandledBeanProperty(handledBeanProperty.getTargetObject(), keyGenericType, handledBeanProperty.getPropertyName(), handledBeanProperty.getUsurperGeneratorSetup());
 84  3
             HandledBeanProperty handledMapValueItem = new HandledBeanProperty(handledBeanProperty.getTargetObject(), valueGenericType, handledBeanProperty.getPropertyName(), handledBeanProperty.getUsurperGeneratorSetup());
 85  33
             for (int i = 0; i < entryNumber; i++) {
 86  30
                 Object key = null;
 87  30
                 Object value = null;
 88  30
                 if (handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(keyGenericType)) != null) {
 89  20
                     key = handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(keyGenericType)).handle(handledMapKeyItem);
 90  
                 } else {
 91  10
                     if (keyGenericType.isEnum()) {
 92  10
                         key = new EnumHandler().handle(handledMapKeyItem);
 93  
                     } else {
 94  0
                         throw new NoHandlerDefinedException("no handler found for Map attribute <" + attribute.getName() + "> of Class <" + keyGenericType.getName() + ">.");
 95  
                     }
 96  
                 }
 97  30
                 if (handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(valueGenericType)) != null) {
 98  20
                     value = handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(valueGenericType)).handle(handledMapValueItem);
 99  
                 } else {
 100  10
                     if (valueGenericType.isEnum()) {
 101  10
                         value = new EnumHandler().handle(handledMapValueItem);
 102  
                     } else {
 103  0
                         throw new NoHandlerDefinedException("no handler found for Map attribute <" + attribute.getName() + "> of Class <" + valueGenericType.getName() + ">.");
 104  
                     }
 105  
                 }
 106  30
                 result.put(key, value);
 107  
             }
 108  0
         } catch (NoSuchFieldException e) {
 109  0
             throw new PropertyTypeHandlingException("Unable to handle field <" + handledBeanProperty.getPropertyName() + "(" + handledBeanProperty.getPropertyClass().getName() + ")> from object " + handledBeanProperty.getTargetObject(), e);
 110  3
         }
 111  3
         return result;
 112  
     }
 113  
 
 114  
     /**
 115  
      * @see org.org.usurper.handlers.basic.AbstractPropertyTypeHandler#handle()
 116  
      */
 117  
     @SuppressWarnings("unchecked")
 118  
     public Object handle(HandledConstructorArg handledConstructorArg) {
 119  1
         Map result = null;
 120  1
         final Integer MAP_ENTRIES = handledConstructorArg.getUsurperGeneratorSetup().getCountCallback().determineCount(handledConstructorArg);
 121  1
         Type[] usurpedClasses = ReflectionUtils.getGenericTypes(handledConstructorArg.getTargetConstructor(), handledConstructorArg.getConstructorArgOrderingNumber());
 122  1
         Class keyGenericType = (Class) usurpedClasses[KEY_TYPE_ARGUMENTS_INDEX];
 123  1
         Class valueGenericType = (Class) usurpedClasses[VALUE_TYPE_ARGUMENTS_INDEX];
 124  1
         int entryNumber = MAP_ENTRIES;
 125  1
         result = new LinkedHashMap(entryNumber);
 126  1
         HandledConstructorArg handledMapKeyItem = new HandledConstructorArg(handledConstructorArg.getTargetConstructor(), keyGenericType, handledConstructorArg.getConstructorArgOrderingNumber(), handledConstructorArg.getUsurperGeneratorSetup());
 127  1
         HandledConstructorArg handledMapValueItem = new HandledConstructorArg(handledConstructorArg.getTargetConstructor(), valueGenericType, handledConstructorArg.getConstructorArgOrderingNumber(), handledConstructorArg.getUsurperGeneratorSetup());
 128  11
         for (int i = 0; i < entryNumber; i++) {
 129  10
             Object key = null;
 130  10
             Object value = null;
 131  10
             if (handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(keyGenericType)) != null) {
 132  10
                 key = handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(keyGenericType)).handle(handledMapKeyItem);
 133  
             } else {
 134  0
                 if (keyGenericType.isEnum()) {
 135  0
                     key = new EnumHandler().handle(handledMapKeyItem);
 136  
                 } else {
 137  0
                     throw new NoHandlerDefinedException("no handler found for Map Key's Class <" + new PropertyTypeDefinition(keyGenericType) + ">.");
 138  
                 }
 139  
             }
 140  10
             if (handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(valueGenericType)) != null) {
 141  10
                 value = handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(valueGenericType)).handle(handledMapValueItem);
 142  
             } else {
 143  0
                 if (valueGenericType.isEnum()) {
 144  0
                     value = new EnumHandler().handle(handledMapValueItem);
 145  
                 } else {
 146  0
                     throw new NoHandlerDefinedException("no handler found for Map Value's Class  <" + new PropertyTypeDefinition(valueGenericType) + ">.");
 147  
                 }
 148  
             }
 149  10
             result.put(key, value);
 150  
         }
 151  
 
 152  1
         return result;
 153  
     }
 154  
 }