Esempio n. 1
0
  @SuppressWarnings("unchecked")
  Map<?, ?> getRandomMap(final Field field) throws IllegalAccessException, BeanPopulationException {
    Class<?> fieldType = field.getType();

    Map<Object, Object> map;
    if (isInterface(field)) {
      map = (Map<Object, Object>) getEmptyTypedMap(fieldType);
    } else {
      try {
        map = (Map<Object, Object>) fieldType.newInstance();
      } catch (InstantiationException e) {
        map = (Map<Object, Object>) objenesis.newInstance(fieldType);
      }
    }

    int size = abs(aNewByteRandomizer().getRandomValue());

    Type fieldGenericType = field.getGenericType();
    Type baseKeyType = String.class;
    Type baseValueType = String.class;
    if (fieldGenericType instanceof ParameterizedType) {
      ParameterizedType parameterizedType = (ParameterizedType) fieldGenericType;
      baseKeyType = parameterizedType.getActualTypeArguments()[0];
      baseValueType = parameterizedType.getActualTypeArguments()[1];
    }
    Class<?> baseKeyTypeClass = (Class<?>) baseKeyType;
    List<?> keyItems = populator.populateBeans(baseKeyTypeClass, size);

    Class<?> baseValueTypeClass = (Class<?>) baseValueType;
    List<?> valueItems = populator.populateBeans(baseValueTypeClass, size);

    for (int index = 0; index < size; index++) {
      map.put(keyItems.get(index), valueItems.get(index));
    }
    return map;
  }
Esempio n. 2
0
 private static <T> Outline<T> buildOutline(Class<T> classToOutline, boolean camelCased) {
   ProxyFactory proxyFactory = new ProxyFactory();
   proxyFactory.setSuperclass(classToOutline);
   Class<? extends T> outlineClass = proxyFactory.createClass();
   return new OutlineImpl<>(objenesis.newInstance(outlineClass), camelCased);
 }