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();
}
}
Aggregations