use of org.evosuite.runtime.annotation.Constraints in project evosuite by EvoSuite.
the class Injector method executePostConstruct.
/**
* Executed the method annotated with @PostConstruct
*
* @param instance
*/
@Constraints(noNullInputs = true, notMutable = true, noDirectInsertion = true)
public static void executePostConstruct(@BoundInputVariable(initializer = true, atMostOnceWithSameParameters = true) Object instance, Class<?> clazz) throws IllegalArgumentException {
Inputs.checkNull(instance, clazz);
if (!clazz.isAssignableFrom(instance.getClass())) {
throw new IllegalArgumentException("Class " + clazz + " is not assignable from " + instance.getClass());
}
if (!hasPostConstruct(clazz)) {
throw new IllegalArgumentException("The class " + clazz.getName() + " does not have a @PostConstruct");
}
Method m = postConstructCache.get(clazz);
assert m != null;
try {
m.invoke(instance);
} catch (IllegalAccessException e) {
// should never happen
logger.error(e.toString());
} catch (InvocationTargetException e) {
throw new RuntimeException("Failed to execute @PostConstructor in " + clazz.getName(), e);
}
}
use of org.evosuite.runtime.annotation.Constraints in project evosuite by EvoSuite.
the class ConstraintHelper method getExcludedMethods.
/**
* Retrieve all blacklisted methods in the test.
* This is based on the 'excludeOthers' constraint
*
* @param tc
* @return
* @throws IllegalArgumentException
*/
public static List<String[]> getExcludedMethods(TestCase tc) throws IllegalArgumentException {
Inputs.checkNull(tc);
List<String[]> list = new ArrayList<>();
for (int i = 0; i < tc.size(); i++) {
Statement st = tc.getStatement(i);
Constraints constraints = getConstraints(st);
if (constraints == null) {
continue;
}
GenericAccessibleObject ao = st.getAccessibleObject();
Class<?> declaringClass = ao.getDeclaringClass();
for (String excluded : constraints.excludeOthers()) {
String[] klassAndMethod = getClassAndMethod(excluded, declaringClass);
list.add(klassAndMethod);
}
}
return list;
}
use of org.evosuite.runtime.annotation.Constraints in project evosuite by EvoSuite.
the class MethodStatement method mutate.
/**
* Go through parameters of method call and apply local search
*
* @param test
* @param factory
*/
@Override
public boolean mutate(TestCase test, TestFactory factory) {
if (Randomness.nextDouble() >= Properties.P_CHANGE_PARAMETER)
return false;
Constraints constraint = method.getMethod().getAnnotation(Constraints.class);
if (constraint != null && constraint.notMutable()) {
return false;
}
List<VariableReference> parameters = getParameterReferences();
boolean changed = false;
int max = parameters.size();
if (!isStatic()) {
max++;
}
if (max == 0)
// Static method with no parameters...
return false;
double pParam = 1.0 / max;
if (!isStatic() && Randomness.nextDouble() < pParam) {
// replace callee
VariableReference callee = getCallee();
List<VariableReference> objects = test.getObjects(callee.getType(), getPosition());
objects.remove(callee);
objects = objects.stream().filter(var -> !(test.getStatement(var.getStPosition()) instanceof FunctionalMockStatement)).collect(Collectors.toList());
if (!objects.isEmpty()) {
VariableReference replacement = Randomness.choice(objects);
setCallee(replacement);
changed = true;
}
}
for (int numParameter = 0; numParameter < parameters.size(); numParameter++) {
if (Randomness.nextDouble() < pParam) {
if (mutateParameter(test, numParameter))
changed = true;
}
}
return changed;
}
use of org.evosuite.runtime.annotation.Constraints in project evosuite by EvoSuite.
the class ConstructorStatement method mutate.
/**
* Go through parameters of constructor call and apply local search
*
* @param test
* @param factory
*/
/* (non-Javadoc)
* @see org.evosuite.testcase.AbstractStatement#mutate(org.evosuite.testcase.TestCase, org.evosuite.testcase.TestFactory)
*/
@Override
public boolean mutate(TestCase test, TestFactory factory) {
if (Randomness.nextDouble() >= Properties.P_CHANGE_PARAMETER)
return false;
Constraints constraint = constructor.getConstructor().getAnnotation(Constraints.class);
if (constraint != null && constraint.notMutable()) {
return false;
}
List<VariableReference> parameters = getParameterReferences();
if (parameters.isEmpty())
return false;
double pParam = 1.0 / parameters.size();
boolean changed = false;
for (int numParameter = 0; numParameter < parameters.size(); numParameter++) {
if (Randomness.nextDouble() < pParam) {
if (mutateParameter(test, numParameter))
changed = true;
}
}
return changed;
}
use of org.evosuite.runtime.annotation.Constraints in project evosuite by EvoSuite.
the class EntityWithParametersStatement method mutateParameter.
protected boolean mutateParameter(TestCase test, int numParameter) {
// replace a parameter
VariableReference parameter = parameters.get(numParameter);
List<VariableReference> objects = test.getObjects(parameter.getType(), getPosition());
objects.remove(parameter);
objects.remove(getReturnValue());
NullStatement nullStatement = new NullStatement(test, parameter.getType());
Statement copy = null;
// check if NULL is a valid option
Constraints constraint = getConstraints();
boolean avoidNull = constraint != null && constraint.noNullInputs();
if (Properties.HONOUR_DATA_ANNOTATIONS && (numParameter < parameterAnnotations.length)) {
if (GenericUtils.isAnnotationTypePresent(parameterAnnotations[numParameter], GenericUtils.NONNULL)) {
avoidNull = true;
}
}
if (avoidNull) {
// be sure to remove all references pointing to NULL
Iterator<VariableReference> iter = objects.iterator();
while (iter.hasNext()) {
VariableReference ref = iter.next();
if (ref instanceof NullReference) {
iter.remove();
}
}
} else {
// If it's not a primitive, then changing to null is also an option
if (!parameter.isPrimitive()) {
objects.add(nullStatement.getReturnValue());
}
}
// we consider adding an instance
if (getNumParametersOfType(parameter.getVariableClass()) + 1 < objects.size()) {
Statement originalStatement = test.getStatement(parameter.getStPosition());
copy = originalStatement.clone(test);
if (originalStatement instanceof PrimitiveStatement<?>) {
((PrimitiveStatement<?>) copy).delta();
}
objects.add(copy.getReturnValue());
}
if (objects.isEmpty())
return false;
VariableReference replacement = Randomness.choice(objects);
if (replacement == nullStatement.getReturnValue()) {
test.addStatement(nullStatement, getPosition());
} else if (copy != null && replacement == copy.getReturnValue()) {
test.addStatement(copy, getPosition());
}
replaceParameterReference(replacement, numParameter);
return true;
}
Aggregations