Search in sources :

Example 16 with EvosuiteError

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

the class ConstructorStatement method execute.

// TODO: Handle inner classes (need instance parameter for newInstance)
/**
 * {@inheritDoc}
 */
@Override
public Throwable execute(final Scope scope, PrintStream out) throws InvocationTargetException, IllegalArgumentException, InstantiationException, IllegalAccessException {
    // PrintStream old_out = System.out;
    // PrintStream old_err = System.err;
    // System.setOut(out);
    // System.setErr(out);
    logger.trace("Executing constructor " + constructor.toString());
    final Object[] inputs = new Object[parameters.size()];
    Throwable exceptionThrown = null;
    try {
        return super.exceptionHandler(new Executer() {

            @Override
            public void execute() throws InvocationTargetException, IllegalArgumentException, IllegalAccessException, InstantiationException, CodeUnderTestException {
                java.lang.reflect.Type[] parameterTypes = constructor.getParameterTypes();
                for (int i = 0; i < parameters.size(); i++) {
                    VariableReference parameterVar = parameters.get(i);
                    try {
                        inputs[i] = parameterVar.getObject(scope);
                    } catch (CodeUnderTestException e) {
                        throw e;
                    // throw new CodeUnderTestException(e.getCause());
                    // throw CodeUnderTestException.throwException(e.getCause());
                    } catch (Throwable e) {
                        // FIXME: this does not seem to propagate to client root. Is this normal behavior?
                        logger.error("Class " + Properties.TARGET_CLASS + ". Error encountered: " + e);
                        assert (false);
                        throw new EvosuiteError(e);
                    }
                    if (inputs[i] != null && !TypeUtils.isAssignable(inputs[i].getClass(), parameterTypes[i])) {
                        // !parameterVar.isAssignableTo(parameterTypes[i])) {
                        throw new CodeUnderTestException(new UncompilableCodeException("Cannot assign " + parameterVar.getVariableClass().getName() + " to " + parameterTypes[i]));
                    }
                    if (inputs[i] == null && constructor.getConstructor().getParameterTypes()[i].isPrimitive()) {
                        throw new CodeUnderTestException(new NullPointerException());
                    }
                }
                // If this is a non-static member class, the first parameter must not be null
                if (constructor.getConstructor().getDeclaringClass().isMemberClass() && !Modifier.isStatic(constructor.getConstructor().getDeclaringClass().getModifiers())) {
                    if (inputs[0] == null) {
                        // throw new NullPointerException();
                        throw new CodeUnderTestException(new NullPointerException());
                    }
                }
                Object ret = constructor.getConstructor().newInstance(inputs);
                try {
                    // assert(retval.getVariableClass().isAssignableFrom(ret.getClass())) :"we want an " + retval.getVariableClass() + " but got an " + ret.getClass();
                    retval.setObject(scope, ret);
                } catch (CodeUnderTestException e) {
                    throw e;
                // throw CodeUnderTestException.throwException(e);
                } catch (Throwable e) {
                    throw new EvosuiteError(e);
                }
            }

            @Override
            public Set<Class<? extends Throwable>> throwableExceptions() {
                Set<Class<? extends Throwable>> t = new LinkedHashSet<Class<? extends Throwable>>();
                t.add(InvocationTargetException.class);
                return t;
            }
        });
    } catch (InvocationTargetException e) {
        VM.disableCallBacks();
        exceptionThrown = e.getCause();
        if (logger.isDebugEnabled()) {
            try {
                logger.debug("Exception thrown in constructor: " + e.getCause());
            }// this can happen if SUT throws exception on toString
             catch (Exception ex) {
                logger.debug("Exception thrown in constructor and SUT gives issue when calling e.getCause()", ex);
            }
        }
    }
    return exceptionThrown;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UncompilableCodeException(org.evosuite.testcase.execution.UncompilableCodeException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) UncompilableCodeException(org.evosuite.testcase.execution.UncompilableCodeException)

Example 17 with EvosuiteError

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

the class SymbolicObserver method pushDummyValue.

private void pushDummyValue(Type argType) {
    if (isBv32(argType)) {
        IntegerValue integerExpr = ExpressionFactory.buildNewIntegerConstant(0);
        env.topFrame().operandStack.pushBv32(integerExpr);
    } else if (isBv64(argType)) {
        IntegerValue integerExpr = ExpressionFactory.buildNewIntegerConstant(0);
        env.topFrame().operandStack.pushBv64(integerExpr);
    } else if (isFp32(argType)) {
        RealValue realExpr = ExpressionFactory.buildNewRealConstant(0);
        env.topFrame().operandStack.pushFp32(realExpr);
    } else if (isFp64(argType)) {
        RealValue realExpr = ExpressionFactory.buildNewRealConstant(0);
        env.topFrame().operandStack.pushFp64(realExpr);
    } else {
        throw new EvosuiteError(argType.toString() + " is not a value type!");
    }
}
Also used : RealValue(org.evosuite.symbolic.expr.fp.RealValue) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue)

Example 18 with EvosuiteError

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

the class SymbolicObserver method after.

private void after(BytePrimitiveStatement statement, Scope scope) {
    byte valueOf = statement.getValue();
    VariableReference varRef = statement.getReturnValue();
    String varRefName = varRef.getName();
    IntegerVariable integerVariable = buildIntegerVariable(varRefName, valueOf, Byte.MIN_VALUE, Byte.MAX_VALUE);
    symb_expressions.put(varRefName, integerVariable);
    Byte byte_instance;
    try {
        byte_instance = (Byte) varRef.getObject(scope);
    } catch (CodeUnderTestException e) {
        throw new EvosuiteError(e);
    }
    ReferenceConstant byteRef = newByteReference(byte_instance, integerVariable);
    symb_references.put(varRefName, byteRef);
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Example 19 with EvosuiteError

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

the class SymbolicObserver method pushValue.

private void pushValue(Type argType, Expression<?> symb_expr) {
    if (isBv32(argType)) {
        IntegerValue booleanExpr = (IntegerValue) symb_expr;
        env.topFrame().operandStack.pushBv32(booleanExpr);
    } else if (isBv64(argType)) {
        IntegerValue longExpr = (IntegerValue) symb_expr;
        env.topFrame().operandStack.pushBv64(longExpr);
    } else if (isFp32(argType)) {
        RealValue realExpr = (RealValue) symb_expr;
        env.topFrame().operandStack.pushFp32(realExpr);
    } else if (isFp64(argType)) {
        RealValue realExpr = (RealValue) symb_expr;
        env.topFrame().operandStack.pushFp64(realExpr);
    } else {
        throw new EvosuiteError(argType.toString() + " is not a value type!");
    }
}
Also used : RealValue(org.evosuite.symbolic.expr.fp.RealValue) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue)

Example 20 with EvosuiteError

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

the class SymbolicObserver method after.

private void after(LongPrimitiveStatement statement, Scope scope) {
    long valueOf = statement.getValue();
    VariableReference varRef = statement.getReturnValue();
    String varRefName = varRef.getName();
    IntegerVariable integerVariable = buildIntegerVariable(varRefName, valueOf, Long.MIN_VALUE, Long.MAX_VALUE);
    symb_expressions.put(varRefName, integerVariable);
    Long long_instance;
    try {
        long_instance = (Long) varRef.getObject(scope);
    } catch (CodeUnderTestException e) {
        throw new EvosuiteError(e);
    }
    ReferenceConstant longRef = newLongReference(long_instance, integerVariable);
    symb_references.put(varRefName, longRef);
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Aggregations

EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)21 VariableReference (org.evosuite.testcase.variable.VariableReference)15 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)14 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)11 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 Set (java.util.Set)3 UncompilableCodeException (org.evosuite.testcase.execution.UncompilableCodeException)3 LinkedHashSet (java.util.LinkedHashSet)2 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)2 IntegerValue (org.evosuite.symbolic.expr.bv.IntegerValue)2 RealValue (org.evosuite.symbolic.expr.fp.RealValue)2 RealVariable (org.evosuite.symbolic.expr.fp.RealVariable)2 ReferenceExpression (org.evosuite.symbolic.expr.ref.ReferenceExpression)2 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)2 MethodStatement (org.evosuite.testcase.statements.MethodStatement)2 Statement (org.evosuite.testcase.statements.Statement)2 StringPrimitiveStatement (org.evosuite.testcase.statements.StringPrimitiveStatement)2 GenericMethod (org.evosuite.utils.generic.GenericMethod)2