use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.
the class NullPointerExceptionContract method check.
/* (non-Javadoc)
* @see org.evosuite.contracts.Contract#check(org.evosuite.testcase.TestCase, org.evosuite.testcase.Statement, org.evosuite.testcase.Scope, java.lang.Throwable)
*/
/**
* {@inheritDoc}
*/
@Override
public ContractViolation check(Statement statement, Scope scope, Throwable exception) {
if (!isTargetStatement(statement))
return null;
try {
if (exception != null) {
// method throws no NullPointerException if no input parameter was null
if (exception instanceof NullPointerException) {
StackTraceElement element = exception.getStackTrace()[0];
// If the exception was thrown in the test directly, it is also not interesting
if (element.getClassName().startsWith(PackageInfo.getEvoSuitePackage() + ".testcase")) {
return null;
}
List<VariableReference> parameters = new ArrayList<VariableReference>();
if (statement instanceof MethodStatement) {
MethodStatement ms = (MethodStatement) statement;
parameters.addAll(ms.getParameterReferences());
} else if (statement instanceof ConstructorStatement) {
ConstructorStatement cs = (ConstructorStatement) statement;
parameters.addAll(cs.getParameterReferences());
} else {
return null;
}
boolean hasNull = false;
for (VariableReference var : parameters) {
if (var.getObject(scope) == null) {
hasNull = true;
break;
}
}
if (!hasNull) {
return new ContractViolation(this, statement, exception);
}
}
}
return null;
} catch (CodeUnderTestException e) {
throw new UnsupportedOperationException();
}
}
use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.
the class PrimitiveFieldTraceObserver 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) {
logger.debug("Checking fields of " + var);
try {
if (var == null)
return;
if (statement.isAssignmentStatement()) {
if (statement.getReturnValue().isArrayIndex()) {
return;
}
if (statement.getReturnValue().isFieldReference()) {
return;
}
}
Object object = var.getObject(scope);
int position = statement.getPosition();
if (object != null && !object.getClass().isPrimitive() && !object.getClass().isEnum() && !isWrapperType(object.getClass())) {
PrimitiveFieldTraceEntry entry = new PrimitiveFieldTraceEntry(var);
for (Field field : var.getVariableClass().getFields()) {
// TODO Check for wrapper types
if (Modifier.isPublic(field.getModifiers()) && !field.getType().equals(void.class) && field.getType().isPrimitive() && !Modifier.isFinal(field.getModifiers()) && !field.isSynthetic()) {
try {
logger.debug("Keeping field " + field + " with value " + field.get(object));
entry.addValue(field, field.get(object));
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
}
}
trace.addEntry(position, var, entry);
}
} catch (CodeUnderTestException e) {
logger.debug("", e);
// throw new UnsupportedOperationException();
}
}
use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.
the class OutputObserver 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 MethodStatement) {
MethodStatement methodStmt = (MethodStatement) statement;
VariableReference varRef = methodStmt.getReturnValue();
try {
Object returnObject = varRef.getObject(scope);
if (exception == null && !methodStmt.getReturnType().equals(Void.TYPE)) {
// we don't save anything if there was an exception
// we are only interested in methods whose return type != void
String className = methodStmt.getDeclaringClassName();
String methodDesc = methodStmt.getDescriptor();
String methodName = methodStmt.getMethodName();
outputCoverage.put(statement.getPosition(), OutputCoverageGoal.createGoalsFromObject(className, methodName, methodDesc, returnObject));
}
} catch (CodeUnderTestException e) {
// ignore?
}
}
}
use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.
the class SymbolicObserver method after.
private void after(FloatPrimitiveStatement statement, Scope scope) {
float valueOf = statement.getValue();
VariableReference varRef = statement.getReturnValue();
String varRefName = varRef.getName();
RealVariable realVariable = buildRealVariable(varRefName, valueOf, -Float.MAX_VALUE, Float.MAX_VALUE);
symb_expressions.put(varRefName, realVariable);
Float float_instance;
try {
float_instance = (Float) varRef.getObject(scope);
} catch (CodeUnderTestException e) {
throw new EvosuiteError(e);
}
ReferenceConstant floatRef = newFloatReference(float_instance, realVariable);
symb_references.put(varRefName, floatRef);
}
use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.
the class SymbolicObserver method call_vm_caller_stack_params.
private void call_vm_caller_stack_params(boolean needThis, List<VariableReference> parameters, Scope scope, String desc) {
int calleeLocalsIndex = 0;
if (needThis)
calleeLocalsIndex++;
for (int i = 0; i < parameters.size(); i++) {
VariableReference p = parameters.get(i);
calleeLocalsIndex += getSize(p.getType());
}
Type[] argTypes = Type.getArgumentTypes(desc);
for (int i = parameters.size() - 1; i >= 0; i--) {
Type argType = argTypes[i];
VariableReference p = parameters.get(i);
try {
Object param_object = p.getObject(scope);
calleeLocalsIndex -= getSize(p.getType());
if (argType.equals(Type.INT_TYPE)) {
int intValue = getIntValue(param_object);
VM.CALLER_STACK_PARAM(intValue, i, calleeLocalsIndex);
} else if (argType.equals(Type.CHAR_TYPE)) {
char charValue = getCharValue(param_object);
VM.CALLER_STACK_PARAM(charValue, i, calleeLocalsIndex);
} else if (argType.equals(Type.BYTE_TYPE)) {
byte byteValue = getByteValue(param_object);
VM.CALLER_STACK_PARAM(byteValue, i, calleeLocalsIndex);
} else if (argType.equals(Type.BOOLEAN_TYPE)) {
boolean booleanValue = getBooleanValue(param_object);
VM.CALLER_STACK_PARAM(booleanValue, i, calleeLocalsIndex);
} else if (argType.equals(Type.SHORT_TYPE)) {
short shortValue = getShortValue(param_object);
VM.CALLER_STACK_PARAM(shortValue, i, calleeLocalsIndex);
} else if (argType.equals(Type.LONG_TYPE)) {
long longValue = getLongValue(param_object);
VM.CALLER_STACK_PARAM(longValue, i, calleeLocalsIndex);
} else if (argType.equals(Type.FLOAT_TYPE)) {
float floatValue = getFloatValue(param_object);
VM.CALLER_STACK_PARAM(floatValue, i, calleeLocalsIndex);
} else if (argType.equals(Type.DOUBLE_TYPE)) {
double doubleValue = getDoubleValue(param_object);
VM.CALLER_STACK_PARAM(doubleValue, i, calleeLocalsIndex);
} else {
VM.CALLER_STACK_PARAM(param_object, i, calleeLocalsIndex);
}
} catch (CodeUnderTestException e) {
throw new RuntimeException(e);
}
}
}
Aggregations