use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class VariableReferenceImpl method getStPosition.
/**
* {@inheritDoc}
*
* The position of the statement, defining this VariableReference, in the
* testcase.
*
* TODO: Notify change listener also when return value changes
*/
@Override
public synchronized int getStPosition() {
if (stPosition == null || changeListener.hasChanged()) {
stPosition = null;
for (int i = 0; i < testCase.size(); i++) {
Statement stmt = testCase.getStatement(i);
if (stmt.getReturnValue().equals(this)) {
stPosition = i;
break;
}
}
if (stPosition == null) {
String msg = "Bloody annoying bug \n";
msg += "Test case has " + testCase.size() + " function calls \n";
for (int i = 0; i < testCase.size(); i++) {
msg += testCase.getStatement(i).getCode(null) + "\n";
}
msg += "failed to find type " + this.type.getTypeName() + "\n";
throw new AssertionError(msg + "A VariableReferences position is only defined if the VariableReference is defined by a statement in the testCase");
}
} else {
// somehow this could be null, leading to NPE. Synchronization issue?
int position = stPosition;
stPosition = null;
stPosition = getStPosition();
assert (stPosition == position);
}
return stPosition;
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class JUnitTestCarvedChromosomeFactorySystemTest method testInnerConstructor.
@Test
public void testInnerConstructor() {
Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.InnerConstructorTest.class.getCanonicalName();
Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.InnerConstructor.class.getCanonicalName();
Properties.SEED_MUTATIONS = 1;
Properties.SEED_CLONE = 1;
JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
Assert.assertTrue(factory.hasCarvedTestCases());
TestChromosome carved = factory.getChromosome();
Assert.assertNotNull(carved);
String code = carved.toString();
Assert.assertNotNull(code);
Assert.assertEquals(code, 2, carved.test.size());
for (int i = 0; i < carved.test.size(); i++) {
Statement stmt = carved.test.getStatement(i);
boolean valid = stmt.isValid();
Assert.assertTrue("Invalid stmt at position " + i, valid);
}
System.out.println(code);
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class ComparisonTraceObserver method visit.
/* (non-Javadoc)
* @see org.evosuite.assertion.AssertionTraceObserver#visit(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, org.evosuite.testcase.VariableReference)
*/
/**
* {@inheritDoc}
*/
@Override
protected void visit(Statement statement, Scope scope, VariableReference var) {
try {
Object object = var.getObject(scope);
if (object == null)
return;
if (statement instanceof AssignmentStatement)
return;
if (statement instanceof PrimitiveStatement<?>)
return;
ComparisonTraceEntry entry = new ComparisonTraceEntry(var);
int position = statement.getPosition();
for (VariableReference other : scope.getElements(var.getType())) {
Object otherObject = other.getObject(scope);
// TODO: Create a matrix of object comparisons?
if (otherObject == null)
// TODO: Don't do this?
continue;
if (object == otherObject)
// Don't compare with self?
continue;
int otherPos = other.getStPosition();
if (otherPos >= position)
// Don't compare with variables that are not defined - may happen with primitives?
continue;
Statement otherStatement = currentTest.getStatement(otherPos);
if (statement instanceof PrimitiveStatement && otherStatement instanceof PrimitiveStatement)
// Don't compare two primitives
continue;
if (otherStatement instanceof MethodStatement) {
if (((MethodStatement) otherStatement).getMethodName().equals("hashCode"))
// No comparison against hashCode, as the hashCode return value will not be in the test
continue;
}
if (Properties.PURE_EQUALS) {
String className = object.getClass().getCanonicalName();
String methodName = "equals";
String descriptor = Type.getMethodDescriptor(Type.BOOLEAN_TYPE, Type.getType(Object.class));
CheapPurityAnalyzer cheapPurityAnalyzer = CheapPurityAnalyzer.getInstance();
if (!cheapPurityAnalyzer.isPure(className, methodName, descriptor))
// Don't compare using impure equals(Object) methods
continue;
}
try {
logger.debug("Comparison of " + var + " with " + other + " is: " + object.equals(otherObject));
entry.addEntry(other, ComparisonTraceEntry.equals(object, otherObject));
} catch (Throwable t) {
logger.debug("Exception during equals: " + t);
// ignore?
}
if (object instanceof Comparable<?>) {
// TODO
}
}
trace.addEntry(statement.getPosition(), var, entry);
} catch (CodeUnderTestException e) {
logger.debug("", e);
}
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class ContainsTraceObserver method visit.
/* (non-Javadoc)
* @see org.evosuite.assertion.AssertionTraceObserver#visit(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, org.evosuite.testcase.VariableReference)
*/
/**
* {@inheritDoc}
*/
@Override
protected void visit(Statement statement, Scope scope, VariableReference var) {
try {
Object object = var.getObject(scope);
if (object == null)
return;
if (statement instanceof AssignmentStatement)
return;
if (statement instanceof PrimitiveStatement<?>)
return;
// Only relevant for Collections
if (!(object instanceof Collection))
return;
Collection collectionObject = (Collection) object;
List<GenericClass> parameterClasses = var.getGenericClass().getParameterClasses();
// Need to know exact type
if (parameterClasses.size() != 1)
return;
java.lang.reflect.Type parameterType = parameterClasses.get(0).getType();
ContainsTraceEntry entry = new ContainsTraceEntry(var);
int position = statement.getPosition();
Set<VariableReference> otherVariables = new LinkedHashSet<>();
otherVariables.addAll(scope.getElements(parameterType));
for (int i = 0; i <= statement.getPosition(); i++) {
for (VariableReference candidateVar : currentTest.getStatement(i).getVariableReferences()) {
if (candidateVar instanceof ConstantValue && candidateVar.isAssignableTo(parameterType)) {
otherVariables.add(candidateVar);
}
}
}
for (VariableReference other : otherVariables) {
Object otherObject;
if (other instanceof ConstantValue)
otherObject = ((ConstantValue) other).getValue();
else
otherObject = other.getObject(scope);
if (otherObject == null)
// TODO: Don't do this?
continue;
int otherPos = other.getStPosition();
if (otherPos > position)
// Don't compare with variables that are not defined - may happen with primitives?
continue;
Statement otherStatement = currentTest.getStatement(otherPos);
if (otherStatement instanceof MethodStatement) {
if (((MethodStatement) otherStatement).getMethodName().equals("hashCode"))
// No comparison against hashCode, as the hashCode return value will not be in the test
continue;
}
try {
logger.debug("Checking whether {} contains {} is: {}", var, other, collectionObject.contains(otherObject));
entry.addEntry(other, collectionObject.contains(otherObject));
} catch (Throwable t) {
logger.debug("Exception during equals: " + t);
// ignore?
}
if (object instanceof Comparable<?>) {
// TODO
}
}
trace.addEntry(statement.getPosition(), var, entry);
} catch (CodeUnderTestException e) {
logger.debug("", e);
}
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class MethodCoverageSuiteFitness method handleConstructorExceptions.
/**
* If there is an exception in a super-constructor, then the corresponding
* constructor might not be included in the execution trace
*
* @param suite
* @param results
* @param calledMethods
*/
protected void handleConstructorExceptions(TestChromosome test, ExecutionResult result, Set<String> calledMethods) {
if (result.hasTimeout() || result.hasTestException() || result.noThrownExceptions())
return;
Integer exceptionPosition = result.getFirstPositionOfThrownException();
Statement 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 (methodCoverageMap.containsKey(name) && !calledMethods.contains(name)) {
TestFitnessFunction goal = methodCoverageMap.get(name);
// only include methods being called
test.getTestCase().addCoveredGoal(goal);
calledMethods.add(name);
this.toRemoveMethods.add(name);
if (Properties.TEST_ARCHIVE) {
Archive.getArchiveInstance().updateArchive(goal, test, 0.0);
}
}
}
}
Aggregations