use of org.evosuite.testcase.execution.CodeUnderTestException 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.execution.CodeUnderTestException 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.execution.CodeUnderTestException in project evosuite by EvoSuite.
the class FunctionalMockStatement method execute.
@Override
public Throwable execute(Scope scope, PrintStream out) throws InvocationTargetException, IllegalArgumentException, IllegalAccessException, InstantiationException {
Throwable exceptionThrown = null;
try {
return super.exceptionHandler(new Executer() {
@Override
public void execute() throws InvocationTargetException, IllegalArgumentException, IllegalAccessException, InstantiationException, CodeUnderTestException {
// First create the listener
listener = createInvocationListener();
// then create the mock
Object ret;
try {
logger.debug("Mockito: create mock for {}", targetClass);
ret = mock(targetClass, createMockSettings());
// ret = mockCreator.invoke(null,targetClass,withSettings().invocationListeners(listener));
// execute all "when" statements
int index = 0;
logger.debug("Mockito: going to mock {} different methods", mockedMethods.size());
for (MethodDescriptor md : mockedMethods) {
if (!md.shouldBeMocked()) {
// no need to mock a method that returns void
logger.debug("Mockito: method {} cannot be mocked", md.getMethodName());
continue;
}
// target method, eg foo.aMethod(...)
Method method = md.getMethod();
// this is needed if method is protected: it couldn't be called here, although fine in
// the generated JUnit tests
method.setAccessible(true);
// target inputs
Object[] targetInputs = new Object[md.getNumberOfInputParameters()];
for (int i = 0; i < targetInputs.length; i++) {
logger.debug("Mockito: executing matcher {}/{}", (1 + i), targetInputs.length);
targetInputs[i] = md.executeMatcher(i);
}
logger.debug("Mockito: going to invoke method {} with {} matchers", method.getName(), targetInputs.length);
if (!method.getDeclaringClass().isAssignableFrom(ret.getClass())) {
String msg = "Mismatch between callee's class " + ret.getClass() + " and method's class " + method.getDeclaringClass();
msg += "\nTarget class classloader " + targetClass.getClassLoader() + " vs method's classloader " + method.getDeclaringClass().getClassLoader();
throw new EvosuiteError(msg);
}
// actual call foo.aMethod(...)
Object targetMethodResult;
try {
if (targetInputs.length == 0) {
targetMethodResult = method.invoke(ret);
} else {
targetMethodResult = method.invoke(ret, targetInputs);
}
} catch (InvocationTargetException e) {
logger.error("Invocation of mocked {}.{}() threw an exception. " + "This means the method was not mocked", targetClass.getName(), method.getName());
throw e;
} catch (IllegalArgumentException | IllegalAccessError e) {
// FIXME: Happens for reasons I don't understand. By throwing a CodeUnderTestException EvoSuite
// will just ignore that mocking statement and continue, instead of crashing
logger.error("IAE on <" + method + "> when called with " + Arrays.toString(targetInputs));
throw new CodeUnderTestException(e);
}
// when(...)
logger.debug("Mockito: call 'when'");
OngoingStubbing<Object> retForThen = Mockito.when(targetMethodResult);
// thenReturn(...)
Object[] thenReturnInputs = null;
try {
int size = Math.min(md.getCounter(), Properties.FUNCTIONAL_MOCKING_INPUT_LIMIT);
thenReturnInputs = new Object[size];
for (int i = 0; i < thenReturnInputs.length; i++) {
// the position in flat parameter list
int k = i + index;
if (k >= parameters.size()) {
// throw new RuntimeException("EvoSuite ERROR: index " + k + " out of " + parameters.size());
throw new CodeUnderTestException(new FalsePositiveException("EvoSuite ERROR: index " + k + " out of " + parameters.size()));
}
VariableReference parameterVar = parameters.get(i + index);
thenReturnInputs[i] = parameterVar.getObject(scope);
CodeUnderTestException codeUnderTestException = null;
if (thenReturnInputs[i] == null && method.getReturnType().isPrimitive()) {
codeUnderTestException = new CodeUnderTestException(new NullPointerException());
} else if (thenReturnInputs[i] != null && !TypeUtils.isAssignable(thenReturnInputs[i].getClass(), method.getReturnType())) {
codeUnderTestException = new CodeUnderTestException(new UncompilableCodeException("Cannot assign " + parameterVar.getVariableClass().getName() + " to " + method.getReturnType()));
}
if (codeUnderTestException != null) {
throw codeUnderTestException;
}
thenReturnInputs[i] = fixBoxing(thenReturnInputs[i], method.getReturnType());
}
} catch (Exception e) {
// be sure "then" is always called after a "when", otherwise Mockito might end up in
// a inconsistent state
retForThen.thenThrow(new RuntimeException("Failed to setup mock: " + e.getMessage()));
throw e;
}
// final call when(...).thenReturn(...)
logger.debug("Mockito: executing 'thenReturn'");
if (thenReturnInputs == null || thenReturnInputs.length == 0) {
retForThen.thenThrow(new RuntimeException("No valid return value"));
} else if (thenReturnInputs.length == 1) {
retForThen.thenReturn(thenReturnInputs[0]);
} else {
Object[] values = Arrays.copyOfRange(thenReturnInputs, 1, thenReturnInputs.length);
retForThen.thenReturn(thenReturnInputs[0], values);
}
index += thenReturnInputs == null ? 0 : thenReturnInputs.length;
}
} catch (CodeUnderTestException e) {
throw e;
} catch (java.lang.NoClassDefFoundError e) {
AtMostOnceLogger.error(logger, "Cannot use Mockito on " + targetClass + " due to failed class initialization: " + e.getMessage());
// or should throw an exception?
return;
} catch (MockitoException | IllegalAccessException | IllegalAccessError | IllegalArgumentException e) {
// FIXME: Happens for reasons I don't understand. By throwing a CodeUnderTestException EvoSuite
// will just ignore that mocking statement and continue, instead of crashing
AtMostOnceLogger.error(logger, "Cannot use Mockito on " + targetClass + " due to IAE: " + e.getMessage());
// or should throw an exception?
throw new CodeUnderTestException(e);
} catch (Throwable t) {
AtMostOnceLogger.error(logger, "Failed to use Mockito on " + targetClass + ": " + t.getMessage());
throw new EvosuiteError(t);
}
// finally, activate the listener
listener.activate();
try {
retval.setObject(scope, ret);
} catch (CodeUnderTestException e) {
throw e;
} catch (Throwable e) {
throw new EvosuiteError(e);
}
}
/**
* a "char" can be used for a "int". But problem is that Mockito takes as input
* Object, and so those get boxed. However, a Character cannot be used for a "int",
* so we need to be sure to convert it here
*
* @param value
* @param expectedType
* @return
*/
private Object fixBoxing(Object value, Class<?> expectedType) {
if (!expectedType.isPrimitive()) {
return value;
}
Class<?> valuesClass = value.getClass();
assert !valuesClass.isPrimitive();
if (expectedType.equals(Integer.TYPE)) {
if (valuesClass.equals(Character.class)) {
value = (int) ((Character) value).charValue();
} else if (valuesClass.equals(Byte.class)) {
value = (int) ((Byte) value).intValue();
} else if (valuesClass.equals(Short.class)) {
value = (int) ((Short) value).intValue();
}
}
if (expectedType.equals(Double.TYPE)) {
if (valuesClass.equals(Integer.class)) {
value = (double) ((Integer) value).intValue();
} else if (valuesClass.equals(Byte.class)) {
value = (double) ((Byte) value).intValue();
} else if (valuesClass.equals(Character.class)) {
value = (double) ((Character) value).charValue();
} else if (valuesClass.equals(Short.class)) {
value = (double) ((Short) value).intValue();
} else if (valuesClass.equals(Long.class)) {
value = (double) ((Long) value).longValue();
} else if (valuesClass.equals(Float.class)) {
value = (double) ((Float) value).floatValue();
}
}
if (expectedType.equals(Float.TYPE)) {
if (valuesClass.equals(Integer.class)) {
value = (float) ((Integer) value).intValue();
} else if (valuesClass.equals(Byte.class)) {
value = (float) ((Byte) value).intValue();
} else if (valuesClass.equals(Character.class)) {
value = (float) ((Character) value).charValue();
} else if (valuesClass.equals(Short.class)) {
value = (float) ((Short) value).intValue();
} else if (valuesClass.equals(Long.class)) {
value = (float) ((Long) value).longValue();
}
}
if (expectedType.equals(Long.TYPE)) {
if (valuesClass.equals(Integer.class)) {
value = (long) ((Integer) value).intValue();
} else if (valuesClass.equals(Byte.class)) {
value = (long) ((Byte) value).intValue();
} else if (valuesClass.equals(Character.class)) {
value = (long) ((Character) value).charValue();
} else if (valuesClass.equals(Short.class)) {
value = (long) ((Short) value).intValue();
}
}
if (expectedType.equals(Short.TYPE)) {
if (valuesClass.equals(Integer.class)) {
value = (short) ((Integer) value).intValue();
} else if (valuesClass.equals(Byte.class)) {
value = (short) ((Byte) value).intValue();
} else if (valuesClass.equals(Short.class)) {
value = (short) ((Short) value).intValue();
} else if (valuesClass.equals(Character.class)) {
value = (short) ((Character) value).charValue();
} else if (valuesClass.equals(Long.class)) {
value = (short) ((Long) value).intValue();
}
}
if (expectedType.equals(Byte.TYPE)) {
if (valuesClass.equals(Integer.class)) {
value = (byte) ((Integer) value).intValue();
} else if (valuesClass.equals(Short.class)) {
value = (byte) ((Short) value).intValue();
} else if (valuesClass.equals(Byte.class)) {
value = (byte) ((Byte) value).intValue();
} else if (valuesClass.equals(Character.class)) {
value = (byte) ((Character) value).charValue();
} else if (valuesClass.equals(Long.class)) {
value = (byte) ((Long) value).intValue();
}
}
return value;
}
@Override
public Set<Class<? extends Throwable>> throwableExceptions() {
Set<Class<? extends Throwable>> t = new LinkedHashSet<>();
t.add(InvocationTargetException.class);
return t;
}
});
} catch (InvocationTargetException e) {
exceptionThrown = e.getCause();
}
return exceptionThrown;
}
use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.
the class ConstructorStatement method execute.
// TODO: Handle inner classes (need instance parameter for newInstance)
/**
* {@inheritDoc}
*/
@Override
public Throwable execute(final Scope scope, PrintStream out) throws InvocationTargetException, IllegalArgumentException, InstantiationException, IllegalAccessException {
// PrintStream old_out = System.out;
// PrintStream old_err = System.err;
// System.setOut(out);
// System.setErr(out);
logger.trace("Executing constructor " + constructor.toString());
final Object[] inputs = new Object[parameters.size()];
Throwable exceptionThrown = null;
try {
return super.exceptionHandler(new Executer() {
@Override
public void execute() throws InvocationTargetException, IllegalArgumentException, IllegalAccessException, InstantiationException, CodeUnderTestException {
java.lang.reflect.Type[] parameterTypes = constructor.getParameterTypes();
for (int i = 0; i < parameters.size(); i++) {
VariableReference parameterVar = parameters.get(i);
try {
inputs[i] = parameterVar.getObject(scope);
} catch (CodeUnderTestException e) {
throw e;
// throw new CodeUnderTestException(e.getCause());
// throw CodeUnderTestException.throwException(e.getCause());
} catch (Throwable e) {
// FIXME: this does not seem to propagate to client root. Is this normal behavior?
logger.error("Class " + Properties.TARGET_CLASS + ". Error encountered: " + e);
assert (false);
throw new EvosuiteError(e);
}
if (inputs[i] != null && !TypeUtils.isAssignable(inputs[i].getClass(), parameterTypes[i])) {
// !parameterVar.isAssignableTo(parameterTypes[i])) {
throw new CodeUnderTestException(new UncompilableCodeException("Cannot assign " + parameterVar.getVariableClass().getName() + " to " + parameterTypes[i]));
}
if (inputs[i] == null && constructor.getConstructor().getParameterTypes()[i].isPrimitive()) {
throw new CodeUnderTestException(new NullPointerException());
}
}
// If this is a non-static member class, the first parameter must not be null
if (constructor.getConstructor().getDeclaringClass().isMemberClass() && !Modifier.isStatic(constructor.getConstructor().getDeclaringClass().getModifiers())) {
if (inputs[0] == null) {
// throw new NullPointerException();
throw new CodeUnderTestException(new NullPointerException());
}
}
Object ret = constructor.getConstructor().newInstance(inputs);
try {
// assert(retval.getVariableClass().isAssignableFrom(ret.getClass())) :"we want an " + retval.getVariableClass() + " but got an " + ret.getClass();
retval.setObject(scope, ret);
} catch (CodeUnderTestException e) {
throw e;
// throw CodeUnderTestException.throwException(e);
} catch (Throwable e) {
throw new EvosuiteError(e);
}
}
@Override
public Set<Class<? extends Throwable>> throwableExceptions() {
Set<Class<? extends Throwable>> t = new LinkedHashSet<Class<? extends Throwable>>();
t.add(InvocationTargetException.class);
return t;
}
});
} catch (InvocationTargetException e) {
VM.disableCallBacks();
exceptionThrown = e.getCause();
if (logger.isDebugEnabled()) {
try {
logger.debug("Exception thrown in constructor: " + e.getCause());
}// this can happen if SUT throws exception on toString
catch (Exception ex) {
logger.debug("Exception thrown in constructor and SUT gives issue when calling e.getCause()", ex);
}
}
}
return exceptionThrown;
}
use of org.evosuite.testcase.execution.CodeUnderTestException in project evosuite by EvoSuite.
the class SymbolicObserver method after.
private void after(BytePrimitiveStatement statement, Scope scope) {
byte valueOf = statement.getValue();
VariableReference varRef = statement.getReturnValue();
String varRefName = varRef.getName();
IntegerVariable integerVariable = buildIntegerVariable(varRefName, valueOf, Byte.MIN_VALUE, Byte.MAX_VALUE);
symb_expressions.put(varRefName, integerVariable);
Byte byte_instance;
try {
byte_instance = (Byte) varRef.getObject(scope);
} catch (CodeUnderTestException e) {
throw new EvosuiteError(e);
}
ReferenceConstant byteRef = newByteReference(byte_instance, integerVariable);
symb_references.put(varRefName, byteRef);
}
Aggregations