use of org.evosuite.testcase.statements.ConstructorStatement 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.ConstructorStatement 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.ConstructorStatement in project evosuite by EvoSuite.
the class ContractViolation method resultsFromMethod.
public boolean resultsFromMethod(String methodName) {
if (statement instanceof MethodStatement) {
MethodStatement ms = (MethodStatement) statement;
String target = ms.getMethodName() + ms.getDescriptor();
return target.equals(methodName);
} else if (statement instanceof ConstructorStatement) {
return methodName.startsWith("<init>");
} else {
return false;
}
}
use of org.evosuite.testcase.statements.ConstructorStatement in project evosuite by EvoSuite.
the class ContractViolation method same.
/**
* Determine if we have already seen an instance of this violation
*
* @param other
* a {@link org.evosuite.contracts.ContractViolation} object.
* @return a boolean.
*/
public boolean same(ContractViolation other) {
// Same contract?
if (!contract.getClass().equals(other.contract.getClass()))
return false;
// Same type of statement?
if (!statement.getClass().equals(other.statement.getClass()))
return false;
// Same exception type?
if (exception != null && other.exception != null) {
if (!exception.getClass().equals(other.exception.getClass()))
return false;
}
// Same method call / constructor?
if (statement instanceof MethodStatement) {
MethodStatement ms1 = (MethodStatement) statement;
MethodStatement ms2 = (MethodStatement) other.statement;
if (ms1.getMethod().getMethod().equals(ms2.getMethod().getMethod())) {
return true;
}
} else if (statement instanceof ConstructorStatement) {
ConstructorStatement ms1 = (ConstructorStatement) statement;
ConstructorStatement ms2 = (ConstructorStatement) other.statement;
if (ms1.getConstructor().getConstructor().equals(ms2.getConstructor().getConstructor())) {
return true;
}
} else if (statement instanceof AssignmentStatement) {
VariableReference var1 = statement.getReturnValue();
VariableReference var2 = other.statement.getReturnValue();
if (var1 instanceof FieldReference && var2 instanceof FieldReference) {
if (((FieldReference) var1).getField().getField().equals(((FieldReference) var2).getField().getField()))
return true;
}
}
return false;
}
use of org.evosuite.testcase.statements.ConstructorStatement in project evosuite by EvoSuite.
the class SameTraceObserver 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) {
// TODO: Only MethodStatement?
if (statement.isAssignmentStatement())
return;
if (statement instanceof PrimitiveStatement<?>)
return;
if (statement instanceof ArrayStatement)
return;
if (statement instanceof ConstructorStatement)
return;
try {
Object object = var.getObject(scope);
if (object == null)
return;
if (var.isPrimitive())
return;
if (var.isString() && Properties.INLINE)
// After inlining the value of assertions would be different
return;
SameTraceEntry entry = new SameTraceEntry(var);
for (VariableReference other : scope.getElements(var.getType())) {
if (other == var)
continue;
if (other.isPrimitive())
continue;
if (other.isWrapperType())
// Issues with inlining resulting in unstable assertions
continue;
Object otherObject = other.getObject(scope);
if (otherObject == null)
continue;
if (otherObject.getClass() != object.getClass())
continue;
try {
logger.debug("Comparison of {} with {}", var, other);
entry.addEntry(other, object == otherObject);
} catch (Throwable t) {
logger.debug("Exception during equals: " + t);
// ignore?
}
}
trace.addEntry(statement.getPosition(), var, entry);
} catch (CodeUnderTestException e) {
logger.debug("", e);
}
}
Aggregations