Coverage Report - org.org.usurper.springframework.UsurperGeneratorSetupFactoryBean
 
Classes in this File Line Coverage Branch Coverage Complexity
UsurperGeneratorSetupFactoryBean
82 %
85/104
94 %
30/32
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  
 package org.org.usurper.springframework;
 20  
 
 21  
 import java.util.HashMap;
 22  
 import java.util.HashSet;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 import java.util.Set;
 26  
 
 27  
 import org.org.usurper.handlers.IHandler;
 28  
 import org.org.usurper.handlers.basic.AbstractPropertyTypeHandler;
 29  
 import org.org.usurper.handlers.basic.AbstractSpecificPropertyHandler;
 30  
 import org.org.usurper.handlers.basic.ArrayHandler;
 31  
 import org.org.usurper.handlers.basic.EnumHandler;
 32  
 import org.org.usurper.model.ITargetDefinition;
 33  
 import org.org.usurper.model.PropertyTypeDefinition;
 34  
 import org.org.usurper.model.SpecificPropertyDefinition;
 35  
 import org.org.usurper.setup.ICountCallback;
 36  
 import org.org.usurper.setup.IUsurperGeneratorSetup;
 37  
 import org.org.usurper.setup.UsurperGeneratorSetup;
 38  
 import org.org.usurper.setup.constants.OnMissingHandlers;
 39  
 import org.org.usurper.setup.constants.PropertyWritingMechanism;
 40  
 import org.springframework.beans.factory.FactoryBean;
 41  
 import org.springframework.beans.factory.InitializingBean;
 42  
 
 43  
 /**
 44  
  * The Class UsurperGeneratorSetupFactoryBean is a Spring compliant FactoryBean and InitializingBean.
 45  
  * As such, it can be used as any other Spring Factory Bean.
 46  
  * It generates an UsurperGeneratorSetup.
 47  
  */
 48  
 public class UsurperGeneratorSetupFactoryBean implements FactoryBean, InitializingBean {
 49  
 
 50  
     private ArrayHandler arrayHandler;
 51  
     private ICountCallback countCallback;
 52  
     private EnumHandler enumHandler;
 53  6
     private OnMissingHandlers onMissingHandlers=null;
 54  6
     private PropertyWritingMechanism propertyWritingMechanism=null;
 55  
     private Map<PropertyTypeDefinition, AbstractPropertyTypeHandler> propertyTypeHandlersMap;
 56  
     private Map<SpecificPropertyDefinition, AbstractSpecificPropertyHandler> specificPropertyHandlersMap;
 57  
     private UsurperGeneratorSetup usurperGeneratorSetup;
 58  
     private IUsurperGeneratorSetup parentSetup;
 59  
 
 60  
     public UsurperGeneratorSetupFactoryBean() {
 61  6
         super();
 62  6
     }
 63  
 
 64  
     public Object getObject() throws Exception {
 65  8
         return usurperGeneratorSetup;
 66  
     }
 67  
 
 68  
     @SuppressWarnings("unchecked")
 69  
     public Class getObjectType() {
 70  0
         return IUsurperGeneratorSetup.class;
 71  
     }
 72  
 
 73  
     public boolean isSingleton() {
 74  6
         return false;
 75  
     }
 76  
 
 77  
     @SuppressWarnings("unchecked")
 78  
     public void afterPropertiesSet() throws Exception {
 79  6
         if (parentSetup != null) {
 80  3
             usurperGeneratorSetup = new UsurperGeneratorSetup(parentSetup);
 81  
         } else {
 82  3
             usurperGeneratorSetup = new UsurperGeneratorSetup();
 83  
         }
 84  6
         if (arrayHandler != null) {
 85  3
             usurperGeneratorSetup.setArrayHandler(arrayHandler);
 86  
         }
 87  6
         if (enumHandler != null) {
 88  3
             usurperGeneratorSetup.setArrayHandler(arrayHandler);
 89  
         }
 90  6
         if (onMissingHandlers != null) {
 91  3
             usurperGeneratorSetup.onMissingHandlers(onMissingHandlers);
 92  
         }
 93  6
         if (propertyWritingMechanism != null) {
 94  3
             usurperGeneratorSetup.usePropertyWritingMechanism(propertyWritingMechanism);
 95  
         }
 96  6
         if (countCallback != null) {
 97  3
             usurperGeneratorSetup.setCountCallback(countCallback);
 98  
         }
 99  6
         Map<ITargetDefinition, IHandler> handlers = new HashMap<ITargetDefinition, IHandler>();
 100  6
         if (propertyTypeHandlersMap != null) {
 101  4
             handlers.putAll(propertyTypeHandlersMap);
 102  
         }
 103  6
         if (specificPropertyHandlersMap != null) {
 104  4
             handlers.putAll(specificPropertyHandlersMap);
 105  
         }
 106  6
         usurperGeneratorSetup.setAllHandlers(handlers);
 107  6
     }
 108  
 
 109  
     public void setArrayHandlerName(String arrayHandler) {
 110  
         try {
 111  2
             this.arrayHandler = (ArrayHandler) Class.forName(arrayHandler).newInstance();
 112  0
         } catch (Exception e) {
 113  0
             throw new IllegalArgumentException("Impossible to instanciate class " + arrayHandler, e);
 114  2
         }
 115  2
     }
 116  
 
 117  
     public void setArrayHandler(ArrayHandler arrayHandler) {
 118  1
         this.arrayHandler = arrayHandler;
 119  1
     }
 120  
 
 121  
     public void setCountCallback(ICountCallback countCallback) {
 122  2
         this.countCallback = countCallback;
 123  2
     }
 124  
 
 125  
     public void setCountCallbackName(String countCallback) {
 126  
         try {
 127  1
             this.countCallback = (ICountCallback) Class.forName(countCallback).newInstance();
 128  0
         } catch (Exception e) {
 129  0
             throw new IllegalArgumentException("Impossible to instanciate class " + countCallback, e);
 130  1
         }
 131  1
     }
 132  
 
 133  
     public void setConstructorParameterTypes(List<String> constructorParameterTypesNames) {
 134  0
         Class<?>[] constructorParameterTypes = new Class<?>[constructorParameterTypesNames.size()];
 135  0
         int i = 0;
 136  0
         for (String constructorParameterType : constructorParameterTypesNames) {
 137  
             try {
 138  0
                 constructorParameterTypes[i++] = Class.forName(constructorParameterType);
 139  0
             } catch (Exception e) {
 140  0
                 throw new IllegalArgumentException("Impossible to find class " + constructorParameterType, e);
 141  0
             }
 142  
         }
 143  0
     }
 144  
 
 145  
     public void setEnumHandlerName(String enumHandler) {
 146  
         try {
 147  2
             this.enumHandler = (EnumHandler) Class.forName(enumHandler).newInstance();
 148  0
         } catch (Exception e) {
 149  0
             throw new IllegalArgumentException("Impossible to instanciate class " + enumHandler, e);
 150  2
         }
 151  2
     }
 152  
 
 153  
     public void setEnumHandler(EnumHandler enumHandler) {
 154  1
         this.enumHandler = enumHandler;
 155  1
     }
 156  
 
 157  
     public void setOnMissingHandlers(String onMissingHandlers) {
 158  2
         this.onMissingHandlers = OnMissingHandlers.valueOf(onMissingHandlers.toUpperCase());
 159  2
     }
 160  
 
 161  
     public void setPropertyTypeHandlers(Set<AbstractPropertyTypeHandler> propertyTypeHandlers) {
 162  4
         initPropertyTypeHandlersMap();
 163  4
         for (AbstractPropertyTypeHandler propertyTypeHandler : propertyTypeHandlers) {
 164  4
             for (PropertyTypeDefinition propertyTypeDefinition : propertyTypeHandler.getHandledTypes()) {
 165  4
                 this.propertyTypeHandlersMap.put(propertyTypeDefinition, propertyTypeHandler);
 166  
             }
 167  
         }
 168  4
     }
 169  
 
 170  
     public void setSpecificPropertyHandlers(Set<AbstractSpecificPropertyHandler> specificPropertyHandlers) {
 171  4
         initSpecificPropertyHandlersMap();
 172  4
         for (AbstractSpecificPropertyHandler specificPropertyHandler : specificPropertyHandlers) {
 173  4
             this.specificPropertyHandlersMap.put(specificPropertyHandler.getTargetProperty(), specificPropertyHandler);
 174  
         }
 175  4
     }
 176  
 
 177  
     public void setPropertyTypeHandlersClassNames(Map<Class<?>, Class<?>> propertyTypeHandlersMap) {
 178  3
         initPropertyTypeHandlersMap();
 179  3
         Map<PropertyTypeDefinition, AbstractPropertyTypeHandler> result = new HashMap<PropertyTypeDefinition, AbstractPropertyTypeHandler>();
 180  3
         for (Class<?> key : propertyTypeHandlersMap.keySet()) {
 181  
             try {
 182  2
                 Set<PropertyTypeDefinition> propertyTypeDefinitions = new HashSet<PropertyTypeDefinition>();
 183  2
                 PropertyTypeDefinition propertyTypeDefinition = new PropertyTypeDefinition(key);
 184  2
                 propertyTypeDefinitions.add(propertyTypeDefinition);
 185  2
                 AbstractPropertyTypeHandler value = (AbstractPropertyTypeHandler) propertyTypeHandlersMap.get(key).getConstructor(Set.class).newInstance(new Object[] { propertyTypeDefinitions });
 186  2
                 result.put(new PropertyTypeDefinition(key), value);
 187  0
             } catch (Exception e) {
 188  0
                 throw new IllegalArgumentException();
 189  2
             }
 190  
         }
 191  3
         this.propertyTypeHandlersMap.putAll(result);
 192  3
     }
 193  
 
 194  
     public void setSpecificPropertyHandlersClassNames(Map<String, Class<?>> specificPropertyHandlersMap) {
 195  3
         initSpecificPropertyHandlersMap();
 196  3
         Map<SpecificPropertyDefinition, AbstractSpecificPropertyHandler> result = new HashMap<SpecificPropertyDefinition, AbstractSpecificPropertyHandler>();
 197  3
         for (String key : specificPropertyHandlersMap.keySet()) {
 198  2
             SpecificPropertyDefinition specificPropertyDefinition = new SpecificPropertyDefinition(key);
 199  
             try {
 200  2
                 AbstractSpecificPropertyHandler value = (AbstractSpecificPropertyHandler) specificPropertyHandlersMap.get(key).getConstructor(SpecificPropertyDefinition.class).newInstance(specificPropertyDefinition);
 201  2
                 result.put(specificPropertyDefinition, value);
 202  0
             } catch (Exception e) {
 203  0
                 throw new IllegalArgumentException();
 204  2
             }
 205  2
         }
 206  3
         this.specificPropertyHandlersMap.putAll(result);
 207  3
     }
 208  
 
 209  
     private synchronized void initPropertyTypeHandlersMap() {
 210  7
         if(propertyTypeHandlersMap==null)propertyTypeHandlersMap = new HashMap<PropertyTypeDefinition, AbstractPropertyTypeHandler>();
 211  7
     }
 212  
     
 213  
     private synchronized void initSpecificPropertyHandlersMap() {
 214  7
         if(specificPropertyHandlersMap==null)specificPropertyHandlersMap = new HashMap<SpecificPropertyDefinition, AbstractSpecificPropertyHandler>();
 215  7
     }
 216  
     
 217  
     public void setPropertyWritingMechanism(String propertyWritingMechanism) {
 218  2
         this.propertyWritingMechanism = PropertyWritingMechanism.valueOf(propertyWritingMechanism.toUpperCase());
 219  2
     }
 220  
 
 221  
     public void onMissingHandlers(OnMissingHandlers onMissingHandlers) {
 222  1
         this.onMissingHandlers = onMissingHandlers;
 223  1
     }
 224  
 
 225  
     public void usePropertyWritingMechanism(PropertyWritingMechanism propertyWritingMechanism) {
 226  1
         this.propertyWritingMechanism = propertyWritingMechanism;
 227  1
     }
 228  
 
 229  
     public void setParentSetup(IUsurperGeneratorSetup parentSetup) {
 230  3
         this.parentSetup = parentSetup;
 231  3
     }
 232  
 
 233  
 }