use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class CoverageGoalTestNameGenerationStrategy method chooseRepresentativeGoal.
/**
* Out of a set of multiple goals, select one that is representative.
* Assumes that goals is not empty, and all items in goals have the same type
* @param test
* @param goals
* @return
*/
private TestFitnessFunction chooseRepresentativeGoal(TestCase test, Collection<TestFitnessFunction> goals) {
Map<String, Integer> methodToPosition = new LinkedHashMap<>();
for (Statement st : test) {
if (st instanceof MethodStatement) {
MethodStatement ms = (MethodStatement) st;
String name = ms.getMethodName() + ms.getDescriptor();
methodToPosition.put(name, st.getPosition());
} else if (st instanceof ConstructorStatement) {
ConstructorStatement cs = (ConstructorStatement) st;
String name = "<init>" + cs.getDescriptor();
methodToPosition.put(name, st.getPosition());
}
}
// Randomness.choice(goals);
TestFitnessFunction chosenGoal = goals.iterator().next();
int chosenPosition = -1;
for (TestFitnessFunction goal : goals) {
if (methodToPosition.containsKey(goal.getTargetMethod())) {
int position = methodToPosition.get(goal.getTargetMethod());
if (position >= chosenPosition) {
chosenPosition = position;
chosenGoal = goal;
}
}
}
return chosenGoal;
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class BranchCoverageSuiteFitness method handleConstructorExceptions.
/**
* If there is an exception in a superconstructor, then the corresponding
* constructor might not be included in the execution trace
*
* @param results
* @param callCount
*/
private void handleConstructorExceptions(TestChromosome test, ExecutionResult result, Map<String, Integer> callCount) {
if (result.hasTimeout() || result.hasTestException() || result.noThrownExceptions()) {
return;
}
Integer exceptionPosition = result.getFirstPositionOfThrownException();
// TODO: Not sure why that can happen
if (exceptionPosition >= result.test.size()) {
return;
}
Statement statement = null;
if (result.test.hasStatement(exceptionPosition)) {
statement = result.test.getStatement(exceptionPosition);
}
if (statement instanceof ConstructorStatement) {
ConstructorStatement c = (ConstructorStatement) statement;
String className = c.getConstructor().getName();
String methodName = "<init>" + Type.getConstructorDescriptor(c.getConstructor().getConstructor());
String name = className + "." + methodName;
if (!callCount.containsKey(name)) {
callCount.put(name, 1);
if (branchlessMethodCoverageMap.containsKey(name)) {
TestFitnessFunction goal = branchlessMethodCoverageMap.get(name);
test.getTestCase().addCoveredGoal(goal);
toRemoveRootBranches.add(name);
if (Properties.TEST_ARCHIVE) {
Archive.getArchiveInstance().updateArchive(goal, test, 0.0);
}
}
}
}
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class HashCodeReturnsNormallyContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
TestCase test = statement.getTestCase();
int position = statement.getPosition();
VariableReference a = variables.get(0);
try {
Method hashCodeMethod = a.getGenericClass().getRawClass().getMethod("hashCode", new Class<?>[] {});
GenericMethod method = new GenericMethod(hashCodeMethod, a.getGenericClass());
Statement st1 = new MethodStatement(test, method, a, Arrays.asList(new VariableReference[] {}));
test.addStatement(st1, position + 1);
st1.addComment("Throws exception: " + exception.getMessage());
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class JUnitTheoryContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
TestCase test = statement.getTestCase();
int position = statement.getPosition();
VariableReference a = variables.get(0);
int pos = a.getStPosition();
try {
Constructor<?> defaultConstructor = theoryReceiver.getClass().getConstructor();
GenericConstructor constructor = new GenericConstructor(defaultConstructor, theoryReceiver.getClass());
Statement st1 = new ConstructorStatement(test, constructor, new ArrayList<VariableReference>());
VariableReference receiver = test.addStatement(st1, position + 1);
Statement st2 = new MethodStatement(test, theoryMethod, receiver, Arrays.asList(new VariableReference[] { test.getStatement(pos).getReturnValue() }));
test.addStatement(st2, position + 2);
st2.addComment("Violates theory: " + theoryMethod.getName());
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
logger.warn("Error while creating contract violation: " + e);
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
logger.warn("Error while creating contract violation: " + e);
e.printStackTrace();
}
}
use of org.evosuite.testcase.statements.Statement 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