use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class Contract method isTargetStatement.
/**
* Check if this statement is related to the unit under test
*
* @param statement
* a {@link org.evosuite.testcase.statements.Statement} object.
* @return a boolean.
*/
protected boolean isTargetStatement(Statement statement) {
// return true;
if (statement instanceof MethodStatement) {
MethodStatement ms = (MethodStatement) statement;
final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
if (targetClass.equals(ms.getMethod().getDeclaringClass()))
return true;
} else if (statement instanceof ConstructorStatement) {
ConstructorStatement cs = (ConstructorStatement) statement;
final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
if (targetClass.equals(cs.getConstructor().getDeclaringClass()))
return true;
} else if (statement instanceof FieldStatement) {
FieldStatement fs = (FieldStatement) statement;
final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
if (targetClass.equals(fs.getField().getDeclaringClass()))
return true;
}
return false;
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class ExceptionCoverageHelper method getMethodIdentifier.
public static String getMethodIdentifier(ExecutionResult result, int exceptionPosition) {
if (result.test.getStatement(exceptionPosition) instanceof MethodStatement) {
MethodStatement ms = (MethodStatement) result.test.getStatement(exceptionPosition);
Method method = ms.getMethod().getMethod();
return method.getName() + Type.getMethodDescriptor(method);
} else if (result.test.getStatement(exceptionPosition) instanceof ConstructorStatement) {
ConstructorStatement cs = (ConstructorStatement) result.test.getStatement(exceptionPosition);
Constructor<?> constructor = cs.getConstructor().getConstructor();
return "<init>" + Type.getConstructorDescriptor(constructor);
}
return "";
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class ExceptionCoverageHelper method isSutException.
public static boolean isSutException(ExecutionResult result, int exceptionPosition) {
if (result.test.getStatement(exceptionPosition) instanceof MethodStatement) {
MethodStatement ms = (MethodStatement) result.test.getStatement(exceptionPosition);
Method method = ms.getMethod().getMethod();
Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
if (method.getDeclaringClass().equals(targetClass)) {
return true;
}
} else if (result.test.getStatement(exceptionPosition) instanceof ConstructorStatement) {
ConstructorStatement cs = (ConstructorStatement) result.test.getStatement(exceptionPosition);
Constructor<?> constructor = cs.getConstructor().getConstructor();
Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
if (constructor.getDeclaringClass().equals(targetClass)) {
return true;
}
}
return false;
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class MutationAssertionGenerator method justNullAssertion.
/**
* Returns true if the statement has nothing but null assertions
*
* @param statement
* @return
*/
protected boolean justNullAssertion(Statement statement) {
Set<Assertion> assertions = statement.getAssertions();
if (assertions.isEmpty())
return false;
else {
Iterator<Assertion> iterator = assertions.iterator();
VariableReference ret = statement.getReturnValue();
VariableReference callee = null;
if (statement instanceof MethodStatement) {
callee = ((MethodStatement) statement).getCallee();
}
boolean just = true;
while (iterator.hasNext()) {
Assertion ass = iterator.next();
if (!(ass instanceof NullAssertion)) {
if (ass.getReferencedVariables().contains(ret) || ass.getReferencedVariables().contains(callee)) {
just = false;
break;
}
}
}
return just;
}
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class ConstraintVerifierTest method testDelete_multipleVarsThatCouldBeReused.
@Test
public void testDelete_multipleVarsThatCouldBeReused() throws Exception {
TestChromosome tc = new TestChromosome();
TestFactory factory = TestFactory.getInstance();
VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
// initializing bounding variable method called directly after the new
factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 1, 0);
// now do it again
VariableReference secondServlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 2, 0);
factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 3, 0);
MethodStatement mt = (MethodStatement) tc.getTestCase().getStatement(3);
// be sure it is using the second servlet
mt.replace(servlet, secondServlet);
Assert.assertEquals(4, tc.size());
Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
// bounded variable can be deleted
Assert.assertTrue(ConstraintVerifier.canDelete(tc.getTestCase(), 0));
// method using bounded variable should not be deleted
Assert.assertFalse(ConstraintVerifier.canDelete(tc.getTestCase(), 1));
// bounded variable can be deleted
Assert.assertTrue(ConstraintVerifier.canDelete(tc.getTestCase(), 2));
// method using bounded variable should not be deleted
Assert.assertFalse(ConstraintVerifier.canDelete(tc.getTestCase(), 3));
boolean mutated = tc.deleteStatement(factory, 2);
Assert.assertTrue(mutated);
/*
deleting the bounded variable in position 2 should lead to also delete the initializing variable in 3,
and not end up with 3 re-using 0
*/
Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
Assert.assertEquals(2, tc.size());
}
Aggregations