Search in sources :

Example 1 with VariableReference

use of org.evosuite.testcase.variable.VariableReference 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 VariableReference

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

the class EqualsContract 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) {
    for (VariableReference var : getAllVariables(scope)) {
        Object object = scope.getObject(var);
        if (object == null)
            continue;
        // We do not want to call equals if it is the default implementation
        Class<?>[] parameters = { Object.class };
        try {
            Method equalsMethod = object.getClass().getMethod("equals", parameters);
            if (equalsMethod.getDeclaringClass().equals(Object.class))
                continue;
        } catch (SecurityException e1) {
            continue;
        } catch (NoSuchMethodException e1) {
            continue;
        }
        try {
            // An object always has to equal itself
            if (!object.equals(object))
                return new ContractViolation(this, statement, exception, var);
        } catch (NullPointerException e) {
            continue;
        // No nullpointer exceptions may be thrown if the parameter was not null
        // TODO: Use UndeclaredExceptionContract instead?
        // return new ContractViolation(this, statement, e, var);
        // Returning this contract violation is definitely wrong as it would look like equals returned false
        } catch (Throwable t) {
            continue;
        }
    }
    return null;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) Method(java.lang.reflect.Method)

Example 3 with VariableReference

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

the class EqualsSymmetricContract method check.

/* (non-Javadoc)
	 * @see org.evosuite.contracts.Contract#check(org.evosuite.testcase.Statement, org.evosuite.testcase.Scope, java.lang.Throwable)
	 */
/**
 * {@inheritDoc}
 */
@Override
public ContractViolation check(Statement statement, Scope scope, Throwable exception) {
    for (Pair<VariableReference> pair : getAllVariablePairs(scope)) {
        // Equals self is covered by EqualsContract
        if (pair.object1 == pair.object2)
            continue;
        Object object1 = scope.getObject(pair.object1);
        Object object2 = scope.getObject(pair.object2);
        if (object1 == null || object2 == null)
            continue;
        // We do not want to call equals if it is the default implementation
        Class<?>[] parameters = { Object.class };
        try {
            Method equalsMethod = object1.getClass().getMethod("equals", parameters);
            if (equalsMethod.getDeclaringClass().equals(Object.class))
                continue;
        } catch (SecurityException e1) {
            continue;
        } catch (NoSuchMethodException e1) {
            continue;
        }
        ExecutionTracer.disable();
        if (object1.equals(object2)) {
            if (!object2.equals(object1)) {
                ExecutionTracer.enable();
                return new ContractViolation(this, statement, exception, pair.object1, pair.object2);
            }
        } else {
            if (object2.equals(object1)) {
                ExecutionTracer.enable();
                return new ContractViolation(this, statement, exception, pair.object1, pair.object2);
            }
        }
        ExecutionTracer.enable();
    }
    return null;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method)

Example 4 with VariableReference

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

the class EqualsSymmetricContract method addAssertionAndComments.

@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
    TestCase test = statement.getTestCase();
    VariableReference a = variables.get(0);
    VariableReference b = variables.get(1);
    try {
        Method equalsMethod = a.getGenericClass().getRawClass().getMethod("equals", new Class<?>[] { Object.class });
        GenericMethod method = new GenericMethod(equalsMethod, a.getGenericClass());
        // Create x = a.equals(b)
        Statement st1 = new MethodStatement(test, method, a, Arrays.asList(new VariableReference[] { b }));
        VariableReference x = test.addStatement(st1, statement.getPosition() + 1);
        // Create y = b.equals(a);
        Statement st2 = new MethodStatement(test, method, b, Arrays.asList(new VariableReference[] { a }));
        VariableReference y = test.addStatement(st2, statement.getPosition() + 2);
        Statement newStatement = test.getStatement(y.getStPosition());
        // Create assertEquals(x, y)
        EqualsAssertion assertion = new EqualsAssertion();
        assertion.setStatement(newStatement);
        assertion.setSource(x);
        assertion.setDest(y);
        assertion.setValue(true);
        newStatement.addAssertion(assertion);
        newStatement.addComment("Violates contract a.equals(b) <-> b.equals(a)");
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) TestCase(org.evosuite.testcase.TestCase) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) EqualsAssertion(org.evosuite.assertion.EqualsAssertion)

Example 5 with VariableReference

use of org.evosuite.testcase.variable.VariableReference 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)

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