Search in sources :

Example 1 with CodeUnderTestException

use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.

the class NullPointerExceptionContract method check.

/* (non-Javadoc)
	 * @see org.evosuite.contracts.Contract#check(org.evosuite.testcase.TestCase, org.evosuite.testcase.Statement, org.evosuite.testcase.Scope, java.lang.Throwable)
	 */
/**
 * {@inheritDoc}
 */
@Override
public ContractViolation check(Statement statement, Scope scope, Throwable exception) {
    if (!isTargetStatement(statement))
        return null;
    try {
        if (exception != null) {
            // method throws no NullPointerException if no input parameter was null
            if (exception instanceof NullPointerException) {
                StackTraceElement element = exception.getStackTrace()[0];
                // If the exception was thrown in the test directly, it is also not interesting
                if (element.getClassName().startsWith(PackageInfo.getEvoSuitePackage() + ".testcase")) {
                    return null;
                }
                List<VariableReference> parameters = new ArrayList<VariableReference>();
                if (statement instanceof MethodStatement) {
                    MethodStatement ms = (MethodStatement) statement;
                    parameters.addAll(ms.getParameterReferences());
                } else if (statement instanceof ConstructorStatement) {
                    ConstructorStatement cs = (ConstructorStatement) statement;
                    parameters.addAll(cs.getParameterReferences());
                } else {
                    return null;
                }
                boolean hasNull = false;
                for (VariableReference var : parameters) {
                    if (var.getObject(scope) == null) {
                        hasNull = true;
                        break;
                    }
                }
                if (!hasNull) {
                    return new ContractViolation(this, statement, exception);
                }
            }
        }
        return null;
    } catch (CodeUnderTestException e) {
        throw new UnsupportedOperationException();
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayList(java.util.ArrayList) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Example 2 with CodeUnderTestException

use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.

the class PrimitiveFieldTraceObserver method visit.

/* (non-Javadoc)
	 * @see org.evosuite.assertion.AssertionTraceObserver#visit(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, org.evosuite.testcase.VariableReference)
	 */
/**
 * {@inheritDoc}
 */
@Override
protected void visit(Statement statement, Scope scope, VariableReference var) {
    logger.debug("Checking fields of " + var);
    try {
        if (var == null)
            return;
        if (statement.isAssignmentStatement()) {
            if (statement.getReturnValue().isArrayIndex()) {
                return;
            }
            if (statement.getReturnValue().isFieldReference()) {
                return;
            }
        }
        Object object = var.getObject(scope);
        int position = statement.getPosition();
        if (object != null && !object.getClass().isPrimitive() && !object.getClass().isEnum() && !isWrapperType(object.getClass())) {
            PrimitiveFieldTraceEntry entry = new PrimitiveFieldTraceEntry(var);
            for (Field field : var.getVariableClass().getFields()) {
                // TODO Check for wrapper types
                if (Modifier.isPublic(field.getModifiers()) && !field.getType().equals(void.class) && field.getType().isPrimitive() && !Modifier.isFinal(field.getModifiers()) && !field.isSynthetic()) {
                    try {
                        logger.debug("Keeping field " + field + " with value " + field.get(object));
                        entry.addValue(field, field.get(object));
                    } catch (IllegalArgumentException e) {
                    } catch (IllegalAccessException e) {
                    }
                }
            }
            trace.addEntry(position, var, entry);
        }
    } catch (CodeUnderTestException e) {
        logger.debug("", e);
    // throw new UnsupportedOperationException();
    }
}
Also used : Field(java.lang.reflect.Field) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Example 3 with CodeUnderTestException

use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.

the class OutputObserver method afterStatement.

/* (non-Javadoc)
     * @see org.evosuite.testcase.ExecutionObserver#afterStatement(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, java.lang.Throwable)
     */
@Override
public void afterStatement(Statement statement, Scope scope, Throwable exception) {
    if (statement instanceof MethodStatement) {
        MethodStatement methodStmt = (MethodStatement) statement;
        VariableReference varRef = methodStmt.getReturnValue();
        try {
            Object returnObject = varRef.getObject(scope);
            if (exception == null && !methodStmt.getReturnType().equals(Void.TYPE)) {
                // we don't save anything if there was an exception
                // we are only interested in methods whose return type != void
                String className = methodStmt.getDeclaringClassName();
                String methodDesc = methodStmt.getDescriptor();
                String methodName = methodStmt.getMethodName();
                outputCoverage.put(statement.getPosition(), OutputCoverageGoal.createGoalsFromObject(className, methodName, methodDesc, returnObject));
            }
        } catch (CodeUnderTestException e) {
        // ignore?
        }
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Example 4 with CodeUnderTestException

use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.

the class SymbolicObserver method after.

private void after(FloatPrimitiveStatement statement, Scope scope) {
    float valueOf = statement.getValue();
    VariableReference varRef = statement.getReturnValue();
    String varRefName = varRef.getName();
    RealVariable realVariable = buildRealVariable(varRefName, valueOf, -Float.MAX_VALUE, Float.MAX_VALUE);
    symb_expressions.put(varRefName, realVariable);
    Float float_instance;
    try {
        float_instance = (Float) varRef.getObject(scope);
    } catch (CodeUnderTestException e) {
        throw new EvosuiteError(e);
    }
    ReferenceConstant floatRef = newFloatReference(float_instance, realVariable);
    symb_references.put(varRefName, floatRef);
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) RealVariable(org.evosuite.symbolic.expr.fp.RealVariable) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Example 5 with CodeUnderTestException

use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.

the class SymbolicObserver method call_vm_caller_stack_params.

private void call_vm_caller_stack_params(boolean needThis, List<VariableReference> parameters, Scope scope, String desc) {
    int calleeLocalsIndex = 0;
    if (needThis)
        calleeLocalsIndex++;
    for (int i = 0; i < parameters.size(); i++) {
        VariableReference p = parameters.get(i);
        calleeLocalsIndex += getSize(p.getType());
    }
    Type[] argTypes = Type.getArgumentTypes(desc);
    for (int i = parameters.size() - 1; i >= 0; i--) {
        Type argType = argTypes[i];
        VariableReference p = parameters.get(i);
        try {
            Object param_object = p.getObject(scope);
            calleeLocalsIndex -= getSize(p.getType());
            if (argType.equals(Type.INT_TYPE)) {
                int intValue = getIntValue(param_object);
                VM.CALLER_STACK_PARAM(intValue, i, calleeLocalsIndex);
            } else if (argType.equals(Type.CHAR_TYPE)) {
                char charValue = getCharValue(param_object);
                VM.CALLER_STACK_PARAM(charValue, i, calleeLocalsIndex);
            } else if (argType.equals(Type.BYTE_TYPE)) {
                byte byteValue = getByteValue(param_object);
                VM.CALLER_STACK_PARAM(byteValue, i, calleeLocalsIndex);
            } else if (argType.equals(Type.BOOLEAN_TYPE)) {
                boolean booleanValue = getBooleanValue(param_object);
                VM.CALLER_STACK_PARAM(booleanValue, i, calleeLocalsIndex);
            } else if (argType.equals(Type.SHORT_TYPE)) {
                short shortValue = getShortValue(param_object);
                VM.CALLER_STACK_PARAM(shortValue, i, calleeLocalsIndex);
            } else if (argType.equals(Type.LONG_TYPE)) {
                long longValue = getLongValue(param_object);
                VM.CALLER_STACK_PARAM(longValue, i, calleeLocalsIndex);
            } else if (argType.equals(Type.FLOAT_TYPE)) {
                float floatValue = getFloatValue(param_object);
                VM.CALLER_STACK_PARAM(floatValue, i, calleeLocalsIndex);
            } else if (argType.equals(Type.DOUBLE_TYPE)) {
                double doubleValue = getDoubleValue(param_object);
                VM.CALLER_STACK_PARAM(doubleValue, i, calleeLocalsIndex);
            } else {
                VM.CALLER_STACK_PARAM(param_object, i, calleeLocalsIndex);
            }
        } catch (CodeUnderTestException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : Type(org.objectweb.asm.Type) VariableReference(org.evosuite.testcase.variable.VariableReference) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Aggregations

CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)33 VariableReference (org.evosuite.testcase.variable.VariableReference)25 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)16 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)14 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)6 ReferenceExpression (org.evosuite.symbolic.expr.ref.ReferenceExpression)6 MethodStatement (org.evosuite.testcase.statements.MethodStatement)5 Type (org.objectweb.asm.Type)5 StringValue (org.evosuite.symbolic.expr.str.StringValue)4 UncompilableCodeException (org.evosuite.testcase.execution.UncompilableCodeException)4 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 LinkedHashSet (java.util.LinkedHashSet)3 Expression (org.evosuite.symbolic.expr.Expression)3 IntegerValue (org.evosuite.symbolic.expr.bv.IntegerValue)3 RealValue (org.evosuite.symbolic.expr.fp.RealValue)3 PrimitiveExpression (org.evosuite.testcase.statements.PrimitiveExpression)3 ArrayReference (org.evosuite.testcase.variable.ArrayReference)3 ConstantValue (org.evosuite.testcase.variable.ConstantValue)3 Field (java.lang.reflect.Field)2