Search in sources :

Example 1 with EvosuiteError

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

the class TestSuiteGenerator method buildLoadTargetClassTestCase.

/**
 * Creates a single Test Case that only loads the target class.
 * <code>
 * Thread currentThread = Thread.currentThread();
 * ClassLoader classLoader = currentThread.getClassLoader();
 * classLoader.load(className);
 * </code>
 * @param className the class to be loaded
 * @return
 * @throws EvosuiteError if a reflection error happens while creating the test case
 */
private static DefaultTestCase buildLoadTargetClassTestCase(String className) throws EvosuiteError {
    DefaultTestCase test = new DefaultTestCase();
    StringPrimitiveStatement stmt0 = new StringPrimitiveStatement(test, className);
    VariableReference string0 = test.addStatement(stmt0);
    try {
        Method currentThreadMethod = Thread.class.getMethod("currentThread");
        Statement currentThreadStmt = new MethodStatement(test, new GenericMethod(currentThreadMethod, currentThreadMethod.getDeclaringClass()), null, Collections.emptyList());
        VariableReference currentThreadVar = test.addStatement(currentThreadStmt);
        Method getContextClassLoaderMethod = Thread.class.getMethod("getContextClassLoader");
        Statement getContextClassLoaderStmt = new MethodStatement(test, new GenericMethod(getContextClassLoaderMethod, getContextClassLoaderMethod.getDeclaringClass()), currentThreadVar, Collections.emptyList());
        VariableReference contextClassLoaderVar = test.addStatement(getContextClassLoaderStmt);
        // Method loadClassMethod = ClassLoader.class.getMethod("loadClass", String.class);
        // Statement loadClassStmt = new MethodStatement(test,
        // new GenericMethod(loadClassMethod, loadClassMethod.getDeclaringClass()), contextClassLoaderVar,
        // Collections.singletonList(string0));
        // test.addStatement(loadClassStmt);
        BooleanPrimitiveStatement stmt1 = new BooleanPrimitiveStatement(test, true);
        VariableReference boolean0 = test.addStatement(stmt1);
        Method forNameMethod = Class.class.getMethod("forName", String.class, boolean.class, ClassLoader.class);
        Statement forNameStmt = new MethodStatement(test, new GenericMethod(forNameMethod, forNameMethod.getDeclaringClass()), null, Arrays.<VariableReference>asList(string0, boolean0, contextClassLoaderVar));
        test.addStatement(forNameStmt);
        return test;
    } catch (NoSuchMethodException | SecurityException e) {
        throw new EvosuiteError("Unexpected exception while creating Class Initializer Test Case");
    }
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 2 with EvosuiteError

use of org.evosuite.testcase.execution.EvosuiteError 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 3 with EvosuiteError

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

the class SymbolicObserver method after.

private void after(BooleanPrimitiveStatement statement, Scope scope) {
    boolean valueOf = statement.getValue();
    VariableReference varRef = statement.getReturnValue();
    String varRefName = varRef.getName();
    IntegerVariable integerVariable = buildIntegerVariable(varRefName, valueOf ? 1 : 0, 0, 1);
    Boolean boolean_instance;
    try {
        boolean_instance = (Boolean) varRef.getObject(scope);
    } catch (CodeUnderTestException e) {
        throw new EvosuiteError(e);
    }
    symb_expressions.put(varRefName, integerVariable);
    ReferenceConstant booleanRef = newBooleanReference(boolean_instance, integerVariable);
    symb_references.put(varRefName, booleanRef);
}
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 4 with EvosuiteError

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

the class SymbolicObserver method after.

private void after(DoublePrimitiveStatement statement, Scope scope) {
    double valueOf = statement.getValue();
    VariableReference varRef = statement.getReturnValue();
    String varRefName = varRef.getName();
    RealVariable realVariable = buildRealVariable(varRefName, valueOf, -Double.MAX_VALUE, Double.MAX_VALUE);
    symb_expressions.put(varRefName, realVariable);
    Double double_instance;
    try {
        double_instance = (Double) varRef.getObject(scope);
    } catch (CodeUnderTestException e) {
        throw new EvosuiteError(e);
    }
    ReferenceConstant doubleRef = newDoubleReference(double_instance, realVariable);
    symb_references.put(varRefName, doubleRef);
}
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 EvosuiteError

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

the class SymbolicObserver method after.

private void after(ShortPrimitiveStatement statement, Scope scope) {
    short valueOf = statement.getValue();
    VariableReference varRef = statement.getReturnValue();
    String varRefName = varRef.getName();
    IntegerVariable integerVariable = buildIntegerVariable(varRefName, valueOf, Short.MIN_VALUE, Short.MAX_VALUE);
    symb_expressions.put(varRefName, integerVariable);
    Short short_instance;
    try {
        short_instance = (Short) varRef.getObject(scope);
    } catch (CodeUnderTestException e) {
        throw new EvosuiteError(e);
    }
    ReferenceConstant shortRef = newShortReference(short_instance, integerVariable);
    symb_references.put(varRefName, shortRef);
}
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