Search in sources :

Example 6 with ConstructionFailedException

use of org.evosuite.ga.ConstructionFailedException in project evosuite by EvoSuite.

the class TestFactory method addFieldAssignment.

/**
 * Add method at given position if max recursion depth has not been reached
 *
 * @param test
 * @param field
 * @param position
 * @param recursionDepth
 * @return
 * @throws ConstructionFailedException
 */
public VariableReference addFieldAssignment(TestCase test, GenericField field, int position, int recursionDepth) throws ConstructionFailedException {
    logger.debug("Recursion depth: " + recursionDepth);
    if (recursionDepth > Properties.MAX_RECURSION) {
        logger.debug("Max recursion depth reached");
        throw new ConstructionFailedException("Max recursion depth reached");
    }
    logger.debug("Adding field " + field);
    int length = test.size();
    VariableReference callee = null;
    if (!field.isStatic()) {
        callee = createOrReuseVariable(test, field.getOwnerType(), position, recursionDepth, null, false, false, false);
        position += test.size() - length;
        length = test.size();
        if (!TestUsageChecker.canUse(field.getField(), callee.getVariableClass())) {
            logger.debug("Cannot call field " + field + " with callee of type " + callee.getClassName());
            throw new ConstructionFailedException("Cannot apply field to this callee");
        }
    }
    VariableReference var = createOrReuseVariable(test, field.getFieldType(), position, recursionDepth, callee, true, false, false);
    int newLength = test.size();
    position += (newLength - length);
    FieldReference f = new FieldReference(test, field, callee);
    if (f.equals(var))
        throw new ConstructionFailedException("Self assignment");
    Statement st = new AssignmentStatement(test, f, var);
    VariableReference ret = test.addStatement(st, position);
    // logger.info("FIeld assignment: " + st.getCode());
    assert (test.isValid());
    return ret;
}
Also used : PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 7 with ConstructionFailedException

use of org.evosuite.ga.ConstructionFailedException in project evosuite by EvoSuite.

the class TestFactory method addConstructor.

/**
 * Add constructor at given position if max recursion depth has not been
 * reached
 *
 * @param constructor
 * @param position
 * @param recursionDepth
 * @return
 * @throws ConstructionFailedException
 */
public VariableReference addConstructor(TestCase test, GenericConstructor constructor, Type exactType, int position, int recursionDepth) throws ConstructionFailedException {
    if (recursionDepth > Properties.MAX_RECURSION) {
        logger.debug("Max recursion depth reached");
        throw new ConstructionFailedException("Max recursion depth reached");
    }
    Class<?> klass = constructor.getRawGeneratedType();
    if (Properties.JEE && InstanceOnlyOnce.canInstantiateOnlyOnce(klass) && ConstraintHelper.countNumberOfNewInstances(test, klass) != 0) {
        throw new ConstructionFailedException("Class " + klass.getName() + " can only be instantiated once");
    }
    int length = test.size();
    try {
        // first be sure if parameters can be satisfied
        List<VariableReference> parameters = satisfyParameters(test, null, Arrays.asList(constructor.getParameterTypes()), Arrays.asList(constructor.getConstructor().getParameters()), position, recursionDepth + 1, true, false, true);
        int newLength = test.size();
        position += (newLength - length);
        // create a statement for the constructor
        Statement st = new ConstructorStatement(test, constructor, parameters);
        VariableReference ref = test.addStatement(st, position);
        if (Properties.JEE) {
            int injectPosition = doInjection(test, position, klass, ref, recursionDepth);
            if (Properties.HANDLE_SERVLETS) {
                if (HttpServlet.class.isAssignableFrom(klass)) {
                    // Servlets are treated specially, as part of JEE
                    if (ConstraintHelper.countNumberOfMethodCalls(test, EvoServletState.class, "initServlet") == 0) {
                        Statement ms = new MethodStatement(test, ServletSupport.getServletInit(), null, Arrays.asList(ref));
                        test.addStatement(ms, injectPosition++);
                    }
                }
            }
        }
        return ref;
    } catch (Exception e) {
        throw new ConstructionFailedException("Failed to add constructor for " + klass.getName() + " due to " + e.getClass().getCanonicalName() + ": " + e.getMessage());
    }
}
Also used : PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) EvoServletState(org.evosuite.runtime.javaee.javax.servlet.EvoServletState) PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 8 with ConstructionFailedException

use of org.evosuite.ga.ConstructionFailedException in project evosuite by EvoSuite.

the class TestFactory method addField.

/**
 * Add a field to the test case
 *
 * @param test
 * @param field
 * @param position
 * @return
 * @throws ConstructionFailedException
 */
public VariableReference addField(TestCase test, GenericField field, int position, int recursionDepth) throws ConstructionFailedException {
    logger.debug("Adding field {}", field);
    if (recursionDepth > Properties.MAX_RECURSION) {
        logger.debug("Max recursion depth reached");
        throw new ConstructionFailedException("Max recursion depth reached");
    }
    VariableReference callee = null;
    int length = test.size();
    if (!field.isStatic()) {
        callee = createOrReuseVariable(test, field.getOwnerType(), position, recursionDepth, null, false, false, false);
        position += test.size() - length;
        if (!TestUsageChecker.canUse(field.getField(), callee.getVariableClass())) {
            logger.debug("Cannot call field {} with callee of type {}", field, callee.getClassName());
            throw new ConstructionFailedException("Cannot apply field to this callee");
        }
        // TODO: Check if field is still accessible in subclass
        if (!field.getOwnerClass().equals(callee.getGenericClass())) {
            try {
                if (!TestUsageChecker.canUse(callee.getVariableClass().getField(field.getName()))) {
                    throw new ConstructionFailedException("Cannot access field in subclass");
                }
            } catch (NoSuchFieldException fe) {
                throw new ConstructionFailedException("Cannot access field in subclass");
            }
        }
    }
    Statement st = new FieldStatement(test, field, callee);
    return test.addStatement(st, position);
}
Also used : PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 9 with ConstructionFailedException

use of org.evosuite.ga.ConstructionFailedException in project evosuite by EvoSuite.

the class TestFactory method addMethod.

/**
 * Add method at given position if max recursion depth has not been reached
 *
 * @param test
 * @param method
 * @param position
 * @param recursionDepth
 * @return
 * @throws ConstructionFailedException
 */
public VariableReference addMethod(TestCase test, GenericMethod method, int position, int recursionDepth) throws ConstructionFailedException {
    logger.debug("Recursion depth: " + recursionDepth);
    if (recursionDepth > Properties.MAX_RECURSION) {
        logger.debug("Max recursion depth reached");
        throw new ConstructionFailedException("Max recursion depth reached");
    }
    logger.debug("Adding method " + method);
    int length = test.size();
    VariableReference callee = null;
    List<VariableReference> parameters = null;
    try {
        if (!method.isStatic()) {
            callee = createOrReuseVariable(test, method.getOwnerType(), position, recursionDepth, null, false, false, false);
            // a functional mock can never be a callee
            assert !(test.getStatement(callee.getStPosition()) instanceof FunctionalMockStatement);
            position += test.size() - length;
            length = test.size();
            logger.debug("Found callee of type " + method.getOwnerType() + ": " + callee.getName());
            if (!TestUsageChecker.canUse(method.getMethod(), callee.getVariableClass())) {
                logger.debug("Cannot call method " + method + " with callee of type " + callee.getClassName());
                throw new ConstructionFailedException("Cannot apply method to this callee");
            }
        }
        // Added 'null' as additional parameter - fix for @NotNull annotations issue on evo mailing list
        parameters = satisfyParameters(test, callee, Arrays.asList(method.getParameterTypes()), Arrays.asList(method.getMethod().getParameters()), position, recursionDepth + 1, true, false, true);
    } catch (ConstructionFailedException e) {
        // TestCluster.getInstance().checkDependencies(method);
        throw e;
    }
    int newLength = test.size();
    position += (newLength - length);
    Statement st = new MethodStatement(test, method, callee, parameters);
    VariableReference ret = test.addStatement(st, position);
    if (callee != null)
        ret.setDistance(callee.getDistance() + 1);
    return ret;
}
Also used : PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 10 with ConstructionFailedException

use of org.evosuite.ga.ConstructionFailedException in project evosuite by EvoSuite.

the class TestFactory method insertRandomCallOnEnvironment.

/**
 * @param test
 * @param lastValidPosition
 * @return the position where the insertion happened, or a negative value otherwise
 */
public int insertRandomCallOnEnvironment(TestCase test, int lastValidPosition) {
    int previousLength = test.size();
    currentRecursion.clear();
    List<GenericAccessibleObject<?>> shuffledOptions = TestCluster.getInstance().getRandomizedCallsToEnvironment();
    if (shuffledOptions == null || shuffledOptions.isEmpty()) {
        return -1;
    }
    // iterate (in random order) over all possible environment methods till we find one that can be inserted
    for (GenericAccessibleObject<?> o : shuffledOptions) {
        try {
            int position = ConstraintVerifier.getAValidPositionForInsertion(o, test, lastValidPosition);
            if (position < 0) {
                // the given method/constructor cannot be added
                continue;
            }
            if (o.isConstructor()) {
                GenericConstructor c = (GenericConstructor) o;
                addConstructor(test, c, position, 0);
                return position;
            } else if (o.isMethod()) {
                GenericMethod m = (GenericMethod) o;
                if (!m.isStatic()) {
                    VariableReference callee = null;
                    Type target = m.getOwnerType();
                    if (!test.hasObject(target, position)) {
                        callee = createObject(test, target, position, 0, null);
                        position += test.size() - previousLength;
                        previousLength = test.size();
                    } else {
                        callee = test.getRandomNonNullObject(target, position);
                    }
                    if (!TestUsageChecker.canUse(m.getMethod(), callee.getVariableClass())) {
                        logger.error("Cannot call method " + m + " with callee of type " + callee.getClassName());
                    }
                    addMethodFor(test, callee, m.copyWithNewOwner(callee.getGenericClass()), position);
                    return position;
                } else {
                    addMethod(test, m, position, 0);
                    return position;
                }
            } else {
                throw new RuntimeException("Unrecognized type for environment: " + o);
            }
        } catch (ConstructionFailedException e) {
            // TODO what to do here?
            AtMostOnceLogger.warn(logger, "Failed environment insertion: " + e);
        }
    }
    return -1;
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) CaptureType(com.googlecode.gentyref.CaptureType) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Aggregations

ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)43 GenericClass (org.evosuite.utils.generic.GenericClass)16 PrivateFieldStatement (org.evosuite.testcase.statements.reflection.PrivateFieldStatement)14 PrivateMethodStatement (org.evosuite.testcase.statements.reflection.PrivateMethodStatement)14 CaptureType (com.googlecode.gentyref.CaptureType)11 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)11 GenericMethod (org.evosuite.utils.generic.GenericMethod)10 ArrayList (java.util.ArrayList)8 GenericField (org.evosuite.utils.generic.GenericField)7 TestFactory (org.evosuite.testcase.TestFactory)6 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)6 TestCase (org.evosuite.testcase.TestCase)4 VariableReference (org.evosuite.testcase.variable.VariableReference)4 Type (java.lang.reflect.Type)3 GenericArrayType (java.lang.reflect.GenericArrayType)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 TypeVariable (java.lang.reflect.TypeVariable)2 WildcardType (java.lang.reflect.WildcardType)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2