Coverage Report - org.org.usurper.handlers.defaults.ListAndSetPropertyTypeHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
ListAndSetPropertyTypeHandler
86 %
38/44
70 %
14/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.util.ArrayList;
 27  
 import java.util.Collection;
 28  
 import java.util.HashSet;
 29  
 import java.util.List;
 30  
 import java.util.Set;
 31  
 
 32  
 import org.org.usurper.handlers.basic.AbstractPropertyTypeHandler;
 33  
 import org.org.usurper.handlers.exceptions.NoHandlerDefinedException;
 34  
 import org.org.usurper.handlers.exceptions.PropertyTypeHandlingException;
 35  
 import org.org.usurper.model.HandledBeanProperty;
 36  
 import org.org.usurper.model.HandledConstructorArg;
 37  
 import org.org.usurper.model.PropertyTypeDefinition;
 38  
 import org.org.usurper.utils.ReflectionUtils;
 39  
 
 40  
 /**
 41  
  * This handler deals with Lists and Sets of Types.<br>
 42  
  * These Lists and Sets must be generic types (Usurper cannot generate Lists of objects without knowing about the type to be generated) and must be declared using the List and Set interface.<br>
 43  
  * This handler then delegates to the handlers defined in the generator setup for the proper generation.<br>
 44  
  * It also delegates to the CountCallback defined in the generator setup to determine the number of elements the List or Set will contain.
 45  
  * 
 46  
  * @author pagregoire
 47  
  */
 48  
 public class ListAndSetPropertyTypeHandler extends AbstractPropertyTypeHandler {
 49  
 
 50  1
     private final static Integer NUMBER_OF_TYPE_ARGUMENTS = 1;
 51  
 
 52  
     private static Set<PropertyTypeDefinition> handledTypes;
 53  
 
 54  
     static {
 55  1
         handledTypes = new HashSet<PropertyTypeDefinition>();
 56  1
         handledTypes.add(new PropertyTypeDefinition(List.class));
 57  1
         handledTypes.add(new PropertyTypeDefinition(Set.class));
 58  1
     }
 59  
 
 60  
     /**
 61  
      * Instantiates a new list and set property type handler.
 62  
      */
 63  
     public ListAndSetPropertyTypeHandler() {
 64  1
         super(handledTypes);
 65  1
     }
 66  
 
 67  
     /**
 68  
      * @see org.org.usurper.handlers.basic.AbstractPropertyTypeHandler#handle()
 69  
      */
 70  
     @SuppressWarnings("unchecked")
 71  
     public Object handle(HandledBeanProperty handledBeanProperty) {
 72  7
         Collection result = null;
 73  7
         final Integer COLLECTION_ENTRIES = handledBeanProperty.getUsurperGeneratorSetup().getCountCallback().determineCount(handledBeanProperty);
 74  
         try {
 75  7
             Field attribute = ReflectionUtils.getField(handledBeanProperty.getTargetObject(), handledBeanProperty.getPropertyName());
 76  7
             Class<?> usurpedClass = Object.class;
 77  7
             Class<?> entityClass = attribute.getType();
 78  7
             usurpedClass = (Class) ReflectionUtils.getGenericTypes(attribute)[NUMBER_OF_TYPE_ARGUMENTS - 1];
 79  7
             if (entityClass.isAssignableFrom(List.class)) {
 80  4
                 result = new ArrayList(COLLECTION_ENTRIES);
 81  3
             } else if (entityClass.isAssignableFrom(Set.class)) {
 82  3
                 result = new HashSet(COLLECTION_ENTRIES);
 83  
             }
 84  7
             HandledBeanProperty handledCollectionItem = new HandledBeanProperty(handledBeanProperty.getTargetObject(), usurpedClass, handledBeanProperty.getPropertyName(), handledBeanProperty.getUsurperGeneratorSetup());
 85  77
             for (int i = 0; i < COLLECTION_ENTRIES; i++) {
 86  70
                 if (handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)) != null) {
 87  50
                     result.add(handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)).handle(handledCollectionItem));
 88  
                 } else {
 89  20
                     if (usurpedClass.isEnum()) {
 90  20
                         result.add(handledBeanProperty.getUsurperGeneratorSetup().getEnumHandler().handle(handledCollectionItem));
 91  
                     } else {
 92  0
                         throw new NoHandlerDefinedException("no handler found for Class <" + new PropertyTypeDefinition(usurpedClass) + "> of List / Set.");
 93  
                     }
 94  
                 }
 95  
             }
 96  0
         } catch (NoSuchFieldException e) {
 97  0
             throw new PropertyTypeHandlingException("Unable to handle field <" + handledBeanProperty.getPropertyName() + "(" + handledBeanProperty.getPropertyClass().getName() + ")> from object " + handledBeanProperty.getTargetObject(), e);
 98  7
         }
 99  7
         return result;
 100  
     }
 101  
 
 102  
     /**
 103  
      * @see org.org.usurper.handlers.basic.AbstractPropertyTypeHandler#handle()
 104  
      */
 105  
     @SuppressWarnings("unchecked")
 106  
     public Object handle(HandledConstructorArg handledConstructorArg) {
 107  3
         Collection result = null;
 108  3
         final Integer COLLECTION_ENTRIES = handledConstructorArg.getUsurperGeneratorSetup().getCountCallback().determineCount(handledConstructorArg);
 109  3
         Class<?> collectionClass = handledConstructorArg.getConstructorArgClass();
 110  3
         Class<?> usurpedClass = (Class<?>) ReflectionUtils.getGenericTypes(handledConstructorArg.getTargetConstructor(), handledConstructorArg.getConstructorArgOrderingNumber())[NUMBER_OF_TYPE_ARGUMENTS - 1];
 111  3
         if (collectionClass.isAssignableFrom(List.class)) {
 112  2
             result = new ArrayList(COLLECTION_ENTRIES);
 113  1
         } else if (collectionClass.isAssignableFrom(Set.class)) {
 114  1
             result = new HashSet(COLLECTION_ENTRIES);
 115  
         }
 116  3
         HandledConstructorArg handledCollectionItem = new HandledConstructorArg(handledConstructorArg.getTargetConstructor(), usurpedClass, handledConstructorArg.getConstructorArgOrderingNumber(), handledConstructorArg.getUsurperGeneratorSetup());
 117  33
         for (int i = 0; i < COLLECTION_ENTRIES; i++) {
 118  30
             if (handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)) != null) {
 119  30
                 result.add(handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)).handle(handledCollectionItem));
 120  
             } else {
 121  0
                 if (usurpedClass.isEnum()) {
 122  0
                     result.add(handledConstructorArg.getUsurperGeneratorSetup().getEnumHandler().handle(handledCollectionItem));
 123  
                 } else {
 124  0
                     throw new NoHandlerDefinedException("no handler found for Class <" + usurpedClass.getName() + "> of List / Set.");
 125  
                 }
 126  
             }
 127  
         }
 128  3
         return result;
 129  
     }
 130  
 
 131  
 }