Example #1
0
 private void fulfillParametersFromAnnotations(
     Class<?> testClass,
     Map<Field, List<? extends Object>> parameters,
     List<Field> unsatisfied,
     Annotation... annotations) {
   for (Annotation annotation : annotations) {
     Use[] useAnnotations = null;
     if (annotation.annotationType() == Uses.class) {
       useAnnotations = ((Uses) annotation).value();
     }
     if (annotation.annotationType() == Use.class) {
       useAnnotations = new Use[] {(Use) annotation};
     }
     if (useAnnotations != null) {
       for (Use useAnnotation : useAnnotations) {
         for (Field field : parameters.keySet()) {
           if (field.getName().equals(useAnnotation.field())) {
             parameters.put(field, getUseParameter(testClass, field.getType(), useAnnotation));
             unsatisfied.remove(field);
           }
         }
       }
     }
     if (annotation.annotationType() == Templates.class) {
       Templates templatesAnnotation = (Templates) annotation;
       parameters.put(
           templatesField,
           getTemplatesParameter(
               testClass, templatesField.getType(), templatesAnnotation, parameters));
     }
   }
 }
Example #2
0
  private List<? extends Object> getUseParameter(
      Class<?> testClass, Class<?> parameterType, Use useAnnotation) {

    List<Object> result = new LinkedList<Object>();

    if (useAnnotation.empty()) {
      result.addAll(Arrays.asList(new Object[] {null}));
    }

    if (useAnnotation.ints().length > 0) {
      if (parameterType == int.class || parameterType == Integer.class) {
        result.addAll(Arrays.asList(ArrayUtils.toObject(useAnnotation.ints())));
      }
    }

    if (useAnnotation.decimals().length > 0) {
      if (parameterType == double.class || parameterType == Double.class) {
        result.addAll(Arrays.asList(ArrayUtils.toObject(useAnnotation.decimals())));
      }
    }

    if (useAnnotation.strings().length > 0) {
      if (parameterType == String.class) {
        result.addAll(Arrays.asList(useAnnotation.strings()));
      }
    }

    if (useAnnotation.booleans().length > 0) {
      if (parameterType == boolean.class || parameterType == Boolean.class) {
        result.addAll(Arrays.asList(ArrayUtils.toObject(useAnnotation.booleans())));
      }
    }

    if (useAnnotation.enumeration()) {
      if (!parameterType.isEnum()) {
        throw new IllegalArgumentException(parameterType + "have to be enumeration");
      }

      result.addAll(Arrays.asList(parameterType.getEnumConstants()));
    }

    // tries satisfy parameter from fields
    if (result.isEmpty()) {
      Object testInstance = getTestInstance(testClass);
      for (int i = 0; i < useAnnotation.value().length; i++) {
        boolean satisfied = false;
        String namePattern = useAnnotation.value()[i];
        namePattern = StringUtils.replace(namePattern, "*", ".+");
        namePattern = StringUtils.replace(namePattern, "?", ".");

        for (Field field : getAllFields(testClass)) {
          Pattern pattern = Pattern.compile(namePattern);
          if (pattern.matcher(field.getName()).matches()) {
            boolean isArray = field.getType().isArray();
            Class<?> representedType = field.getType();
            if (!parameterType.isAssignableFrom(representedType) && isArray) {
              representedType = field.getType().getComponentType();
            } else {
              isArray = false;
            }
            if (parameterType.isAssignableFrom(representedType)) {
              Object[] assignments = getDeclaredFieldValues(testInstance, field, isArray);
              for (Object assignment : assignments) {
                result.add(assignment);
              }
              satisfied = true;
            } else {
              throw new IllegalStateException(
                  "cannot satisfy parameter with declared field '" + field.getName() + "'");
            }
          }
        }
        if (satisfied) {
          continue;
        }
        throw new IllegalStateException(
            "cannot find the field satysfying injection point with name pattern: "
                + useAnnotation.value()[i]);
      }
    }

    if (useAnnotation.useNull()) {
      if (parameterType.isPrimitive()) {
        throw new IllegalArgumentException("parameterType is primitive, can't use null value");
      }

      if (result.contains(null)) {
        result.addAll(null);
      }
    }

    return result;
  }