use of org.evosuite.runtime.annotation.Constraints in project evosuite by EvoSuite.
the class TestFactory method addMethodFor.
/**
* Add a call on the method for the given callee at position
*
* @param test
* @param callee
* @param method
* @param position
* @return
* @throws ConstructionFailedException
*/
public VariableReference addMethodFor(TestCase test, VariableReference callee, GenericMethod method, int position) throws ConstructionFailedException {
logger.debug("Adding method {} for {} (Generating {})", method, callee, method.getGeneratedClass());
if (position <= callee.getStPosition()) {
throw new ConstructionFailedException("Cannot insert call on object before the object is defined");
}
currentRecursion.clear();
int length = test.size();
boolean allowNull = true;
Constraints constraints = method.getMethod().getAnnotation(Constraints.class);
if (constraints != null && constraints.noNullInputs()) {
allowNull = false;
}
// Added 'null' as additional parameter - fix for @NotNull annotations issue on evo mailing list
List<VariableReference> parameters = satisfyParameters(test, callee, Arrays.asList(method.getParameterTypes()), Arrays.asList(method.getMethod().getParameters()), position, 1, allowNull, false, true);
int newLength = test.size();
position += (newLength - length);
Statement st = new MethodStatement(test, method, callee, parameters);
VariableReference ret = test.addStatement(st, position);
ret.setDistance(callee.getDistance() + 1);
logger.debug("Success: Adding method {}", method);
return ret;
}
use of org.evosuite.runtime.annotation.Constraints in project evosuite by EvoSuite.
the class Injector method validateBean.
/**
* Check if all beans that need injection have been properly injected
*
* @param instance
* @param clazz
* @param <T>
* @throws FalsePositiveException
* @throws IllegalArgumentException
*/
@Constraints(noNullInputs = true, notMutable = true, noDirectInsertion = true)
public static <T> void validateBean(@BoundInputVariable(initializer = true, atMostOnceWithSameParameters = true) T instance, Class<?> clazz) throws FalsePositiveException, IllegalArgumentException {
Inputs.checkNull(instance, clazz);
for (Field f : getAllFieldsToInject(clazz)) {
f.setAccessible(true);
try {
Object obj = f.get(instance);
if (obj == null) {
throw new FalsePositiveException("Missing dependency injection for field " + f.getName() + " in class " + clazz.getName());
}
// it might be a bean with its own dependency injections.
// but those should be handled in its instantiation
} catch (IllegalAccessException e) {
// shouldn't really happen
logger.warn(e.toString());
}
}
Class<?> parent = clazz.getSuperclass();
if (parent != null && !parent.equals(Object.class)) {
validateBean(instance, parent);
}
}
use of org.evosuite.runtime.annotation.Constraints in project evosuite by EvoSuite.
the class Injector method injectEvent.
@Constraints(noNullInputs = true, notMutable = true, noDirectInsertion = true)
public static <T> void injectEvent(@BoundInputVariable(initializer = true, atMostOnceWithSameParameters = true) T instance, Class<?> clazz) throws IllegalArgumentException {
Inputs.checkNull(instance, clazz);
String field = eventCache.getFieldName(clazz);
assert field != null;
// TODO this will likely need to change in the future
inject(instance, clazz, field, new EvoEvent());
}
use of org.evosuite.runtime.annotation.Constraints in project evosuite by EvoSuite.
the class Injector method injectUserTransaction.
@Constraints(noNullInputs = true, notMutable = true, noDirectInsertion = true)
public static <T> void injectUserTransaction(@BoundInputVariable(initializer = true, atMostOnceWithSameParameters = true) T instance, Class<?> clazz) throws IllegalArgumentException {
Inputs.checkNull(instance, clazz);
String field = userTransactionCache.getFieldName(clazz);
assert field != null;
// TODO this will likely need to change in the future
inject(instance, clazz, field, new EvoUserTransaction());
}
Aggregations