Search in sources :

Example 1 with PrimitiveExpression

use of org.evosuite.testcase.statements.PrimitiveExpression 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)

Aggregations

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