use of org.evosuite.testcase.statements.PrimitiveStatement in project evosuite by EvoSuite.
the class InspectorTraceObserver 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: Check the variable class is complex?
// We don't want inspector checks on string constants
Statement declaringStatement = currentTest.getStatement(var.getStPosition());
if (declaringStatement instanceof PrimitiveStatement<?>)
return;
if (statement.isAssignmentStatement() && statement.getReturnValue().isArrayIndex())
return;
if (statement instanceof ConstructorStatement) {
if (statement.getReturnValue().isWrapperType() || statement.getReturnValue().isAssignableTo(EvoSuiteMock.class))
return;
}
if (var.isPrimitive() || var.isString() || var.isWrapperType())
return;
logger.debug("Checking for inspectors of " + var + " at statement " + statement.getPosition());
List<Inspector> inspectors = InspectorManager.getInstance().getInspectors(var.getVariableClass());
InspectorTraceEntry entry = new InspectorTraceEntry(var);
for (Inspector i : inspectors) {
// No inspectors from java.lang.Object
if (i.getMethod().getDeclaringClass().equals(Object.class))
continue;
try {
Object target = var.getObject(scope);
if (target != null) {
// Don't call inspector methods on mock objects
if (target.getClass().getCanonicalName().contains("EnhancerByMockito"))
return;
Object value = i.getValue(target);
logger.debug("Inspector " + i.getMethodCall() + " is: " + value);
// We need no assertions that include the memory location
if (value instanceof String) {
// String literals may not be longer than 32767
if (((String) value).length() >= 32767)
continue;
// Maximum length of strings we look at
if (((String) value).length() > Properties.MAX_STRING)
continue;
// If we suspect an Object hashCode not use this, as it may lead to flaky tests
if (addressPattern.matcher((String) value).find())
continue;
// The word "hashCode" is also suspicious
if (((String) value).toLowerCase().contains("hashcode"))
continue;
// Avoid asserting anything on values referring to mockito proxy objects
if (((String) value).toLowerCase().contains("EnhancerByMockito"))
continue;
if (((String) value).toLowerCase().contains("$MockitoMock$"))
continue;
if (target instanceof URL) {
// Absolute paths may be different between executions
if (((String) value).startsWith("/") || ((String) value).startsWith("file:/"))
continue;
}
}
entry.addValue(i, value);
}
} catch (Exception e) {
if (e instanceof TimeoutException) {
logger.debug("Timeout during inspector call - deactivating inspector " + i.getMethodCall());
InspectorManager.getInstance().removeInspector(var.getVariableClass(), i);
}
logger.debug("Exception " + e + " / " + e.getCause());
if (e.getCause() != null && !e.getCause().getClass().equals(NullPointerException.class)) {
logger.debug("Exception during call to inspector: " + e + " - " + e.getCause());
}
}
}
logger.debug("Found " + entry.size() + " inspectors for " + var + " at statement " + statement.getPosition());
trace.addEntry(statement.getPosition(), var, entry);
}
use of org.evosuite.testcase.statements.PrimitiveStatement in project evosuite by EvoSuite.
the class ArrayLocalSearch method stripAssignments.
private int stripAssignments(ArrayStatement statement, TestChromosome test, LocalSearchObjective<TestChromosome> objective) {
int difference = 0;
ArrayReference arrRef = (ArrayReference) statement.getReturnValue();
TestFactory factory = TestFactory.getInstance();
for (int position = test.size() - 1; position > statement.getPosition(); position--) {
logger.debug("Current delete position: " + position);
if (test.getTestCase().getStatement(position) instanceof AssignmentStatement) {
logger.debug("Is assignment statement");
AssignmentStatement assignment = (AssignmentStatement) test.getTestCase().getStatement(position);
Statement valueStatement = test.getTestCase().getStatement(assignment.getValue().getStPosition());
if (assignment.getReturnValue().getAdditionalVariableReference() == arrRef) {
int currentDelta = 0;
int differenceDelta = 0;
logger.debug("Assigns to target array. Checking if we can remove it without worsening fitness");
backup(test);
try {
factory.deleteStatement(test.getTestCase(), position);
if (valueStatement instanceof PrimitiveStatement || valueStatement instanceof NullStatement) {
if (!test.getTestCase().hasReferences(valueStatement.getReturnValue())) {
if (valueStatement.getPosition() < statement.getPosition())
differenceDelta = 1;
currentDelta = 1;
logger.debug("Deleting primitive statement assigned to this array at " + valueStatement.getPosition());
factory.deleteStatement(test.getTestCase(), valueStatement.getPosition());
}
}
if (!objective.hasNotWorsened(test)) {
logger.debug("Fitness has decreased, so restoring test");
restore(test);
currentDelta = 0;
differenceDelta = 0;
}
} catch (ConstructionFailedException e) {
currentDelta = 0;
differenceDelta = 0;
restore(test);
}
position -= currentDelta;
difference += differenceDelta;
}
}
}
return difference;
}
use of org.evosuite.testcase.statements.PrimitiveStatement in project evosuite by EvoSuite.
the class RandomInsertion method selectRandomVariableForCall.
private VariableReference selectRandomVariableForCall(TestCase test, int position) {
if (test.isEmpty() || position == 0)
return null;
List<VariableReference> allVariables = test.getObjects(position);
List<VariableReference> candidateVariables = new ArrayList<>();
for (VariableReference var : allVariables) {
if (!(var instanceof NullReference) && !var.isVoid() && !var.getGenericClass().isObject() && !(test.getStatement(var.getStPosition()) instanceof PrimitiveStatement) && !var.isPrimitive() && (test.hasReferences(var) || var.getVariableClass().equals(Properties.getInitializedTargetClass())) && // do not directly call methods on mock objects
!(test.getStatement(var.getStPosition()) instanceof FunctionalMockStatement)) {
candidateVariables.add(var);
}
}
if (candidateVariables.isEmpty()) {
return null;
} else if (Properties.SORT_OBJECTS) {
candidateVariables = candidateVariables.stream().sorted(Comparator.comparingInt(item -> item.getDistance())).collect(Collectors.toList());
return ListUtil.selectRankBiased(candidateVariables);
} else {
return Randomness.choice(candidateVariables);
}
}
use of org.evosuite.testcase.statements.PrimitiveStatement 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.PrimitiveStatement 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);
}
}
Aggregations