Search in sources :

Example 1 with EqualsAssertion

use of org.evosuite.assertion.EqualsAssertion 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 2 with EqualsAssertion

use of org.evosuite.assertion.EqualsAssertion in project evosuite by EvoSuite.

the class EqualsHashcodeContract 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 });
        Method hashCodeMethod = a.getGenericClass().getRawClass().getMethod("hashCode", new Class<?>[] {});
        GenericMethod genericEqualsMethod = new GenericMethod(equalsMethod, a.getGenericClass());
        GenericMethod genericHashCodeMethod = new GenericMethod(hashCodeMethod, a.getGenericClass());
        // Create x = a.equals(b)
        Statement st1 = new MethodStatement(test, genericEqualsMethod, a, Arrays.asList(new VariableReference[] { b }));
        VariableReference x = test.addStatement(st1, statement.getPosition() + 1);
        // Create y = a.hashCode();
        Statement st2 = new MethodStatement(test, genericHashCodeMethod, a, Arrays.asList(new VariableReference[] {}));
        VariableReference y = test.addStatement(st2, statement.getPosition() + 2);
        // Create z = b.hashCode();
        Statement st3 = new MethodStatement(test, genericHashCodeMethod, b, Arrays.asList(new VariableReference[] {}));
        VariableReference z = test.addStatement(st3, statement.getPosition() + 3);
        // Create w = z == z
        VariableReference w = new VariableReferenceImpl(test, boolean.class);
        PrimitiveExpression exp = new PrimitiveExpression(test, w, y, Operator.EQUALS, z);
        w = test.addStatement(exp, statement.getPosition() + 4);
        Statement newStatement = test.getStatement(w.getStPosition());
        // Create assertEquals(x, w)
        EqualsAssertion assertion = new EqualsAssertion();
        assertion.setStatement(newStatement);
        assertion.setSource(x);
        assertion.setDest(w);
        assertion.setValue(true);
        newStatement.addAssertion(assertion);
        newStatement.addComment("Violates contract equals - hashcode");
    } 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) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) PrimitiveExpression(org.evosuite.testcase.statements.PrimitiveExpression) EqualsAssertion(org.evosuite.assertion.EqualsAssertion)

Example 3 with EqualsAssertion

use of org.evosuite.assertion.EqualsAssertion in project evosuite by EvoSuite.

the class EqualsContract method addAssertionAndComments.

@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
    EqualsAssertion assertion = new EqualsAssertion();
    assertion.setStatement(statement);
    assertion.setSource(variables.get(0));
    assertion.setDest(variables.get(0));
    assertion.setValue(true);
    statement.addAssertion(assertion);
    statement.addComment("Violates contract a.equals(a)");
}
Also used : EqualsAssertion(org.evosuite.assertion.EqualsAssertion)

Example 4 with EqualsAssertion

use of org.evosuite.assertion.EqualsAssertion in project evosuite by EvoSuite.

the class EqualsNullContract method addAssertionAndComments.

@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
    EqualsAssertion assertion = new EqualsAssertion();
    assertion.setStatement(statement);
    VariableReference var = variables.get(0);
    assertion.setSource(var);
    assertion.setDest(new ConstantValue(statement.getTestCase(), Object.class));
    // assertion.setDest(new NullReference(statement.getTestCase(), var.getType()));
    assertion.setValue(false);
    statement.addAssertion(assertion);
    statement.addComment("Violates contract equals(null)");
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) ConstantValue(org.evosuite.testcase.variable.ConstantValue) EqualsAssertion(org.evosuite.assertion.EqualsAssertion)

Aggregations

EqualsAssertion (org.evosuite.assertion.EqualsAssertion)4 VariableReference (org.evosuite.testcase.variable.VariableReference)3 Method (java.lang.reflect.Method)2 TestCase (org.evosuite.testcase.TestCase)2 MethodStatement (org.evosuite.testcase.statements.MethodStatement)2 Statement (org.evosuite.testcase.statements.Statement)2 GenericMethod (org.evosuite.utils.generic.GenericMethod)2 PrimitiveExpression (org.evosuite.testcase.statements.PrimitiveExpression)1 ConstantValue (org.evosuite.testcase.variable.ConstantValue)1 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)1