Search in sources :

Example 1 with MethodStatement

use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.

the class UnitAssertionGenerator method isRelevant.

private boolean isRelevant(Statement s, TestCase t) {
    // Always allow assertions on the last statement
    if (s.getPosition() == (t.size() - 1))
        return true;
    // Allow assertions after method calls on the UUT
    if (s instanceof MethodStatement) {
        MethodStatement ms = (MethodStatement) s;
        String declaringClass = ms.getMethod().getDeclaringClass().getName();
        while (declaringClass.contains("$")) declaringClass = declaringClass.substring(0, declaringClass.indexOf("$"));
        if (declaringClass.equals(Properties.TARGET_CLASS) || (!Properties.TARGET_CLASS_PREFIX.isEmpty() && declaringClass.startsWith(Properties.TARGET_CLASS_PREFIX)))
            return true;
    }
    return false;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement)

Example 2 with MethodStatement

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

use of org.evosuite.testcase.statements.MethodStatement 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 4 with MethodStatement

use of org.evosuite.testcase.statements.MethodStatement 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 5 with MethodStatement

use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.

the class MutationAssertionGenerator method filterInspectorPrimitiveDuplication.

/**
 * Remove inspector assertions that follow method calls of the same method
 *
 * @param statement
 */
protected void filterInspectorPrimitiveDuplication(Statement statement) {
    Set<Assertion> assertions = new HashSet<Assertion>(statement.getAssertions());
    if (assertions.size() < 2)
        return;
    if (!(statement instanceof MethodStatement))
        return;
    MethodStatement methodStatement = (MethodStatement) statement;
    boolean hasPrimitive = false;
    for (Assertion assertion : assertions) {
        if (assertion instanceof PrimitiveAssertion) {
            if (assertion.getStatement().equals(statement)) {
                hasPrimitive = true;
            }
        }
    }
    if (hasPrimitive) {
        for (Assertion assertion : assertions) {
            if (assertion instanceof InspectorAssertion) {
                InspectorAssertion ia = (InspectorAssertion) assertion;
                if (ia.getInspector().getMethod().equals(methodStatement.getMethod().getMethod())) {
                    statement.removeAssertion(assertion);
                    return;
                }
            }
        }
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) HashSet(java.util.HashSet)

Aggregations

MethodStatement (org.evosuite.testcase.statements.MethodStatement)54 VariableReference (org.evosuite.testcase.variable.VariableReference)40 GenericMethod (org.evosuite.utils.generic.GenericMethod)29 Method (java.lang.reflect.Method)25 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)22 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)22 Statement (org.evosuite.testcase.statements.Statement)19 GenericClass (org.evosuite.utils.generic.GenericClass)15 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)11 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)8 TestCase (org.evosuite.testcase.TestCase)7 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)5 FieldStatement (org.evosuite.testcase.statements.FieldStatement)5 StringPrimitiveStatement (org.evosuite.testcase.statements.StringPrimitiveStatement)5 HashSet (java.util.HashSet)4 TestFactory (org.evosuite.testcase.TestFactory)4 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)4 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)4