Coverage Report - nl.beesting.beangenerator.generator.StringInstanceGenerator
 
Classes in this File Line Coverage Branch Coverage Complexity
StringInstanceGenerator
75%
30/40
64%
9/14
0
 
 1  
 package nl.beesting.beangenerator.generator;
 2  
 
 3  
 import java.lang.reflect.Constructor;
 4  
 import nl.beesting.beangenerator.TypeNotSupportedException;
 5  
 import nl.beesting.beangenerator.util.ParameterDescriptor;
 6  
 import nl.beesting.beangenerator.util.ParameterPair;
 7  
 import nl.beesting.beangenerator.util.RandomUtil;
 8  
 import org.apache.commons.logging.Log;
 9  
 import org.apache.commons.logging.LogFactory;
 10  
 
 11  
 /**
 12  
  * Instance generator for String types: <code>Character</code>, <code>String</code> and <code>StringBuffer</code>
 13  
  */
 14  
 public class StringInstanceGenerator extends AbstractInstanceGenerator {
 15  
 
 16  
    /** The <code>logger</code> for this class. */
 17  1
    private static final Log logger = LogFactory.getLog(PhoneticalNameGenerator.class);
 18  
    private static final int DEFAULT_LENGTH = 30;
 19  
    private static final String LENGTH_PARAMETER = "length";
 20  2
    private int length = DEFAULT_LENGTH;
 21  
    private int from;
 22  
    private int until;
 23  
 
 24  2
    public StringInstanceGenerator() {
 25  
       // set the suppported types
 26  2
       addSupportedTypes(new Class[]{String.class, StringBuffer.class});
 27  
 
 28  
       // set the supported parameters
 29  2
       setSupportedParameterDescriptors(new ParameterDescriptor[]{
 30  
          new ParameterDescriptor(LENGTH_PARAMETER, String.class, "Fixed length of generated text."),
 31  
          new ParameterDescriptor(FROM_PARAMETER, String.class, "From. variable length left boundry."),
 32  
          new ParameterDescriptor(UNTIL_PARAMETER, String.class, "Until. variable length right boundry.")
 33  
       });
 34  2
    }
 35  
 
 36  
    /**
 37  
     * Initializes the instnce generator with the given array of objects.
 38  
     */
 39  
    public void init(ParameterPair[] initParams) {
 40  5
       if (initParams != null) {
 41  4
          length = initParameter(initParams, LENGTH_PARAMETER, DEFAULT_LENGTH);
 42  4
          from = initParameter(initParams, FROM_PARAMETER, 0);
 43  4
          until = initParameter(initParams, UNTIL_PARAMETER, 0);
 44  
       }
 45  5
    }
 46  
 
 47  
    /**
 48  
     * Create an instance for the given class. The returned object is of the given class.
 49  
     */
 50  
    public Object generateInstance(Class<?> type) throws TypeNotSupportedException {
 51  
       // preconditions
 52  4000
       if (type == null) {
 53  0
          throw new IllegalArgumentException("Input parameter type cannot be null.");
 54  
       }
 55  4000
       if (!checkSupport(type)) {
 56  0
          throw new TypeNotSupportedException(type.getName(), "generateInstance");
 57  
       }
 58  
 
 59  4000
       if (type.equals(Character.class)) {
 60  0
          return instantiateCharacter();
 61  
       }
 62  4000
       String result = instantiateString();
 63  
 
 64  
       try {
 65  4000
          Constructor<?> c = type.getConstructor(String.class);
 66  4000
          return c.newInstance(result);
 67  0
       } catch (Exception e) {
 68  0
          logger.warn("Could not construct Object of type " + type + " . Returning null.", e);
 69  
       }
 70  
 
 71  0
       return null;
 72  
    }
 73  
 
 74  
    /**
 75  
     * Create an instance for the given class with an instance generator specific modus. The returned object is of the given
 76  
     * class.
 77  
     */
 78  
    public Object generateInstance(Class<?> type, int modus) throws TypeNotSupportedException {
 79  0
       if (!checkSupport(type)) {
 80  0
          throw new TypeNotSupportedException(type.getName(), "generateInstance");
 81  
       }
 82  0
       return generateInstance(type);
 83  
    }
 84  
 
 85  
    // ///////////// INSTANCE METHODS ////////////////
 86  
    /**
 87  
     * Instantiates random Character
 88  
     * @return
 89  
     */
 90  
    private Character instantiateCharacter() {
 91  0
       return RandomUtil.initRandomChar();
 92  
    }
 93  
 
 94  
    /**
 95  
     * Instantiates random String value.
 96  
     * @return
 97  
     */
 98  
    private String instantiateString() {
 99  4000
       int stringSize = 0;
 100  4000
       if (from == 0) {
 101  2000
          stringSize = length;
 102  
       } else {
 103  2000
          stringSize = RandomUtil.initRandomIntBetween(from, until);
 104  
       }
 105  
 
 106  4000
       StringBuffer returnStringBuf = new StringBuffer();
 107  71929
       for (int i = 0; i < stringSize; i++) {
 108  67929
          char randomChar = RandomUtil.initRandomChar();
 109  67929
          returnStringBuf.append(randomChar);
 110  
       }
 111  4000
       return returnStringBuf.toString();
 112  
    }
 113  
 
 114  
    /* For testing */
 115  
    protected Object getFromValue() {
 116  4005
       return from;
 117  
    }
 118  
 
 119  
    protected Object getUntilValue() {
 120  4005
       return until;
 121  
    }
 122  
 
 123  
    protected Object getDefaultFromValue() {
 124  2
       return 0;
 125  
    }
 126  
 
 127  
    protected Object getDefaultUntilValue() {
 128  2
       return 0;
 129  
    }
 130  
 }