use of org.evosuite.testcase.variable.ConstantValue 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.variable.ConstantValue in project evosuite by EvoSuite.
the class InputObserver method afterStatement.
/* (non-Javadoc)
* @see org.evosuite.testcase.ExecutionObserver#afterStatement(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, java.lang.Throwable)
*/
@Override
public void afterStatement(Statement statement, Scope scope, Throwable exception) {
if (statement instanceof EntityWithParametersStatement) {
EntityWithParametersStatement parameterisedStatement = (EntityWithParametersStatement) statement;
List<VariableReference> parRefs = parameterisedStatement.getParameterReferences();
List<Object> argObjects = new ArrayList<>(parRefs.size());
for (VariableReference parRef : parRefs) {
Object parObject = null;
try {
if (parRef instanceof ArrayIndex || parRef instanceof FieldReference) {
parObject = parRef.getObject(scope);
} else if (parRef instanceof ConstantValue) {
parObject = ((ConstantValue) parRef).getValue();
} else {
parObject = parRef.getObject(scope);
}
} catch (CodeUnderTestException e) {
e.printStackTrace();
}
argObjects.add(parObject);
}
assert parRefs.size() == argObjects.size();
String className = parameterisedStatement.getDeclaringClassName();
String methodDesc = parameterisedStatement.getDescriptor();
String methodName = parameterisedStatement.getMethodName();
inputCoverage.put(statement.getPosition(), InputCoverageGoal.createCoveredGoalsFromParameters(className, methodName, methodDesc, argObjects));
// argumentsValues.put((EntityWithParametersStatement) statement, argObjects);
}
}
use of org.evosuite.testcase.variable.ConstantValue in project evosuite by EvoSuite.
the class PrivateMethodStatement method getReflectionParams.
private static List<VariableReference> getReflectionParams(TestCase tc, Class<?> klass, Method method, VariableReference callee, List<VariableReference> inputs) {
List<VariableReference> list = new ArrayList<>(3 + inputs.size() * 2);
list.add(new ConstantValue(tc, new GenericClass(Class.class), klass));
list.add(callee);
list.add(new ConstantValue(tc, new GenericClass(String.class), method.getName()));
Class<?>[] parameterTypes = method.getParameterTypes();
assert (parameterTypes.length == inputs.size());
for (int parameterNum = 0; parameterNum < parameterTypes.length; parameterNum++) {
VariableReference vr = inputs.get(parameterNum);
list.add(vr);
list.add(new ConstantValue(tc, new GenericClass(Class.class), parameterTypes[parameterNum]));
}
return list;
}
use of org.evosuite.testcase.variable.ConstantValue in project evosuite by EvoSuite.
the class EqualsNullContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
EqualsAssertion assertion = new EqualsAssertion();
assertion.setStatement(statement);
VariableReference var = variables.get(0);
assertion.setSource(var);
assertion.setDest(new ConstantValue(statement.getTestCase(), Object.class));
// assertion.setDest(new NullReference(statement.getTestCase(), var.getType()));
assertion.setValue(false);
statement.addAssertion(assertion);
statement.addComment("Violates contract equals(null)");
}
use of org.evosuite.testcase.variable.ConstantValue in project evosuite by EvoSuite.
the class TestOverloading method testNotOverloadedMethod.
@Test
public void testNotOverloadedMethod() {
Class<?> clazz = ClassWithoutOverloadedMethods.class;
Method method1 = clazz.getMethods()[0];
Method method2 = clazz.getMethods()[1];
GenericMethod genericMethod1 = new GenericMethod(method1, clazz);
GenericMethod genericMethod2 = new GenericMethod(method2, clazz);
TestCase test = new DefaultTestCase();
ConstantValue intValue = new ConstantValue(test, int.class);
VariableReference stringVar = new VariableReferenceImpl(test, String.class);
List<VariableReference> parameters1 = Arrays.asList(intValue);
List<VariableReference> parameters2 = Arrays.asList(stringVar);
assertFalse(genericMethod1.isOverloaded());
assertFalse(genericMethod2.isOverloaded());
assertFalse(genericMethod1.isOverloaded(parameters1));
assertFalse(genericMethod2.isOverloaded(parameters1));
assertFalse(genericMethod1.isOverloaded(parameters2));
assertFalse(genericMethod2.isOverloaded(parameters2));
}
Aggregations