Search in sources :

Example 36 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestCaseExpander method visitArrayStatement.

public void visitArrayStatement(TestCase test, ArrayStatement statement) {
    ArrayReference arrRef = (ArrayReference) statement.getReturnValue();
    Set<Integer> assignments = new HashSet<Integer>();
    int position = statement.getPosition() + 1;
    while (position < test.size()) {
        Statement st = test.getStatement(position);
        if (st instanceof AssignmentStatement) {
            if (st.getReturnValue() instanceof ArrayIndex) {
                ArrayIndex arrayIndex = (ArrayIndex) st.getReturnValue();
                if (arrayIndex.getArray().equals(arrRef)) {
                    assignments.add(arrayIndex.getArrayIndex());
                }
            }
        } else if (st instanceof PrimitiveStatement) {
        // OK, ignore
        } else {
            break;
        }
        position++;
    }
    position = statement.getPosition() + 1;
    for (int i = 0; i < statement.size(); i++) {
        if (assignments.contains(i))
            continue;
        ArrayIndex index = new ArrayIndex(test, arrRef, i);
        VariableReference retVal = null;
        if (index.isPrimitive()) {
            PrimitiveStatement<?> primitive = PrimitiveStatement.getPrimitiveStatement(test, index.getGenericClass());
            retVal = test.addStatement(primitive, position++);
        } else {
            NullStatement nullStatement = new NullStatement(test, index.getType());
            retVal = test.addStatement(nullStatement, position++);
        }
        AssignmentStatement assignment = new AssignmentStatement(test, index, retVal);
        test.addStatement(assignment, position++);
    }
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) HashSet(java.util.HashSet)

Example 37 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestCaseExpander method visitMethodStatement.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.evosuite.testcase.TestVisitor#visitMethodStatement(org.evosuite
	 * .testcase.MethodStatement)
	 */
public void visitMethodStatement(TestCase test, MethodStatement statement) {
    // The problem is that at this point in the test case the parameters
    // might have already changed
    int i = 0;
    for (VariableReference var : statement.getParameterReferences()) {
        if (var.isPrimitive() || var.isString()) {
            if (usedVariables.contains(var) && test.getStatement(var.getStPosition()) instanceof PrimitiveStatement<?>) {
                // Duplicate and replace
                VariableReference varCopy = duplicateStatement(test, var);
                statement.replaceParameterReference(varCopy, i);
                usedVariables.add(varCopy);
            }
            usedVariables.add(var);
        }
        i++;
    }
    addUnchangedMapping(test, statement.getReturnValue());
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference)

Example 38 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestCarvingExecutionObserver method afterStatement.

/**
 * own comment..
 */
@Override
public void afterStatement(final Statement statement, final Scope scope, final Throwable exception) {
    if (statement instanceof AssignmentStatement) {
        final AssignmentStatement assign = (AssignmentStatement) statement;
        final VariableReference left = assign.getReturnValue();
        if (left instanceof FieldReference) {
            final FieldReference fieldRef = (FieldReference) left;
            final GenericField field = fieldRef.getField();
            FieldRegistry.notifyModification(field.isStatic() ? null : scope.getObject(fieldRef.getSource()), this.captureId, Type.getInternalName(field.getDeclaringClass()), field.getName(), Type.getDescriptor(field.getField().getType()));
            // PUTFIELDRegistry creates PUTXXX as well as corresponding GETXXX statements
            this.captureId -= 2;
        }
    }
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) GenericField(org.evosuite.utils.generic.GenericField)

Example 39 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class ConstantInliner method afterStatement.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.evosuite.testcase.ExecutionObserver#statement(org.evosuite.testcase.
	 * StatementInterface, org.evosuite.testcase.Scope, java.lang.Throwable)
	 */
/**
 * {@inheritDoc}
 */
@Override
public void afterStatement(Statement statement, Scope scope, Throwable exception) {
    try {
        for (VariableReference var : statement.getVariableReferences()) {
            if (var.equals(statement.getReturnValue()) || var.equals(statement.getReturnValue().getAdditionalVariableReference()))
                continue;
            Object object = var.getObject(scope);
            if (var.isPrimitive()) {
                ConstantValue value = new ConstantValue(test, var.getGenericClass());
                value.setValue(object);
                // logger.info("Statement before inlining: " +
                // statement.getCode());
                statement.replace(var, value);
            // logger.info("Statement after inlining: " +
            // statement.getCode());
            } else if (var.isString() && object != null) {
                ConstantValue value = new ConstantValue(test, var.getGenericClass());
                try {
                    String val = StringEscapeUtils.unescapeJava(new String(object.toString()));
                    if (val.length() < Properties.MAX_STRING) {
                        value.setValue(val);
                        statement.replace(var, value);
                    }
                } catch (IllegalArgumentException e) {
                    // Exceptions may happen if strings are not valid
                    // unicode
                    logger.info("Cannot escape invalid string: " + object);
                }
            // logger.info("Statement after inlining: " +
            // statement.getCode());
            } else if (var.isArrayIndex()) {
                // then replace the array index with that object
                for (VariableReference otherVar : scope.getElements(var.getType())) {
                    Object otherObject = otherVar.getObject(scope);
                    if (otherObject == object && !otherVar.isArrayIndex() && otherVar.getStPosition() < statement.getPosition()) {
                        statement.replace(var, otherVar);
                        break;
                    }
                }
            } else {
                // the assertion for now
                if (object == null) {
                    if (statement instanceof MethodStatement) {
                        MethodStatement ms = (MethodStatement) statement;
                        if (var.equals(ms.getCallee())) {
                            // Don't put null in callee's, the compiler will not accept it
                            continue;
                        }
                    } else if (statement instanceof FieldStatement) {
                        FieldStatement fs = (FieldStatement) statement;
                        if (var.equals(fs.getSource())) {
                            // Don't put null in source, the compiler will not accept it
                            continue;
                        }
                    }
                    ConstantValue value = new ConstantValue(test, var.getGenericClass());
                    value.setValue(null);
                    // logger.info("Statement before inlining: " +
                    // statement.getCode());
                    statement.replace(var, value);
                // logger.info("Statement after inlining: " +
                // statement.getCode());
                }
            }
        }
    } catch (CodeUnderTestException e) {
        logger.warn("Not inlining test: " + e.getCause());
    // throw new AssertionError("This case isn't handled yet: " +
    // e.getCause()
    // + ", " + Arrays.asList(e.getStackTrace()));
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) FieldStatement(org.evosuite.testcase.statements.FieldStatement) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) ConstantValue(org.evosuite.testcase.variable.ConstantValue)

Example 40 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class ConstraintVerifier method checkInitializingBoundedVariable.

private static boolean checkInitializingBoundedVariable(TestCase tc, int i, VariableReference vr) {
    for (int j = i - 1; j > vr.getStPosition(); j--) {
        Statement st = tc.getStatement(j);
        MethodStatement ms = null;
        ConstructorStatement cs = null;
        Annotation[][] annotations = null;
        List<VariableReference> inputs = null;
        if (st instanceof MethodStatement) {
            ms = (MethodStatement) st;
            annotations = ms.getMethod().getMethod().getParameterAnnotations();
            inputs = ms.getParameterReferences();
            // is any other method of the bounded variable been called?
            VariableReference callee = ms.getCallee();
            if (vr.same(callee)) {
                logger.error("Invalid method call at position " + j + " on bounded variable created in " + vr.getStPosition() + " " + "and initialized in " + i + "\nTest case code:\n" + tc.toCode());
                return false;
            }
        }
        if (st instanceof ConstructorStatement) {
            cs = (ConstructorStatement) st;
            annotations = cs.getConstructor().getConstructor().getParameterAnnotations();
            inputs = cs.getParameterReferences();
        }
        if (inputs == null || inputs.isEmpty()) {
            continue;
        }
        // is the bounded variable used as input in another method?
        outer: for (int k = 0; k < inputs.size(); k++) {
            VariableReference input = inputs.get(k);
            Annotation[] varAnns = annotations[k];
            for (Annotation ann : varAnns) {
                if (ann instanceof BoundInputVariable) {
                    // it is fine if bounded variable is used several methods (eg injectors for each field)
                    continue outer;
                }
            }
            if (vr.same(input)) {
                logger.error("Bounded variable of type " + vr.getType() + " created at position " + vr.getStPosition() + " is used as input in " + j + " before its bounding initializer at position " + i + ". Statement at position " + j + " is:\n" + tc.getStatement(j).getCode() + "\nTest case code:\n" + tc.toCode());
                return false;
            }
        }
    }
    Statement declaration = tc.getStatement(vr.getStPosition());
    if (!(declaration instanceof ConstructorStatement)) {
        logger.error("Bounded variable is declared in " + vr.getStPosition() + " but not with a 'new' constructor." + "Statement:\n" + declaration + "\nTest code:\n" + tc.toCode());
        return false;
    }
    return true;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) Constraint(org.evosuite.symbolic.expr.Constraint) Annotation(java.lang.annotation.Annotation)

Aggregations

VariableReference (org.evosuite.testcase.variable.VariableReference)472 Method (java.lang.reflect.Method)289 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)143 Test (org.junit.Test)73 GenericMethod (org.evosuite.utils.generic.GenericMethod)68 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)55 MethodStatement (org.evosuite.testcase.statements.MethodStatement)44 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)38 ArrayList (java.util.ArrayList)31 GenericClass (org.evosuite.utils.generic.GenericClass)27 TestCase (org.evosuite.testcase.TestCase)26 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)26 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)25 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)19 Type (java.lang.reflect.Type)17 Statement (org.evosuite.testcase.statements.Statement)17 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)15 ArrayReference (org.evosuite.testcase.variable.ArrayReference)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)15 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)14