Search in sources :

Example 6 with Constraints

use of org.evosuite.runtime.annotation.Constraints in project evosuite by EvoSuite.

the class TestFactory method addMethodFor.

/**
 * Add a call on the method for the given callee at position
 *
 * @param test
 * @param callee
 * @param method
 * @param position
 * @return
 * @throws ConstructionFailedException
 */
public VariableReference addMethodFor(TestCase test, VariableReference callee, GenericMethod method, int position) throws ConstructionFailedException {
    logger.debug("Adding method {} for {} (Generating {})", method, callee, method.getGeneratedClass());
    if (position <= callee.getStPosition()) {
        throw new ConstructionFailedException("Cannot insert call on object before the object is defined");
    }
    currentRecursion.clear();
    int length = test.size();
    boolean allowNull = true;
    Constraints constraints = method.getMethod().getAnnotation(Constraints.class);
    if (constraints != null && constraints.noNullInputs()) {
        allowNull = false;
    }
    // Added 'null' as additional parameter - fix for @NotNull annotations issue on evo mailing list
    List<VariableReference> parameters = satisfyParameters(test, callee, Arrays.asList(method.getParameterTypes()), Arrays.asList(method.getMethod().getParameters()), position, 1, allowNull, false, true);
    int newLength = test.size();
    position += (newLength - length);
    Statement st = new MethodStatement(test, method, callee, parameters);
    VariableReference ret = test.addStatement(st, position);
    ret.setDistance(callee.getDistance() + 1);
    logger.debug("Success: Adding method {}", method);
    return ret;
}
Also used : PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) Constraints(org.evosuite.runtime.annotation.Constraints) PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 7 with Constraints

use of org.evosuite.runtime.annotation.Constraints in project evosuite by EvoSuite.

the class Injector method validateBean.

/**
 * Check if all beans that need injection have been properly injected
 *
 * @param instance
 * @param clazz
 * @param <T>
 * @throws FalsePositiveException
 * @throws IllegalArgumentException
 */
@Constraints(noNullInputs = true, notMutable = true, noDirectInsertion = true)
public static <T> void validateBean(@BoundInputVariable(initializer = true, atMostOnceWithSameParameters = true) T instance, Class<?> clazz) throws FalsePositiveException, IllegalArgumentException {
    Inputs.checkNull(instance, clazz);
    for (Field f : getAllFieldsToInject(clazz)) {
        f.setAccessible(true);
        try {
            Object obj = f.get(instance);
            if (obj == null) {
                throw new FalsePositiveException("Missing dependency injection for field " + f.getName() + " in class " + clazz.getName());
            }
        // it might be a bean with its own dependency injections.
        // but those should be handled in its instantiation
        } catch (IllegalAccessException e) {
            // shouldn't really happen
            logger.warn(e.toString());
        }
    }
    Class<?> parent = clazz.getSuperclass();
    if (parent != null && !parent.equals(Object.class)) {
        validateBean(instance, parent);
    }
}
Also used : FalsePositiveException(org.evosuite.runtime.FalsePositiveException) Field(java.lang.reflect.Field) Constraints(org.evosuite.runtime.annotation.Constraints)

Example 8 with Constraints

use of org.evosuite.runtime.annotation.Constraints in project evosuite by EvoSuite.

the class Injector method injectEvent.

@Constraints(noNullInputs = true, notMutable = true, noDirectInsertion = true)
public static <T> void injectEvent(@BoundInputVariable(initializer = true, atMostOnceWithSameParameters = true) T instance, Class<?> clazz) throws IllegalArgumentException {
    Inputs.checkNull(instance, clazz);
    String field = eventCache.getFieldName(clazz);
    assert field != null;
    // TODO this will likely need to change in the future
    inject(instance, clazz, field, new EvoEvent());
}
Also used : EvoEvent(org.evosuite.runtime.javaee.javax.enterprise.event.EvoEvent) Constraints(org.evosuite.runtime.annotation.Constraints)

Example 9 with Constraints

use of org.evosuite.runtime.annotation.Constraints in project evosuite by EvoSuite.

the class Injector method injectUserTransaction.

@Constraints(noNullInputs = true, notMutable = true, noDirectInsertion = true)
public static <T> void injectUserTransaction(@BoundInputVariable(initializer = true, atMostOnceWithSameParameters = true) T instance, Class<?> clazz) throws IllegalArgumentException {
    Inputs.checkNull(instance, clazz);
    String field = userTransactionCache.getFieldName(clazz);
    assert field != null;
    // TODO this will likely need to change in the future
    inject(instance, clazz, field, new EvoUserTransaction());
}
Also used : EvoUserTransaction(org.evosuite.runtime.javaee.javax.transaction.EvoUserTransaction) Constraints(org.evosuite.runtime.annotation.Constraints)

Aggregations

Constraints (org.evosuite.runtime.annotation.Constraints)9 VariableReference (org.evosuite.testcase.variable.VariableReference)3 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)1 FalsePositiveException (org.evosuite.runtime.FalsePositiveException)1 EvoEvent (org.evosuite.runtime.javaee.javax.enterprise.event.EvoEvent)1 EvoUserTransaction (org.evosuite.runtime.javaee.javax.transaction.EvoUserTransaction)1 PrivateFieldStatement (org.evosuite.testcase.statements.reflection.PrivateFieldStatement)1 PrivateMethodStatement (org.evosuite.testcase.statements.reflection.PrivateMethodStatement)1 NullReference (org.evosuite.testcase.variable.NullReference)1 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)1